Matplotlib ignores timezone

Next graph

import matplotlib
f= plt.figure(figsize=(12,4))
ax = f.add_subplot(111)
df.set_index('timestamp')['values'].plot(ax=ax)
ax.xaxis.set_major_locator(matplotlib.dates.HourLocator(interval=1))
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%I'))
plt.show()

      

Displays the hour in the wrong zone (GMT), although I have:

> df['timestamp'][0]
Timestamp('2014-09-02 18:37:00-0400', tz='US/Eastern')

      

In fact, if I comment out the line:

ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%I'))

      

the hour is displayed in the correct time zone.

Why?

+4


source to share


2 answers


If you don't want to change rcParams (which seems like a simple solution), you can pass the timezone to mdates.DateFormatter

.

from dateutil import tz
mdates.DateFormatter('%H:%M', tz=tz.gettz('Europe/Berlin'))

      



The problem is that it mdates.DateFormatter

completely ignores whatever you install, like plot_date

or xaxis_date

or whatever you use.

+5


source


I think you can get the functionality you want by setting the timezone to rcParams

:

import matplotlib
matplotlib.rcParams['timezone'] = 'US/Eastern'

      

The formatter is timezone aware and will convert the timestamp to the rcParams timezone (which is probably UTC), and if you don't format it, it will just accept the timestamp and ignore the timezone. If I understand correctly.



Additional Information:

+4


source







All Articles