Plotting a time series?
Asked Answered
P

2

21

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?

Here's the graph

Pesthouse answered 30/4, 2017 at 14:42 Comment(6)
You should, please, show us your code. Otherwise, how can we know what you might be doing incorrectly?Coz
well what I did was just use this newly created df and simply used df.plot(), Resulting graph has no dates i X axis, only a range 0 -> 2000Pesthouse
The fact that you have something called df suggests the use of pandas. Then, in addition to that, you have only two distinct dates. I'm wondering what kind of plot you expect to get, and also exactly what you did.Coz
I did use pandas, and the dataframe is a simple 2 column df, Date column and result column, with values of either 0 or 1. I wanna plot the values by time, for every timestamp.Pesthouse
try df.set_index('Date').plot() or df.plot(x='Date', y='Result'). I guess it's because of the plot use index of df as x-axis. So for your df, it defaults 0->2000. So set the 'Date' columns as index and try againNonjoinder
Is that exactly what the dataframe looks like? Did you format your date column as a date?Neddra
N
49

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

Nonjoinder answered 30/4, 2017 at 15:36 Comment(1)
Bless you. I've been working on this problem forever and this is the first answer I've seen that cuts to the heart of the issue. Now that I know that matplotlib was plotting the index on the x axis, it makes perfect sense. Cheers!Standardbearer
P
0

One common issue is that Date column looks like datetime64 but is actually object. So changing the dtype fixes that issue. N.B. Passing the datetime format= makes the conversion run much, much faster (see this answer for more info).

df['Date'] = pd.to_datetime(df['Date'], format='%Y-%m-%d')
df.plot(x='Date', y='Result', kind='scatter', rot=90)
Petigny answered 2/2, 2023 at 20:41 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.