编程语言
首页 > 编程语言> > python-如何使用熊猫系列绘制两个不同长度/开始日期的时间序列?

python-如何使用熊猫系列绘制两个不同长度/开始日期的时间序列?

作者:互联网

我正在绘制几个“每周事件总数”的熊猫系列对象.系列events_per_week中的数据如下所示:

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年开始

 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.

我怎样才能做到这一点?

解决方法:

我真的不了解您遇到的问题.
我试图重新创建数据框的一部分,并且没有问题.

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()

使用系列而不是数据框:

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,matplotlib,time-series,python
来源: https://codeday.me/bug/20191119/2034857.html