My matplotlib.pyplot legend is being cut off

后端 未结 5 675
时光取名叫无心
时光取名叫无心 2021-02-01 01:33

I\'m attempting to create a plot with a legend to the side of it using matplotlib. I can see that the plot is being created, but the image bounds do not allow the entire legend

5条回答
  •  悲哀的现实
    2021-02-01 02:02

    Just use plt.tight_layout()

    import matplotlib.pyplot as plt
        
    fig = plt.figure(1)
    plt.plot([1, 2, 3], [1, 0, 1], label='A')
    plt.plot([1, 2, 3], [1, 2, 2], label='B')
    plt.legend(loc='center left', bbox_to_anchor=(1, 0))
    
    plt.tight_layout()
    

    This is probably introduced in the newer matplotlib version and neatly does the job.

提交回复
热议问题