Python:接受多个可选参数的图形函数
对于我的研究,我希望能够快速生成特定类型的多个图表,但数据略有不同,例如不同的日期或不同的传感器。我正在尝试编写一个函数,该函数使用几个强制参数和最多20个可选参数生成一个图形。我希望这个函数:当我只给它一个传感器时,以及当我给它10个传感器时,能够生成一个漂亮的图形。2仅显示starttime和endtime之间的所需时间 到目前为止,我掌握的代码是:Python:接受多个可选参数的图形函数,python,python-3.x,matplotlib,optional-arguments,Python,Python 3.x,Matplotlib,Optional Arguments,对于我的研究,我希望能够快速生成特定类型的多个图表,但数据略有不同,例如不同的日期或不同的传感器。我正在尝试编写一个函数,该函数使用几个强制参数和最多20个可选参数生成一个图形。我希望这个函数:当我只给它一个传感器时,以及当我给它10个传感器时,能够生成一个漂亮的图形。2仅显示starttime和endtime之间的所需时间 到目前为止,我掌握的代码是: import numpy as np import pvlib as pvl import pandas as pd import matpl
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
如果我给它一个传感器,它会产生期望的结果。对于更多传感器,我创建了一个新功能。现在我定义了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个或更多可选参数的函数 最简单的方法可能是传递一个临时标签对列表 你可以使用一个*args参数,你可以称它为*temps或者其他任何东西,*是最重要的部分。带有*的参数接受任意数量的位置参数,您可以将其作为列表循环。由于每个参数还需要一个标签,所以可以预期每个参数都是一个元组值label 例如,在函数中,不必为每个可能的参数量定义函数: def multiple_daysstartdatetime、enddatetime、index、*temps、windowtitle=随时间变化的温度’: ... 对于temp中的temp: ax1.plotframe.index,frame.temp[0],label=temp[1] ...
如果您想通过显式指定temp的位置来调用函数,当然也可以对接受元组列表的关键字参数**temp执行相同的操作。我找到了切片/截断问题的答案。我将包含所有传感器的整个数据帧输入到函数中。然后对整个帧进行局部切片。然后将带有传感器的列作为字符串检索
def temp_multiple_days(df, startdatetime, enddatetime, *temps, windowtitle = 'Temperatures over time'):
df = df.truncate(startdatetime, enddatetime)
fig, ax1 = plt.subplots()
fig.canvas.set_window_title(windowtitle)
for (temp, label) in temps:
ax1.plot(df.index, df[temp], label = label)
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
# i then call to the function in this way:
temp_multiple_days(df, '2018-04-21 00:00:00', '2018-04-27 23:59:59', ('name of sensor 1', 'graph label 1'), ('name of sensor 2', 'graph label 2'), windowtitle= 'A nice title')
谢谢你的帮助 可能的重复可能的重复我正在尝试让它工作,但我不知道如何格式化它,以便正确地对本地数据帧进行切片。我对pandas或matplotlib还没有太多经验,但我发现了这个,我想知道它是否解决了您的问题:
def temp_multiple_days(df, startdatetime, enddatetime, *temps, windowtitle = 'Temperatures over time'):
df = df.truncate(startdatetime, enddatetime)
fig, ax1 = plt.subplots()
fig.canvas.set_window_title(windowtitle)
for (temp, label) in temps:
ax1.plot(df.index, df[temp], label = label)
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
# i then call to the function in this way:
temp_multiple_days(df, '2018-04-21 00:00:00', '2018-04-27 23:59:59', ('name of sensor 1', 'graph label 1'), ('name of sensor 2', 'graph label 2'), windowtitle= 'A nice title')