我有一个带有两个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".我怎么能将这第三个标签添加到图例中?

我有一个如下表,它存储在名为“data”的 pandas 数据框中。
| 第1栏 | 设备1 | 事件发生率% | % 事件距离 | % 非事件分布 | % 总分布 |
|---|---|---|---|---|---|
| 0 | 安卓 | 3.08 | 27.3 | 32.96 | 32.75 |
| 1 | Chrome操作系统 | 4.05 | 0.47 | 0.42 | 0.43 |
| 2 | 铬操作系统 | 9.95 | 0.23 | 0.08 | 0.09 |
| 3 | Linux | 2.27 | 0.05 | 0.09 | 0.09 |
| 4 | 苹果系统 | 6.43 | 4.39 | 2.45 | 2.52 |
| 5 | 其他的 | 2.64 | 7.41 | 10.48 | 10.36 |
| 6 | 视窗 | 5.7 | 15.89 | 10.08 | 10.3 |
| 7 | iOS系统 | 3.76 | 44.26 | 43.44 | 43.47 |
我正在尝试创建一个所需的seaborn/matplot图表,如下所示,它是在Excel中创建的。
这是我的Python代码:
feature = 'Device1'
fig, ax1 = plt.subplots(figsize=(10,6))
color = 'tab:blue'
title = 'Event rate by ' + feature …Run Code Online (Sandbox Code Playgroud)