简体   繁体   English

Plot 在同一 x 轴上的时间序列的每一年

[英]Plot each year of a time series on the same x-axis

I have a time series with daily data that I want to plot to see how it evolves over a year.我有一个包含每日数据的时间序列,我想要 plot 看看它在一年内是如何演变的。 I want to compare how it evolves over the year compared to previous years.我想比较它在一年中与往年相比的发展情况。 I have written the following code in Python:我在 Python 中编写了以下代码:

xindex = data['biljett'].index.month*30 + data['biljett'].index.day
plt.plot(xindex, data['biljett'])
plt.show()

The graph looks as follows: A graph how the data evolves over a year compared to previous years.该图如下所示: 与前几年相比,数据在一年内的演变情况。 The line is continuous and and does not end with the end of the year which makes it fuzzy.这条线是连续的,并且不会随着年底而结束,这使得它变得模糊。 What am I doing wrong?我究竟做错了什么?

在此处输入图像描述

From technical perspectives, it happens because your data points are not sorted w.r.t.从技术角度来看,这是因为您的数据点未按 W.r.t 排序。 date, thus it goes back and forth to connect data points in the data frame order.日期,因此它来回连接数据帧顺序中的数据点。 you sort the data based on xindex and you're good to go.你根据xindex对数据进行排序,你对 go 很好。 to do that: (first you need to put xindex in data dataframe as a new column)为此:(首先您需要将xindex作为新列放入data dataframe 中)

data.sort_values(by='xindex').reset_index(drop=True)

From the visualization perspective, I think you might have several values per each day count, thus plot is not a good option to begin with.从可视化的角度来看,我认为您每天可能有几个值,因此plot不是一个好的选择。 So IMHO you'd want to try plt.scatter() to visualize your data in a better way.所以恕我直言,您想尝试plt.scatter()以更好的方式可视化您的数据。

I have rewritten as follows:我重写如下:

 xindex = data['biljett'].index.month*30 + data['biljett'].index.day
 data['biljett'].sort_values('xindex').reset_index(drop=True)
 plt.plot(xindex, data['biljett'])
 plt.show()

but gets the following error message: ValueError: No axis named xindex for object type但收到以下错误消息: ValueError: No axis named xindex for object type

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM