2

I am doing a barplot out of a dataframe with a 15min datetimeindex over a couple of years. Using this code:

df_Vol.resample(
    'A',how='sum'
).plot.bar(
    title='Sums per year',
    style='ggplot',
    alpha=0.8
)

Unfortunately the ticks on the X-axis are now shown with the full timestamp like this: 2009-12-31 00:00:00.

I would prefer to Keep the code for plotting short, but I couldn't find an easy way to format the timestamp simply to the year (2009...2016) for the plot.

Can someone help on this?

2
  • see related: stackoverflow.com/questions/34207409/… Commented Aug 22, 2016 at 13:51
  • Thanks EdChum, I was hoping there would be a Parameter like ('%Y') to Format this in the df.plot() command. Commented Aug 22, 2016 at 14:50

2 Answers 2

4

As it does not seem to be possible to Format the date within the Pandas df.plot(), I have decided to create a new dataframe and plot from it.

The solution below worked for me:

df_Vol_new = df_Vol.resample('A',how='sum')
df_Vol_new.index = df_Vol_new.index.format(formatter=lambda x: x.strftime('%Y')) 
ax2 =df_Vol_new.plot.bar(title='Sums per year',stacked=True, style='ggplot', alpha=0.8)
Sign up to request clarification or add additional context in comments.

Comments

1

I figured an alternative (better, at least to me) way is to add the following to df_Vol_new.plot() command:

plt.legend(df_Vol_new.index.to_period('A'))

This way you would reserve df_Vol_new.index datetime format while getting better plots at the same time.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.