python - 在 Python for 循环中,如何为绘图创建图例?
问题描述
我已经成功创建了四个子图和两个具有双 y 轴特征的子图,如下所示。每个子图都有许多不同的图。我想添加一个传奇。但是,我不能用代码来做到这一点。该图如下:
我的代码如下:
fig, axs = plt.subplots(2,2,figsize=(17,10))
fig.legend(loc="center right", fontsize=13,fancybox=True, framealpha=1, shadow=True, borderpad=1)
plt.rc('font',family='Times New Roman')
.
.
for i,j in zip(IV_start_index,IV_start_index[1:]): # This is simple code to access present and next element in a list
axs[0][0].plot(module_allData_df['Time'].iloc[mpp_index],pmpp_theo,'bs',label="Theoretical")
axs[0][0].plot(module_allData_df['Time'].iloc[mpp_index],pmpp_act,'rd',label="Actual")
.
.
plt.suptitle('A NIST Module %s power loss analysis'%(module_allData_df['Time'].loc[i].strftime('%Y-%m-%d')),fontsize=18) #
plt.savefig('All_day_power_loss')
输出是:
No handles with labels found to put in legend.
你能帮我更正我的代码吗?
更正:我确实更改了代码,如下所示。
for i,j in zip(IV_start_index,IV_start_index[1:]): # This is simple code to access present and next element in a list
axs[0][0].plot(module_allData_df['Time'].iloc[mpp_index],pmpp_theo,'bs',label="Theoretical")
axs[0][0].plot(module_allData_df['Time'].iloc[mpp_index],pmpp_act,'rd',label="Actual")
axs[0][0].legend()
它创造了许多传说。输出图如下:
解决方案
经过一系列的跟踪,我对我的代码执行了以下操作:
for i,j in zip(IV_start_index,IV_start_index[1:]): # This is simple code to access present and next element in a list
axs[0][0].plot(module_allData_df['Time'].iloc[mpp_index],pmpp_theo,'bs')
axs[0][0].plot(module_allData_df['Time'].iloc[mpp_index],pmpp_act,'rd')
axs[0][0].legend(['Theoretical','Actual'])
.
.
我的输出是:
推荐阅读
- powershell - 将文件上移一级文件夹
- reactjs - 强制子组件重新渲染(React)
- c# - 将选定的行从 WPF DATAGRID 检索到 DataTable
- mysql - 工作台侧边栏面板中数据库菜单下的表子菜单下拉选项不可用
- java - 找不到记录器的附加程序(com.zaxxer.hikari.HikariConfig)
- python - python在同一行搜索不同的字符串
- angular - 对带有主题的单个 http 请求使用 switchmap
- javascript - 单击自定义按钮时事件的全日历执行顺序
- android - 如何将糖 ORM 数据库作为文件发送到服务器?
- python - Python Pandas - 新的列分组和模式