为什么时区对话不影响在 matplotlib 中绘制日期时间 objects 的数字?

Why time zone conversation doesn't affect the figure in plotting datetime objects in matplotlib?

我正在尝试绘制一个数字,其中没有时间的日期在 x-axis 中,没有日期的时间在 y-axis 中:

import datetime as dt
import matplotlib.pyplot as plt
import matplotlib.dates as mdates


dates = [dt.datetime(2020, 8, 11),
         dt.datetime(2020, 8,  9),
         dt.datetime(2020, 8,  8),
         dt.datetime(2020, 8,  6),
         dt.datetime(2020, 8,  4),
         dt.datetime(2020, 8,  3)]

times = [dt.datetime(1900, 1, 1, 22,  7, 0),
         dt.datetime(1900, 1, 1, 23,  0, 0),
         dt.datetime(1900, 1, 1, 21,  5, 0),
         dt.datetime(1900, 1, 1,  2, 33, 0),
         dt.datetime(1900, 1, 1,  2, 33, 0),
         dt.datetime(1900, 1, 1, 14,  0, 0)]

fig, ax = plt.subplots()

ax.plot(dates, times, "ro")

ax.yaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
plt.gca().yaxis.set_major_locator(mdates.HourLocator())

ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y/%m/%d"))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())

fig.autofmt_xdate()

plt.show()

以上代码运行良好。但是,当我将时区从 UTC 转换为 US/Eastern 时,我得到了相同的结果,就好像我什么也没做一样。

import pytz
old_timezone = pytz.timezone("UTC")
new_timezone = pytz.timezone("US/Eastern")
times = [old_timezone.localize(t).astimezone(new_timezone) for t in times]

时区对话前后的结果:

例如,当我在对话前后打印列表的第一个元素 times 时,我得到了不同的预期结果。所以对话很顺利:

1900-01-01 22:07:00       # before
1900-01-01 17:11:00-04:56 # after

为了获得正确的本地化时间,我建议将 dates 列表中的日期与 times 列表中的时间结合起来 - 假设它们属于同一类!否则,日期 1900-1-1 的本地化很可能不正确。

import datetime as dt
from dateutil.tz import gettz

dates = [dt.datetime(2020, 8, 11),
         dt.datetime(2020, 8,  9),
         dt.datetime(2020, 8,  8),
         dt.datetime(2020, 8,  6),
         dt.datetime(2020, 8,  4),
         dt.datetime(2020, 8,  3)]

times = [dt.datetime(1900, 1, 1, 22,  7, 0),
         dt.datetime(1900, 1, 1, 23,  0, 0),
         dt.datetime(1900, 1, 1, 21,  5, 0),
         dt.datetime(1900, 1, 1,  2, 33, 0),
         dt.datetime(1900, 1, 1,  2, 33, 0),
         dt.datetime(1900, 1, 1, 14,  0, 0)]

loctimes = [dt.datetime.combine( # outer combine:
                dt.date(1900, 1, 1), # will reset the date back to 1900-01-01
                dt.datetime.combine(d.date(), t.time()) # inner combine: date from "dates" with time from "times"
                .replace(tzinfo=dt.timezone.utc) # define that it's UTC
                .astimezone(gettz('US/Eastern')) # change timezone to US/Eastern
                .time() # use only the time part from the inner combine
            ) # outer combine done
            for d, t in zip(dates, times)]

# loctimes
# [datetime.datetime(1900, 1, 1, 18, 7),
#  datetime.datetime(1900, 1, 1, 19, 0),
#  datetime.datetime(1900, 1, 1, 17, 5),
#  datetime.datetime(1900, 1, 1, 22, 33),
#  datetime.datetime(1900, 1, 1, 22, 33),
#  datetime.datetime(1900, 1, 1, 10, 0)]

现在剧情按预期运行了:

import matplotlib.pyplot as plt
import matplotlib.dates as mdates

fig, ax = plt.subplots()
ax.plot(dates, loctimes, "ro")
ax.yaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
plt.gca().yaxis.set_major_locator(mdates.HourLocator())
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y/%m/%d"))
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
fig.autofmt_xdate()
plt.show()