Для моего исследования я хочу иметь возможность быстро создавать несколько графиков определенного вида, но с немного разными данными (например, с разными датами или разными датчиками). Я пытаюсь написать функцию, которая создает график, используя несколько обязательных аргументов и до 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 или более необязательных аргументов?