88from diagnostic_interface import settings
99from typing import Optional
1010import mplcursors
11-
11+ from dateutil import tz
1212
1313plt .style .use ("seaborn-v0_8-darkgrid" )
14+ local_tz = tz .tzlocal ()
1415
1516
1617class PlotCanvas (FigureCanvas ):
@@ -42,8 +43,13 @@ def plot(self, ts: TimeSeries, title: str, y_label: str) -> None:
4243 self .ax .set_title (title )
4344 self .ax .set_xlabel ("Time" )
4445 self .ax .set_ylabel (y_label )
45- self .ax .xaxis .set_major_formatter (mdates .DateFormatter ("%H:%M" ))
46- self .ax .xaxis .set_major_locator (mdates .HourLocator ())
46+
47+ locator = mdates .AutoDateLocator ()
48+ formatter = mdates .DateFormatter ("%H:%M" , tz = local_tz )
49+
50+ self .ax .xaxis .set_major_formatter (formatter )
51+ self .ax .xaxis .set_major_locator (locator )
52+
4753 self .figure .autofmt_xdate ()
4854
4955 else :
@@ -59,7 +65,7 @@ def plot(self, ts: TimeSeries, title: str, y_label: str) -> None:
5965 @cursor .connect ("add" )
6066 def _ (sel ):
6167 x , y = sel .target # x is a float (matplotlib date), y is the y-value
62- dt = mdates .num2date (x )
68+ dt = mdates .num2date (x , tz = local_tz )
6369 sel .annotation .set_text (
6470 f"{ y :.2f} { ts .units } at { dt .strftime ('%H:%M' )} "
6571 )
@@ -129,9 +135,10 @@ def plot(self, data, data2):
129135 self .ax .legend ([self .line1 , self .line2 ], ["Wind Speed" , "Precipitation Rate" ])
130136
131137 locator = mdates .AutoDateLocator ()
132- formatter = mdates .ConciseDateFormatter ( locator )
133- self . ax . xaxis . set_major_locator ( locator )
138+ formatter = mdates .DateFormatter ( "%H:%M" , tz = local_tz )
139+
134140 self .ax .xaxis .set_major_formatter (formatter )
141+ self .ax .xaxis .set_major_locator (locator )
135142
136143 else :
137144 # Only update data
@@ -154,7 +161,7 @@ def plot(self, data, data2):
154161 @cursor .connect ("add" )
155162 def _ (sel ):
156163 x , y = sel .target # x is a float (matplotlib date), y is the y-value
157- dt = mdates .num2date (x )
164+ dt = mdates .num2date (x , tz = local_tz )
158165 sel .annotation .set_text (
159166 f"{ y :.2f} at { dt .strftime ('%H:%M' )} "
160167 )
@@ -238,9 +245,10 @@ def plot(self, data, plot_title, ylabel):
238245
239246 # Improve datetime formatting
240247 locator = mdates .AutoDateLocator ()
241- formatter = mdates .ConciseDateFormatter ( locator )
242- self . ax . xaxis . set_major_locator ( locator )
248+ formatter = mdates .DateFormatter ( "%H:%M" , tz = local_tz )
249+
243250 self .ax .xaxis .set_major_formatter (formatter )
251+ self .ax .xaxis .set_major_locator (locator )
244252
245253 else :
246254 # Only update data
@@ -259,7 +267,7 @@ def plot(self, data, plot_title, ylabel):
259267 @cursor .connect ("add" )
260268 def _ (sel ):
261269 x , y = sel .target # x is a float (matplotlib date), y is the y-value
262- dt = mdates .num2date (x )
270+ dt = mdates .num2date (x , tz = local_tz )
263271 sel .annotation .set_text (
264272 f"{ y / 1e6 :.2f} MJ/m^2 at { dt .strftime ('%H:%M' )} "
265273 )
0 commit comments