Pandas Datetime Timeseries calculation in AM / PM format

I have a pandas series with Timestamp indices that I would like to build.

print example.head() 2015-08-11 20:07:00-05:00 26 2015-08-11 20:08:00-05:00 66 2015-08-11 20:09:00-05:00 71 2015-08-11 20:10:00-05:00 63 2015-08-11 20:11:00-05:00 73 

But when I draw it in pandas with:

 plt.figure(figsize = (15,8)) cubs1m.plot(kind='area') 

I would like the y-axis values ​​to be displayed in AM / PM (8:08 PM) format, and not in wartime (20:08). Is there an easy way to do this?

And also, how can I control # ticks and # tags built with pandas?

Thanks in advance.

+4
source share
1 answer

There are two elements in your question:

  • How to control # ticks / tags on the chart
  • How to change a 24-hour time to a 12-hour period

The axes of the set_xticks , set_yticks , set_xticklabels and set_yticklabels control set_yticklabels and labels:

 import matplotlib.pyplot as plt plt.plot(range(10), range(10)) plt.gca().set_xticks(range(0,10,2)) plt.gca().set_xticklabels(['a{}'.format(ii) for ii in range(0,10,2)]) 

To change the time format, use pd.datetime.strftime: How can I convert 24-hour time to 12 hours?

 import pandas as pd data = pd.Series(range(12), index=pd.date_range('2016-2-3 9:00','2016-2-3 20:00', freq='H')) ax = data.plot(xticks=data.index[::2]) ax.set_xticklabels(data.index[::2].map(lambda x: pd.datetime.strftime(x, '%I %p'))) 

This question covers an alternative approach to building with dates: Pandas timeline that sets major and minor ticks and labels along the x axis

+1
source

All Articles