Python 绘制时间序列?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/43707620/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Plotting a time series?
提问by RedaBitar
I'm really new to using python as a data analysis tool, and it's my first time ever dealing with time series. I have a data set which has dates in the first column, and a "result" integer which is either 1 or 0. The date column was successfully converted to a time object. I tried to plot the values directly using matplotlib's plot function, but that did not work.. Sample:
我对使用 python 作为数据分析工具真的很陌生,这是我第一次处理时间序列。我有一个数据集,它的第一列中有日期,还有一个“结果”整数,它要么是 1,要么是 0。日期列已成功转换为时间对象。我尝试使用 matplotlib 的 plot 函数直接绘制值,但这不起作用.. 示例:
Date Result
2017-01-06 0.0
2017-01-06 1.0
2017-01-06 0.0
2017-01-07 0.0
2017-01-07 0.0
I tried using df.plot(), but the resulting plot has very undesirable results.
我尝试使用 df.plot(),但结果图有非常不理想的结果。
What I want at the end of the day is dates on the x axis, and the "result" on the y axis. Where am I going wrong? What's wrong with what I'm doing? EDIT: Here's the graph
我最终想要的是 x 轴上的日期和 y 轴上的“结果”。我哪里错了?我在做什么有什么问题?编辑:这是图表
回答by heyu91
Please use
请用
df.set_index('Date').plot()
df.set_index('Date').plot()
or
或者
df.plot(x='Date', y='Result')
df.plot(x='Date', y='Result')
because of the plot by default use index of df
as the x-axis, so you should set the 'Date' column as the index, or specify which column to use as the x-axis.
由于绘图默认使用索引df
作为 x 轴,因此您应该将 'Date' 列设置为索引,或者指定使用哪一列作为 x 轴。
see more at pandas.DataFrame.plot