Monte Cristo
Monte Cristo

Reputation: 131

pandas plot timeseries for overlapping hours

I am trying to do a visual analysis of load variation. I've read in the data from CSV files, converted the index to datetime format, which works great. The data is per 5 minutes for a whole year, with the daytime (currently selected as 07:00-18:00) hours of interest.

I can plot the timeseries data sequentially using the selected time (pandas timeseries between_datetime function?), which is one of the questions I've tried to solve.

What I'd like to do now: plot the daytime data with a fixed (daytime) x axis, so that the data per day is shown to run between 07:00 and 18:00. Each day of the year (or month) should then be a separate line on the graph.

Example of my code for the sequential timeseries plot:

import pandas as pd
import matplotlib.pyplot as plt
import matplotlib

Y13 = pd.read_csv(path + "Y13.csv")
Y13.index = pd.to_datetime(Y13.Datetime)
Y14 = pd.read_csv(path + "Y14.csv")
Y14.index = pd.to_datetime(Y14.Datetime)

fig, axes = plt.subplots(nrows=2, ncols=1, sharex=True)
Y13.LOAD.between_time('07:00','18:00').plot(ax=axes[0])
Y14.LOAD.between_time('07:00','18:00').plot(ax=axes[0])

Y13.LOAD.between_time('07:00','18:00').diff().plot(ax=axes[1])
Y14.LOAD.between_time('07:00','18:00').diff().plot(ax=axes[1])

I suppose that I could plot this using a loop, but a more efficient/pythonic/pandas method would be great.

Am I wrong in having the data as a timeseries to plot, or are there any other large gaps to address?

Thanks

Upvotes: 3

Views: 1844

Answers (1)

Kapil Sharma
Kapil Sharma

Reputation: 1432

pandas.plot has an argument subplots which might be what you need. Hope this helps.

Pandas Visualization - Subplots

In[1]: import pandas as pd

In[2]: import numpy as np

In[3]: datapoints = 365 * 24 * 12

In[4]: data = pd.DataFrame({"Data": np.random.randn(datapoints)}, index=pd.date_range(start="2015-01-01", freq="5min", periods=datapoints))

In[5]: data = data.between_time("07:00", "18:00")

In[6]: data["Date"] = data.index.date

In[7]: data["Time"]  = data.index.time

In[8]: data[:4]
Out[8]: 
                         Data        Date      Time
2015-01-01 07:00:00  0.890659  2015-01-01  07:00:00
2015-01-01 07:05:00 -0.643869  2015-01-01  07:05:00
2015-01-01 07:10:00  0.267501  2015-01-01  07:10:00
2015-01-01 07:15:00  0.627690  2015-01-01  07:15:00

In[9]: data = data.pivot(index="Time", columns="Date", values="Data")

In[10]: data.iloc[:, 0:4].plot(subplots=True)
Out[10]: 
array([<matplotlib.axes._subplots.AxesSubplot object at 0x1213316d0>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x1214b3e90>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x120fa7350>,
       <matplotlib.axes._subplots.AxesSubplot object at 0x11eb6bf10>], dtype=object)

Chart

Upvotes: 4

Related Questions