Matplotlib忽略时区 [英] Matplotlib ignoring timezone

查看:28
本文介绍了Matplotlib忽略时区的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

下图

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.dates.HourLocator(interval=1))
ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%I'))
plt.show()

在错误的区域 (GMT) 渲染小时,即使我有:

Renders the hour in the wrong zone (GMT) even though I have:

> df['timestamp'][0]
Timestamp('2014-09-02 18:37:00-0400', tz='US/Eastern')

事实上,如果我将这一行注释掉:

As a matter of fact, if I comment out the line:

ax.xaxis.set_major_formatter(matplotlib.dates.DateFormatter('%I'))

小时在正确的时区呈现.

the hour is rendered in the right time zone.

为什么?

推荐答案

我认为您可以通过在 rcParams 中设置时区来获得所需的功能:

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

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

格式化程序会识别时区,并将时间戳转换为rcParams时区(可能是UTC),而如果不格式化,它将仅使用时间戳并忽略时区.如果我理解正确.

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.

更多阅读:

这篇关于Matplotlib忽略时区的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆