durbachit durbachit - 17 days ago 5
Python Question

Can I have only the date from datetime object in tick label matplotlib?

If I have a pandas dataframe such as this:

col1 col2
time
2014-10-01 11:49:32 1 193.75
2014-10-02 10:32:51 2 62.26
2014-10-11 15:19:03 3 96.68
...and so forth


and would like to plot these values for each date.

within the plot, to get the datetime labels, I would use

xticks2 = pd.date_range(start=df.iloc[0,0],end=df.iloc[-1,0], freq='7D', normalize=True)
ax2 = axes2.xaxis.set_minor_locator(mpl.dates.WeekdayLocator(byweekday=range(7), interval=1))
ax2 = plt.xticks(xticks2,xticks2, rotation=0)


But this gives the horrendously long label, i.e. 2014-10-01 11:49:32. Is there a way to achieve a label just like 2014-10-01 (date without time)?

Answer

Try creating a new column just for the date and plot against that.

df['Dates'] = df.time.apply(lambda x: x.date().isoformat())
Comments