比如我想一张图显示好几个legend
如果是直接用plt.plot()的话,使用plt.gca().add_artist()函数即可。
示范代码:
plt.plot([1,2,3],[3,6,9], label=[3,6,9])
l1 = plt.legend(loc='upper right')
plt.legend(loc='upper left')
plt.gca().add_artist(l1)
plt.show()
因为用了第二个plt.legend之后,第一个会被覆盖掉,使用add_artist可以把它添加进来一起显示
如果使用ax.plot()的话, 如下面这种形式
f = Figure(figsize=(16, 6), dpi=100)
a = f.add_subplot(111)
l1 = a.legend(...)
a.legend()
f.gca().add_artist(l1)
...
legend要放在a后面,即a.legend(…), 而gca().add_artist()这个函数要用f调用,而不是a,即f.gca().add_artist()