Search code examples
pythonpython-3.xmatplotliboptional-arguments

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 (e.g. 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. 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. 2) Show only the desired time between starttime and endtime
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. For more sensors, I create a new function. So now I have defined 10 functions, with this being number 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?


Solution

  • 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!