Python: функция Graph, которая принимает несколько необязательных аргументов - PullRequest
0 голосов
/ 01 мая 2018

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

Ответы [ 3 ]

0 голосов
/ 01 мая 2018

Вы можете использовать параметр *args (вы можете назвать его *temps или как угодно - * является важной частью). Параметр с * принимает произвольное количество позиционных аргументов, и вы можете перебирать их в виде списка. Поскольку для каждого из них также требуется метка, можно ожидать, что каждый параметр будет кортежем (value, "label").

Например, в вашей функции вместо необходимости определять функцию для каждого возможного количества параметров:

def multiple_days(startdatetime, enddatetime, index, *temps, windowtitle = "Temperatures over time'):
    # ...
    for temp in temps:
        ax1.plot(frame.index, frame.temp[0], label = temp[1])
    # ...

Или вы, конечно, можете сделать то же самое с аргументом ключевого слова **temps, принимая список кортежей, если вы хотите вызвать функцию, явно указав, где заданы временные значения.

0 голосов
/ 02 мая 2018

Я нашел ответ на проблему среза / усечения. Я вводю весь фрейм данных (в котором есть все датчики) в функцию. Весь кадр затем нарезается локально. Столбцы с датчиками затем извлекаются в виде строки.

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')

Спасибо за помощь!

0 голосов
/ 01 мая 2018

Вероятно, самый простой способ - просто передать список из 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)
...