如何使用 Pandas Series 绘制两个不同长度/起始日期的时间序列? [英] How to use Pandas Series to plot two Time Series of different lengths/starting dates?
本文介绍了如何使用 Pandas Series 绘制两个不同长度/起始日期的时间序列?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!
问题描述
我正在绘制几个每周事件总数"的熊猫系列对象. events_per_week
系列中的数据如下:
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
我的问题如下.所有熊猫系列的长度均相同,即从1995年开始.但是,其中一个阵列于2003年开始. events_per_week2003
从2003年开始
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 2003
Datetime
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)
我得到以下值错误.
ValueError: setting an array element with a sequence.
我该怎么做?
推荐答案
我真的不了解您遇到的问题.我试图重新创建数据框的一部分,并且绘制它没有问题.
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()
这篇关于如何使用 Pandas Series 绘制两个不同长度/起始日期的时间序列?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文