How to put the legend out of the plot

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

    Here is an example from the matplotlib tutorial found here. This is one of the more simpler examples but I added transparency to the legend and added plt.show() so you can paste this into the interactive shell and get a result:

    import matplotlib.pyplot as plt
    p1, = plt.plot([1, 2, 3])
    p2, = plt.plot([3, 2, 1])
    p3, = plt.plot([2, 3, 1])
    plt.legend([p2, p1, p3], ["line 1", "line 2", "line 3"]).get_frame().set_alpha(0.5)
    plt.show()
    

提交回复
热议问题