首页
学习
活动
专区
工具
TVP
发布
社区首页 >问答首页 >如何将twinx()的次轴添加到legend中?

如何将twinx()的次轴添加到legend中?

提问于 2018-01-16 00:34:26
回答 2关注 0查看 2.4K

一个有两个y轴的图,用twinx().我也给线条贴上了标签,使用legend(),但我只获得了图例中一个轴的标签:

代码语言:javascript
复制
import numpy as np
import matplotlib.pyplot as plt
from matplotlib import rc
rc('mathtext', default='regular')

fig = plt.figure()
ax = fig.add_subplot(111)
ax.plot(time, Swdown, '-', label = 'Swdown')
ax.plot(time, Rn, '-', label = 'Rn')
ax2 = ax.twinx()
ax2.plot(time, temp, '-r', label = 'temp')
ax.legend(loc=0)
ax.grid()
ax.set_xlabel("Time (h)")
ax.set_ylabel(r"Radiation ($MJ\,m^{-2}\,d^{-1}$)")
ax2.set_ylabel(r"Temperature ($^\circ$C)")
ax2.set_ylim(0, 35)
ax.set_ylim(-20,100)
plt.show()

如上,只得到图例中第一个轴的标签,而不是第二个轴的标签‘temp’。我怎样才能把第三个标签添加到legend中呢?

相关文章

相似问题

相关问答用户
领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档