How to put the legend out of the plot

后端 未结 17 3232
时光说笑
时光说笑 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:16

    Short Answer: Invoke draggable on the legend and interactively move it wherever you want:

    ax.legend().draggable()
    

    Long Answer: If you rather prefer to place the legend interactively/manually rather than programmatically, you can toggle the draggable mode of the legend so that you can drag it to wherever you want. Check the example below:

    import matplotlib.pylab as plt
    import numpy as np
    #define the figure and get an axes instance
    fig = plt.figure()
    ax = fig.add_subplot(111)
    #plot the data
    x = np.arange(-5, 6)
    ax.plot(x, x*x, label='y = x^2')
    ax.plot(x, x*x*x, label='y = x^3')
    ax.legend().draggable()
    plt.show()
    

提交回复
热议问题