Matplotlibplot_date 即使使用自定义时区也能保持 UTC 时间

发布于 2024-10-08 13:08:09 字数 1057 浏览 2 评论 0原文

我正在使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

旧街凉风 2024-10-15 13:08:09

我也在 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 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

七堇年 2024-10-15 13:08:09

要获取标有 EST 的日期,您可以执行以下操作:

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

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

樱花坊 2024-10-15 13:08:09

我卸载了 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?

初雪 2024-10-15 13:08:09

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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文