Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plotting a time series?

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:

    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.

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

like image 315
RedaBitar Avatar asked Apr 30 '17 14:04

RedaBitar


People also ask

What is time series plot with example?

Example: Deaths by Horsekicks The time series plot above of "Deaths by horsekick in Prussian cavalry corps, 1875-94" displays the number of annual deaths in each of these 20 years. In other words, one quantitative variable is examined over time.

Which plot is best for time series data?

Scatter. The Scatter view uses a scatter plot to display time series data. A scatter plot can have anything on the horizontal axis, in any transformation, and points are not connected or ordered. The scatter visualization maps each data point to X and Y coordinates.

What graph do you use for time series?

A line graph is the simplest way to represent time series data.

How do you plot a time series graph in Python?

In X-axis we should have a variable of DateTime. In Y-axis we can have the variable which we want to analyze with respect to time. plt. plot() method is used to plot the graph in matplotlib.


1 Answers

Please use

df.set_index('Date').plot()

or

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.

see more at pandas.DataFrame.plot

like image 131
heyu91 Avatar answered Sep 21 '22 08:09

heyu91