简体   繁体   English

Python:带多个可选参数的Graph函数

[英]Python: Graph function that takes multiple optional arguments

For my research I want to be able to quickly produce multiple graphs of a particular kind, but with slightly different data (eg different dates or different sensors). 对于我的研究,我希望能够快速生成特定种类的多个图形,但数据略有不同(例如,不同的日期或不同的传感器)。 I'm trying to write a function that produces a graph using a few mandatory arguments and up to 20 optional arguments. 我正在尝试编写一个函数,该函数使用一些强制性参数和最多20个可选参数来生成图形。 I want this function to: 1) be able to produce a nice graph when I give it just one sensor as well as when I give it 10 sensors. 我希望此函数能够:1)当我给它一个传感器以及给它10个传感器时,能够生成一个漂亮的图形。 2) Show only the desired time between starttime and endtime 2)仅显示开始时间和结束时间之间的所需时间
The code I have so far is: 到目前为止,我的代码是:

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

This produces the desired results if I give it 1 sensor. 如果我给它1个传感器,这将产生预期的结果。 For more sensors, I create a new function. 对于更多传感器,我创建了一个新功能。 So now I have defined 10 functions, with this being number 10: 所以现在我定义了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

Now my question is: How do I make this into one function that can take 20 or more optional arguments? 现在我的问题是:如何使它成为一个可以接受20个或更多可选参数的函数?

Probably the easiest way is to just pass a list of temp - label pairs. 可能最简单的方法是仅传递一个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)

You could use an *args parameter (you can call it *temps or whatever - the * is the important part). 您可以使用*args参数(您可以将其称为*temps或其他名称- *是重要部分)。 A parameter with * accepts an arbitrary number of positional arguments and you can loop over them as a list. *的参数接受任意数量的位置参数,您可以将它们作为列表循环。 Since you also need a label for each, you could expect each parameter to be a tuple (value, "label") . 由于还需要为每个参数添加标签,因此可以期望每个参数都是一个元组(value, "label")

For example, within your function, instead of having to define a function for each possible amount of parameters: 例如,在您的函数中,不必为每种可能的参数数量定义函数:

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])
    # ...

Or you could of course do the same with a keyword argument **temps accepting a list of tuples, if you want to call the function by explicitly specifying where the temps are given. 或者,如果您想通过显式指定给定温度的位置来调用函数,则当然可以对关键字参数**temps接受元组列表进行相同的操作。

I found the answer to the slicing/truncate problem. 我找到了切片/截断问题的答案。 I input the whole dataframe (which has all sensors) into the function. 我将整个数据框(包含所有传感器)输入到函数中。 The whole frame is then sliced locally. 然后将整个帧局部切片。 Columns with sensors are then retrieved as a string. 然后将带有传感器的列作为字符串检索。

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

Thanks for the help! 谢谢您的帮助!

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM