1

I have the following code that iterates through a data frame row and grabs a start timestamp and an end timestamp from two columns, creating a "time series range", and then plots that time series range from another dataframe of timeseries data. And so, I iterate through each time series range and create the corresponding plot from a for loop. I am using python. Here is my code:

from datetime import datetime
import pandas as pd
import numpy as np
from functools import reduce
pd.options.mode.chained_assignment = None  # default='warn'
#Ignore warning messages
from matplotlib import pyplot as plt
from matplotlib.pyplot import figure
import matplotlib as mpl
from datetime import datetime, timedelta
import warnings
warnings.filterwarnings("ignore")

for index, row in log_df.iterrows():
    print('Range ID: ' + str(row['cycle_ID']))

    start_time_string = row['Start_time']
    end_time_string = row['End_time']
    
    start_time = datetime.strptime(start_time_string, '%Y-%m-%d %H:%M:%S')
    end_time = datetime.strptime(end_time_string, '%Y-%m-%d %H:%M:%S')

    print('Start timestamp: ' + str(start_time))
    print('End timestamp: ' + str(end_time))

    mask = (data_df['timestamp'] > start_time) & (data_df['timestamp'] <= end_time)
    df_masked = data_df.loc[mask]  
    df_masked
    
    plt.rcParams['figure.figsize'] = [20, 10]
    df_masked.plot('localminute' , 'values', legend=False)
    plt.xlabel("Timestamp", fontsize=14)
    plt.ylabel("Watts", fontsize=14)
    plt.show()

This works, however, the problem is that the first plot that gets produced is not formatted with my intended plot size, while all the next plots are. And so the first plot is small, while every plot proceeding is correctly sized. I am confused why this is not working, since I would think my figure size parameter would be applied to each plot within the loop. How can I correctly set my figure size parameter so that every plot is formatted correctly?

  • Try moving the call to `plt.rcParams()` after `df_masked.plot()`, like the label calls. – Barmar Dec 13 '22 at 18:27
  • I tried this, but unfortunately the issue remains, the first plot in the loop is not the specified figure size, while every other plot after is. – LostinSpatialAnalysis Dec 13 '22 at 18:56
  • 1
    I suggest not messing with the global rcParams. Just set the figsize in the [DataFrame.plot](https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.plot.html) call: `df_masked.plot('localminute' , 'clotheswasher1', legend=False, figsize=(20, 10))` – tdy Dec 13 '22 at 18:58
  • This [answer](https://stackoverflow.com/a/39770939/7758804) of the duplicate. – Trenton McKinney Dec 13 '22 at 22:19

1 Answers1

0

You invoked in this sequence:

    plt.rcParams['figure.figsize'] = ...
    df_masked.plot( ... )

Prefer this order:

    df_masked.plot( ... )
    plt.rcParams['figure.figsize'] = ...

Also, ignoring chained_assignment warnings is ill advised. Go to the trouble of understanding what is happening, and fix the root cause. The warning is there for good reason: to save you endless grief caused by hard-to-diagnose bugs.

J_H
  • 17,926
  • 4
  • 24
  • 44
  • I just changed the order of those lines to your suggested order, and I still see that the first plot is small (default sized), while all of the next plots in the loop are much larger, to my specifications. Could there perhaps be another cause of this? – LostinSpatialAnalysis Dec 13 '22 at 18:47
  • Chage the figsize after the x/y-labels? IDK. – J_H Dec 13 '22 at 18:48