python - 显示图例 pandas 绘制多个 Y 轴
问题描述
谁能给我一个关于如何在我的情节中添加图例的提示?我很高兴情节看起来只需要一个传奇:)
# data from pandas df's
zoom_plot = fc1_dataset.loc['2021-8-9'].between_time('5:00', '17:00')
_no_vfd_sig = zoom_plot[['fc1_flag','duct_static','duct_static_setpoint']]
_vfdsig = zoom_plot.vfd_speed
# using subplots() function
fig, ax = plt.subplots(figsize=(25,8))
plt.title('Visually verify FDD is flagging OK')
# using the twinx() for creating another
# axes object for secondary y-Axis
ax2 = ax.twinx()
ax.plot(zoom_plot.index, _no_vfd_sig)
ax2.plot(zoom_plot.index, _vfdsig, color = 'r')
# giving labels to the axises
ax.set_xlabel('Date')
ax.set_ylabel('Duct Pressure and FDD Flag')
# secondary y-axis label
ax2.set_ylabel('% Fan Speed')
# defining display layout
plt.tight_layout()
# show plot
plt.show()
解决方案
和之前的代码一样...
plot1, = ax.plot(zoom_plot.index, _no_vfd_sig)
plot2, = ax2.plot(zoom_plot.index, _vfdsig, color = 'r')
ax.set_xlabel('Date')
ax.set_ylabel('Duct Pressure and FDD Flag')
# secondary y-axis label
ax2.set_ylabel('% Fan Speed')
plt.legend([plot1,plot2],["plot 1", "plot 2"])
# defining display layout
plt.tight_layout()
# show plot
plt.show()
推荐阅读
- xcode - 如何强制 Flutter 更新我的版本和内部版本号?
- python - 如何使用 openpyxl 粘贴特定单元格?
- vba - 如何使用 VBA 将 Microsoft Word 内容控制字段数据写入文本文件?
- python - 在单独的窗口 Tkinter 中绘制由类定义的小部件
- visual-studio-code - 更漂亮的扩展在文件保存时不格式化代码
- c++ - 构建 Windows 文件搜索应用程序
- iframe - pipedrive web 表单的 CSS 样式(嵌入为 iframe)
- java - FAT JAR 外的 Spring Boot 属性
- google-cloud-platform - 错误: (gcloud.functions.deploy) ResponseError: status=[403], code=[Forbidden]
- java - 使用 Apache Camel 进行编排时如何返回我的 API 异常?