14

I'm using matplotlib to plot data against timestamps in python 2.6. I've found the plot_date() function and it works great! However, the times are all in UTC. I read the docs which says you can pass the tz parameter to the function to set a custom timezome. This does not seem to be working. Even after passing the parameter, times remain in UTC. I've tried a few methods as well to no avail.

Notes: I'm importing a CSV file filled with epoch times to generate the X-axis. So, I'm converting from epoch to matplotlib format using epoch2num().

Sample Source:

import matplotlib.pyplot as plt
import matplotlib.mlab as mlab
import matplotlib.dates as mdates
from pytz import timezone

...

dates = mdates.epoch2num(dates) # Converting my list of dates from epochs
plt.plot_date(dates, data,fmt='-',tz="US/Eastern",xdate=True)

That code fails to produce correct timestamps along the axis. It graphs just fine though.

Also tried:

plt.plot_date(dates,data,fmt='-',tz=timezone('US/Eastern'),xdate=True)

Same result as the first example. Everything remains in UTC.

For grins I ran a num2date() on the 'dates' list and printed the output. The values output were in UTC. I then added the tz=timezone('US/Eastern') to the num2date() call and the values output were in Eastern, as they should be.

I also tried to "preconvert" the dates to Eastern before calling plot_date, and then omitting the tz= keyword in plot_date, but still the values are UTC.

Finally, I edited my matplotlibrc file and changed #timezone UTC to #timezone US/Eastern and still no success.

What am I missing to get these dates to graph outside of UTC?

4 Answers 4

5

I'm seeing this behavior in Matplotlib 1.0.1 too.

I looked at it in the debugger and IMHO the argument tz is ignored. The bug is in matplotlib.axes.Axes.xaxis_date (file "axes.py", line 2679).

I filed a bug report for it: https://sourceforge.net/tracker/index.php?func=detail&aid=3176823&group_id=80706&atid=560720

1
4

To get the dates labeled with EST, you could do this:

import matplotlib.pyplot as plt
import pytz
import numpy as np
import datetime
import time

est=pytz.timezone('US/Eastern')
n=20
duration=1000
now=time.mktime(time.localtime())
timestamps=np.linspace(now,now+duration,n)
# You could build timezone-aware datetime objects this way:
dates=[datetime.datetime.fromtimestamp(ts,est) for ts in timestamps]
# or use timezone-naive datetime objects using `utcfromtimestamp` below.
# plt.plot_date interprets naive datetime objects to be in the UTC timezone.
# dates=[datetime.datetime.utcfromtimestamp(ts) for ts in timestamps]    
values=np.cumsum(np.random.random(n) - 0.5)
plt.xticks(rotation=25)
plt.plot_date(dates,values,tz=est,linestyle='dashed')
plt.show()

yields

alt text

4
  • 2
    That's more or less what I've tried. I'm wondering if it's some configuration problem on my sytem. I've commented out all the other code aside from reading the CSV file, putting this in, like in your example: dates =[datetime.datetime.fromtimestamp(ts,mst) for ts in dates], and also adding a timezone variable (mst). Still it's showing in UTC. Edit: Ah..Yeah, even your example fails on my system. I copy-pasted it into its own module and ran it.
    – Scott
    Dec 20, 2010 at 0:59
  • @Scott: Maybe if you post what version of Python and matplotlib you are using, (and perhaps also your OS), others with similar systems can try the code and report if they can reproduce the problem.
    – unutbu
    Dec 20, 2010 at 1:17
  • Windows 7 x64 Python 2.6 32bit Matplotlib 1.0.0 for Python 2.6 32bit
    – Scott
    Dec 20, 2010 at 1:20
  • This should work but does not because of this bug... Jun 8, 2016 at 12:09
3

I uninstalled Matplotlib 1.0.0 and installed 0.99.3 instead. After doing this, I am now able to properly set the timezone and see the effects. I tested with the sample provided by Unutbu and then with my own code. Everything works as it should.

Perhaps this is a bug in 1.0.0 then?

1

tz is set to default UTC within matplotlib rcParams. simply use tz from dateutils.tz.tzlocal() to change to local timezone.

1
  • plot_date() didn't work for me, this did: matplotlib.rcParams.update({'timezone': mytz}). Thanks!
    – AstroFloyd
    Feb 12, 2023 at 17:01

Your Answer

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

Not the answer you're looking for? Browse other questions tagged or ask your own question.