我有一个带有两个y轴的情节,使用twinx().我也给线条贴了标签,并希望用它们来展示legend(),但我只是成功地在图例中获得了一个轴的标签:
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()
Run Code Online (Sandbox Code Playgroud)
所以我只得到图例中第一个轴的标签,而不是第二个轴的标签"temp".我怎么能将这第三个标签添加到图例中?

我想在相同的图上用相同的x轴和次y轴绘制两个时间序列.我已经以某种方式实现了这一点,但是两个传说重叠并且无法给x轴和次y轴赋予标签.我尝试在左上角和右上角放置两个图例,但它仍然不起作用.
码:
plt.figure(figsize=(12,5))
# Number of request every 10 minutes
log_10minutely_count_Series = log_df['IP'].resample('10min').count()
log_10minutely_count_Series.name="Count"
log_10minutely_count_Series.plot(color='blue', grid=True)
plt.legend(loc='upper left')
plt.xlabel('Number of request ever 10 minute')
# Sum of response size over each 10 minute
log_10minutely_sum_Series = log_df['Bytes'].resample('10min').sum()
log_10minutely_sum_Series.name = 'Sum'
log_10minutely_sum_Series.plot(color='red',grid=True, secondary_y=True)
plt.legend(loc='upper right')
plt.show()
Run Code Online (Sandbox Code Playgroud)
提前致谢
我正在通过pandas绘制界面通过第二个y轴绘制pandas数据框,如文档中所述:
df = pd.DataFrame(np.random.randn(24*3, 3),
index=pd.date_range('1/1/2019', periods=24*3, freq='h'))
df.columns = ['A (left)', 'B (right)', 'C (right)']
ax = df.plot(secondary_y=['B (right)', 'C (right)'], mark_right=False)
ax.set_ylabel('A scale')
ax.right_ax.set_ylabel('BC scale')
ax.legend(loc='upper right')
plt.show()
Run Code Online (Sandbox Code Playgroud)
产生
可以看出,当我使用设置位置时,图例会丢失条目ax.legend(loc='upper right')。
有谁知道我如何设置图例位置并保留所有条目?
提前致谢!