How do I fit long title?

后端 未结 4 858
北恋
北恋 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:07

    Here's what I've finally used:

    #!/usr/bin/env python3
    
    import matplotlib
    from matplotlib import pyplot as plt
    from textwrap import wrap
    
    data = range(5)
    
    fig = plt.figure()
    ax = fig.add_subplot(111)
    
    ax.plot(data, data)
    
    title = ax.set_title("\n".join(wrap("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.", 60)))
    
    fig.tight_layout()
    title.set_y(1.05)
    fig.subplots_adjust(top=0.8)
    
    fig.savefig("1.png")
    

    enter image description here

提交回复
热议问题