I’m trying to plot a figure in which dates, without times, are in x-axis and times, without dates, are in y-axis:
JavaScript
x
33
33
1
import datetime as dt
2
import matplotlib.pyplot as plt
3
import matplotlib.dates as mdates
4
5
6
dates = [dt.datetime(2020, 8, 11),
7
dt.datetime(2020, 8, 9),
8
dt.datetime(2020, 8, 8),
9
dt.datetime(2020, 8, 6),
10
dt.datetime(2020, 8, 4),
11
dt.datetime(2020, 8, 3)]
12
13
times = [dt.datetime(1900, 1, 1, 22, 7, 0),
14
dt.datetime(1900, 1, 1, 23, 0, 0),
15
dt.datetime(1900, 1, 1, 21, 5, 0),
16
dt.datetime(1900, 1, 1, 2, 33, 0),
17
dt.datetime(1900, 1, 1, 2, 33, 0),
18
dt.datetime(1900, 1, 1, 14, 0, 0)]
19
20
fig, ax = plt.subplots()
21
22
ax.plot(dates, times, "ro")
23
24
ax.yaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
25
plt.gca().yaxis.set_major_locator(mdates.HourLocator())
26
27
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y/%m/%d"))
28
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
29
30
fig.autofmt_xdate()
31
32
plt.show()
33
The above code works well. However when I convert time zones from UTC to US/Eastern I get the same result, as if I did nothing.
JavaScript
1
5
1
import pytz
2
old_timezone = pytz.timezone("UTC")
3
new_timezone = pytz.timezone("US/Eastern")
4
times = [old_timezone.localize(t).astimezone(new_timezone) for t in times]
5
The result of both before and after time zone conversation:
When I print, for example, first element of the list times
before and after conversation I get different and the desired result. So the conversation works well:
JavaScript
1
3
1
1900-01-01 22:07:00 # before
2
1900-01-01 17:11:00-04:56 # after
3
Advertisement
Answer
to get a correct localization of the time, I suggest to combine the date from your dates
list with the time from your times
list – assuming they belong together! otherwise, localization will most likely be incorrect for the date 1900-1-1.
JavaScript
1
34
34
1
import datetime as dt
2
from dateutil.tz import gettz
3
4
dates = [dt.datetime(2020, 8, 11),
5
dt.datetime(2020, 8, 9),
6
dt.datetime(2020, 8, 8),
7
dt.datetime(2020, 8, 6),
8
dt.datetime(2020, 8, 4),
9
dt.datetime(2020, 8, 3)]
10
11
times = [dt.datetime(1900, 1, 1, 22, 7, 0),
12
dt.datetime(1900, 1, 1, 23, 0, 0),
13
dt.datetime(1900, 1, 1, 21, 5, 0),
14
dt.datetime(1900, 1, 1, 2, 33, 0),
15
dt.datetime(1900, 1, 1, 2, 33, 0),
16
dt.datetime(1900, 1, 1, 14, 0, 0)]
17
18
loctimes = [dt.datetime.combine( # outer combine:
19
dt.date(1900, 1, 1), # will reset the date back to 1900-01-01
20
dt.datetime.combine(d.date(), t.time()) # inner combine: date from "dates" with time from "times"
21
.replace(tzinfo=dt.timezone.utc) # define that it's UTC
22
.astimezone(gettz('US/Eastern')) # change timezone to US/Eastern
23
.time() # use only the time part from the inner combine
24
) # outer combine done
25
for d, t in zip(dates, times)]
26
27
# loctimes
28
# [datetime.datetime(1900, 1, 1, 18, 7),
29
# datetime.datetime(1900, 1, 1, 19, 0),
30
# datetime.datetime(1900, 1, 1, 17, 5),
31
# datetime.datetime(1900, 1, 1, 22, 33),
32
# datetime.datetime(1900, 1, 1, 22, 33),
33
# datetime.datetime(1900, 1, 1, 10, 0)]
34
now the plot works as expected:
JavaScript
1
12
12
1
import matplotlib.pyplot as plt
2
import matplotlib.dates as mdates
3
4
fig, ax = plt.subplots()
5
ax.plot(dates, loctimes, "ro")
6
ax.yaxis.set_major_formatter(mdates.DateFormatter("%H:%M"))
7
plt.gca().yaxis.set_major_locator(mdates.HourLocator())
8
ax.xaxis.set_major_formatter(mdates.DateFormatter("%Y/%m/%d"))
9
plt.gca().xaxis.set_major_locator(mdates.DayLocator())
10
fig.autofmt_xdate()
11
plt.show()
12