python - Matplotlib 忽略时区

标签 python 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) 呈现小时,即使我有:

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

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

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

小时在正确的时区呈现。

为什么?

最佳答案

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

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

格式化程序是时区感知的,会将时间戳转换为您的 rcParams 时区(可能是 UTC),而如果您不格式化它,它只会采用时间戳并忽略时区。如果我没理解错的话。

更多阅读:

关于python - Matplotlib 忽略时区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25654944/

相关文章:

python - 使用后缀数组查找子字符串的一次出现

Python 类型在方法中暗示自己的类

python - 在 Python 中,即使使用 rawstring 或 unicode 设置, '\' 也会转换为 '\\'

python - OpenCV BGR 和 PyPlot RGB

python - seaborn 热图中的轮廓 (iso-z) 或阈值线

python - 如何在 Anaconda 中使用 Python 的框架构建

python - 如何在 python 中优化它?

python - 如何将列表python计算成矩阵相似度

python - 有没有办法在 matplotlib 中制作多个水平箱线图?

python-3.x - 如何将最高的 matplotlib 散点图 c 值发送到前面?