Reputation: 343
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?
Upvotes: 22
Views: 63688
Reputation: 23021
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)
Upvotes: 0
Reputation: 1274
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
Upvotes: 51