How to use Pandas Series to plot two Time Series of different lengths/starting dates?
10,046
I really don't get where you're having problems. I tried to recreate a piece of the dataframe, and it plotted with no problems.
import numpy, matplotlib
data = numpy.array([45,63,83,91,101])
df1 = pd.DataFrame(data, index=pd.date_range('2005-10-09', periods=5, freq='W'), columns=['events'])
df2 = pd.DataFrame(numpy.arange(10,21,2), index=pd.date_range('2003-01-09', periods=6, freq='W'), columns=['events'])
matplotlib.pyplot.plot(df1.index, df1.events)
matplotlib.pyplot.plot(df2.index, df2.events)
matplotlib.pyplot.show()
Using Series instead of Dataframe:
ds1 = pd.Series(data, index=pd.date_range('2005-10-09', periods=5, freq='W'))
ds2 = pd.Series(numpy.arange(10,21,2), index=pd.date_range('2003-01-09', periods=6, freq='W'))
matplotlib.pyplot.plot(ds1)
matplotlib.pyplot.plot(ds2)
matplotlib.pyplot.show()
Author by
JianguoHisiang
Updated on June 05, 2022Comments
-
JianguoHisiang almost 2 years
I am plotting several pandas series objects of "total events per week". The data in the series
events_per_week
looks like this:Datetime 1995-10-09 45 1995-10-16 63 1995-10-23 83 1995-10-30 91 1995-11-06 101 Freq: W-SUN, dtype: int64
My problem is as follows. All pandas series are the same length, i.e. beginning in same year 1995. One array begins in 2003 however.
events_per_week2003
begins in 2003Datetime 2003-09-08 25 2003-09-15 36 2003-09-22 74 2003-09-29 25 2003-09-05 193 Freq: W-SUN, dtype: int64 import matplotlib.pyplot as plt fig = plt.figure(figsize=(20,5)) ax = plt.subplot(111) plt.plot(events_per_week) plt.plot(events_per_week2003)
I get the following value error.
ValueError: setting an array element with a sequence.
How can I do this?