My matplotlib.pyplot legend is being cut off

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

    Here is another way of making space (shrinking an axis):

    # get the current axis
    ax = plt.gca()
    # Shink current axis by 20%
    box = ax.get_position()
    ax.set_position([box.x0, box.y0, box.width * 0.8, box.height])
    

    where 0.8 scales the width of the axis by 20%. On my win7 64 machine, using a factor greater than 1 will make room for the legend if it's outside the plot.

    This code was referenced from: How to put the legend out of the plot

提交回复
热议问题