Skip to content Skip to sidebar Skip to footer

Matplotlib Ignoring Timezone

The following plot 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.da

Solution 1:

I think you can have the desired functionality by setting the timezone in rcParams:

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

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

More reading:

Solution 2:

If you don't want to change rcParams (which seems to be an easy fix), then 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 mdates.DateFormatter completely ignores everything you set in like plot_date or xaxis_date or whatever you use.

Solution 3:

Using pytz

import pytz
import matplotlib.dates as mdatesmy_tz= pytz.timezone('Europe/Berlin')
formatter = mdates.DateFormatter('%m-%d %H:%M', tz=my_tz)

Post a Comment for "Matplotlib Ignoring Timezone"