Matplotlibplot_date 即使使用自定义时区也能保持 UTC 时间
我正在使用 matplotlib 根据 python 2.6 中的时间戳绘制数据。我找到了plot_date()函数并且它工作得很好!但是,时间均为 UTC。我阅读了文档,其中说您可以将 tz 参数传递给函数来设置自定义时区。这似乎不起作用。即使传递参数后,时间仍保留 UTC 格式。我也尝试过一些方法,但都没有效果。
笔记: 我正在导入一个充满纪元时间的 CSV 文件来生成 X 轴。因此,我使用 epoch2num() 从 epoch 格式转换为 matplotlib 格式。
示例来源:
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)
该代码无法沿轴生成正确的时间戳。不过它的图表效果很好。
还尝试过:
plt.plot_date(dates,data,fmt='-',tz=timezone('US/Eastern'),xdate=True)
与第一个示例相同的结果。一切都保留在 UTC 中。
为了一笑,我在“日期”列表上运行了 num2date() 并打印了输出。输出值采用 UTC 格式。然后,我将 tz=timezone('US/Eastern') 添加到 num2date() 调用中,并且输出的值是东部的,因为它们应该是这样。
我还尝试在调用plot_date之前将日期“预转换”为东部日期,然后在plot_date中省略tz=关键字,但值仍然是UTC。
最后,我编辑了 matplotlibrc 文件并将 #timezone UTC 更改为 #timezone US/Eastern 但仍然没有成功。
为了在 UTC 之外绘制这些日期,我缺少什么?
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?
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
我也在 Matplotlib 1.0.1 中看到了这种行为。
我在调试器中查看了它,恕我直言,参数 tz 被忽略。该错误位于 matplotlib.axes.Axes.xaxis_date 中(文件“axes.py”,第 2679 行)。
我为此提交了一份错误报告:
https://sourceforge.net/tracker/index.php? func=detail&aid=3176823&group_id=80706&atid=560720
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 inmatplotlib.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
要获取标有
EST
的日期,您可以执行以下操作:yields
To get the dates labeled with
EST
, you could do this:yields
我卸载了 Matplotlib 1.0.0 并安装了 0.99.3。完成此操作后,我现在可以正确设置时区并查看效果。我使用 Unutbu 提供的示例进行了测试,然后使用我自己的代码进行了测试。一切都按其应有的方式进行。
也许这是 1.0.0 中的一个错误?
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 在 matplotlib rcParams 中设置为默认 UTC。
只需使用 dateutils.tz.tzlocal() 中的 tz 即可更改为本地时区。
tz is set to default UTC within matplotlib rcParams.
simply use tz from dateutils.tz.tzlocal() to change to local timezone.