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 o开发者_Go百科f 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?
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
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
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?
tz is set to default UTC within matplotlib rcParams. simply use tz from dateutils.tz.tzlocal() to change to local timezone.
精彩评论