How to put the legend out of the plot

后端 未结 17 3158
时光说笑
时光说笑 2020-11-21 04:42

I have a series of 20 plots (not subplots) to be made in a single figure. I want the legend to be outside of the box. At the same time, I do not want to change the axes, a

17条回答
  •  广开言路
    2020-11-21 05:23

    In addition to all the excellent answers here, newer versions of matplotlib and pylab can automatically determine where to put the legend without interfering with the plots, if possible.

    pylab.legend(loc='best')
    

    This will automatically place the legend away from the data if possible! Compare the use of loc='best'

    However, if there is no place to put the legend without overlapping the data, then you'll want to try one of the other answers; using loc="best" will never put the legend outside of the plot.

提交回复
热议问题