How do I fit long title?

后端 未结 4 862
北恋
北恋 2020-12-25 12:47

There\'s a similar question - but I can\'t make the solution proposed there work.

Here\'s an example plot with a long title:

#!/usr/bin/env python

i         


        
4条回答
  •  佛祖请我去吃肉
    2020-12-25 13:03

    • matplotlib: Auto-wrapping text is the official answer from the matplotlib version 3.1.2 documentation
    • matplotlib: Text properties and layout are also useful
    • It doesn't work to display an inline plot in Jupyter because of GitHub: Text wrapping doesn't seem to work in jupyter notebook #10869 unless you do this answer Matplotlib and Ipython-notebook: Displaying exactly the figure that will be saved
      • plt.savefig(...) seems to work properly with wrap
    import matplotlib.pyplot as plt
    
    x = [1,2,3]
    y = [4,5,6]
    
    # initialization:
    fig, axes = plt.subplots(figsize=(8.0, 5.0))
    
    # title:
    myTitle = "Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all."
    
    # lines:
    axes.plot(x, y)
    
    # set title
    axes.set_title(myTitle, loc='center', wrap=True)
    
    plt.show()
    

    • The following also works
    plt.figure(figsize=(8, 5))
    
    # title:
    myTitle = "Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all."
    
    # lines:
    plt.plot(x, y)
    
    # set title
    plt.title(myTitle, loc='center', wrap=True)
    
    plt.show()
    

    Note

    • The following way of adding an axes is deprecated
    # lines:
    fig.add_subplot(111).plot(x, y)
    
    # title:
    myTitle = "Some really really long long long title I really really need - and just can't - just can't - make it any - simply any - shorter - at all."
    
    fig.add_subplot(111).set_title(myTitle, loc='center', wrap=True)
    

    MatplotlibDeprecationWarning: Adding an axes using the same arguments as a previous axes currently reuses the earlier instance. In a future version, a new instance will always be created and returned. Meanwhile, this warning can be suppressed, and the future behavior ensured, by passing a unique label to each axes instance.

提交回复
热议问题