子图中的Matplotlib图例 [英] Matplotlib legends in subplot

查看:68
本文介绍了子图中的Matplotlib图例的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在下面的每个子图中放入图例.我试过 plt.legend 但没有用.

I would like to put legends inside each one of the subplots below. I've tried with plt.legend but it didn't work.

有什么建议吗?

预先感谢:-)

f, (ax1, ax2, ax3) = plt.subplots(3, sharex=True, sharey=True)
ax1.plot(xtr, color='r', label='Blue stars')
ax2.plot(ytr, color='g')
ax3.plot(ztr, color='b')
ax1.set_title('2012/09/15')
plt.legend([ax1, ax2, ax3],["HHZ 1", "HHN", "HHE"])
plt.show()

根据 atomh33ls 的建议:

With the suggestion from atomh33ls:

ax1.legend("HHZ 1",loc="upper right")
ax2.legend("HHN",loc="upper right")
ax3.legend("HHE",loc="upper right")

图例位置固定,但是字符串似乎有问题,因为每个字母都放在新行中.

The legend position is fixed, however it seems to have a problem with the strings, because each letter is placed in a new line.

有人知道怎么解决吗?

推荐答案

这应该有效:

ax1.plot(xtr, color='r', label='HHZ 1')
ax1.legend(loc="upper right")
ax2.plot(xtr, color='r', label='HHN')
ax2.legend(loc="upper right")
ax3.plot(xtr, color='r', label='HHE')
ax3.legend(loc="upper right")

这篇关于子图中的Matplotlib图例的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆