【发布时间】:2017-09-20 11:25:30
【问题描述】:
问题是绘制一条日期分布不均匀的直线。使用系列值数据可以解决曲线问题,但会丢失时间线(日期)。有没有办法解决这个问题?
编辑:为什么日期不直接映射到 x 轴上的刻度:
0 -> 2017-02-17,
1 -> 2017-02-20,
... ?
现在橙色线似乎有 12 个刻度,但只有 8 个数据点。
import pandas as pd
import matplotlib.pyplot as plt
def straight_line(index):
y = [3 + 2*x for x in range(len(index))]
zserie = pd.Series(y, index=index)
return zserie
if __name__ == '__main__':
start = '2017-02-10'
end = '2017-02-17'
index = pd.date_range(start,end)
index1 = pd.DatetimeIndex(['2017-02-17', '2017-02-20', '2017-02-21', '2017-02-22',
'2017-02-23', '2017-02-24', '2017-02-27', '2017-02-28',],
dtype='datetime64[ns]', name='pvm', freq=None)
plt.figure(1, figsize=(8, 4))
zs = straight_line(index)
zs.plot()
zs = straight_line(index1)
zs.plot()
plt.figure(2, figsize=(8, 4))
zs = straight_line(index1)
plt.plot(zs.values)
【问题讨论】:
-
您是要创建一条日期间距不均匀的直线(x 轴),还是要让日期值表现得像一个分类值?
-
第一个情节中的橙色线似乎正是您正在寻找的情节。它有什么问题?
-
要获得一条直线,您必须以与 y 轴相同的速率调整 x 轴。这实际上不适用于针对彼此不同距离的日期进行绘图。为什么数据需要图表在一条直线上?
-
哦,我应该解释一下,最初的问题是将带有回归线的时间序列绘制成带有日期的同一图形的常见任务。回归线应该是直的。
-
为什么日期不直接映射到数据点,例如:
标签: python pandas matplotlib plot time-series