My matplotlib.pyplot legend is being cut off

后端 未结 5 677
时光取名叫无心
时光取名叫无心 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:21

    Edit: @gcalmettes posted a better answer.
    His solution should probably be used instead of the method shown below.
    Nonetheless I'll leave this since it sometimes helps to see different ways of doing things.


    As shown in the legend plotting guide, you can make room for another subplot and place the legend there.

    import matplotlib.pyplot as plt
    ax = plt.subplot(121) # <- with 2 we tell mpl to make room for an extra subplot
    ax.plot([1,2,3], color='red', label='thin red line')
    ax.plot([1.5,2.5,3.5], color='blue', label='thin blue line')
    ax.legend(bbox_to_anchor=(1.05, 1), loc=2, borderaxespad=0.)
    plt.show()
    

    Produces:

    enter image description here

提交回复
热议问题