python - Python:接受多个可选参数的图形函数
问题描述
对于我的研究,我希望能够快速生成多个特定类型的图表,但数据略有不同(例如不同的日期或不同的传感器)。我正在尝试编写一个函数,该函数使用一些强制参数和最多 20 个可选参数来生成图形。我希望这个功能:1)当我只给它一个传感器以及给它10个传感器时,能够产生一个漂亮的图表。2)仅显示开始时间和结束时间之间的所需时间
到目前为止我的代码是:
import numpy as np
import pvlib as pvl
import pandas as pd
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
def temp1_multiple_days(startdatetime, enddatetime, index, temp1, label1, windowtitle = 'Temperatures over time'):
d = {'index':index, 'temp1': temp1}
frame = pd.DataFrame(data = d)
frame.set_index([index], inplace = True)
frame = frame[startdatetime:enddatetime] #slicing right dates out of large dataset
fig, ax1 = plt.subplots()
fig.canvas.set_window_title(windowtitle)
ax1.plot(frame.index, frame.temp1, label = label1)
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%d-%b-'%y"))
ax1.xaxis.set_major_locator(mdates.DayLocator())
ax1.set_xlim(startdatetime, enddatetime)
ax1.set_ylabel('Temperature (°C)')
ax1.legend(loc=1)
fig.tight_layout
fig.autofmt_xdate()
plt.grid(True)
plt.show
如果我给它 1 个传感器,这会产生所需的结果。对于更多传感器,我创建了一个新函数。所以现在我已经定义了 10 个函数,其中第 10 个是:
def temp10_multiple_days(startdatetime, enddatetime, index, temp1, label1, temp2, label2, temp3, label3, temp4, label4, temp5, label5, temp6, label6, temp7, label7, temp8, label8, temp9, label9, temp10, label10, windowtitle = 'Temperatures over time'):
d = {'index':index, 'temp1': temp1, 'temp2': temp2, 'temp3': temp3, 'temp4': temp4, 'temp5': temp5, 'temp6': temp6, 'temp7': temp7, 'temp8': temp8, 'temp9': temp9, 'temp10': temp10}
frame = pd.DataFrame(data = d)
frame.set_index([index], inplace = True)
frame = frame[startdatetime:enddatetime] #slicing right dates out of large dataset
fig, ax1 = plt.subplots()
fig.canvas.set_window_title(windowtitle)
ax1.plot(frame.index, frame.temp1, label = label1)
ax1.plot(frame.index, frame.temp2, label = label2)
ax1.plot(frame.index, frame.temp3, label = label3)
ax1.plot(frame.index, frame.temp4, label = label4)
ax1.plot(frame.index, frame.temp5, label = label5)
ax1.plot(frame.index, frame.temp6, label = label6)
ax1.plot(frame.index, frame.temp7, label = label7)
ax1.plot(frame.index, frame.temp8, label = label8)
ax1.plot(frame.index, frame.temp9, label = label9)
ax1.plot(frame.index, frame.temp10, label = label10)
ax1.xaxis.set_major_formatter(mdates.DateFormatter("%d-%b-'%y"))
ax1.xaxis.set_major_locator(mdates.DayLocator())
ax1.set_xlim(startdatetime, enddatetime)
ax1.set_ylabel('Temperature (°C)')
ax1.legend(loc=1)
fig.tight_layout
fig.autofmt_xdate()
plt.grid(True)
plt.show
现在我的问题是:如何将它变成一个可以接受 20 个或更多可选参数的函数?
解决方案
可能最简单的方法是只传递temp
-label
对的列表。
def temp10_multiple_days(startdatetime, enddatetime, index, temp_label_pairs,
windowtitle = 'Temperatures over time'):
for (temp, label) in temp_label_pairs:
ax.plot(frame.index, temp, label)
推荐阅读
- node.js - 我可以用 Node.js Mongoose 填充整个目录吗?
- python-3.x - read_table 处理缺失的字符串
- c# - 使用 foreach 迭代相似记录
- for-loop - Scilab - 具有变化索引的 For 循环
- c# - 找不到类型库“Microsoft.Office.Interop.Access.Dao”的包装程序集
- cordova - Cordova 停止从钩子构建
- java - 使用spring esTemplate删除文档中的字段
- python - Scrapy 如何使用 itemloader 填充空项目?
- asp.net-core - 如何在类库项目中使用 AspNetCore 3.0 功能/组件
- html - 应用 type=password 时文本框失去 CSS 样式