Skip to content

Commit

Permalink
fix correct use of timezone information for date plots
Browse files Browse the repository at this point in the history
  • Loading branch information
newville committed Jan 22, 2025
1 parent e530e88 commit 9c9b69d
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 9 deletions.
10 changes: 7 additions & 3 deletions wxmplot/basepanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,9 +15,12 @@
from matplotlib.widgets import Lasso
from matplotlib import dates
from matplotlib.backends.backend_wx import RendererWx
import pytz

from .utils import Printer, MenuItem

tzname = os.environ.get('TZ', 'UTC')
TIMEZONE = pytz.timezone(tzname)

class BasePanel(wx.Panel):
"""
Expand Down Expand Up @@ -46,6 +49,7 @@ def __init__(self, parent, messenger=None,
self._yfmt = self._y2fmt = self._xfmt = None
self._y3fmt = self._y4fmt = None
self.use_dates = False
self.dates_tzinfo = TIMEZONE
self.show_config_popup = show_config_popup
self.launch_dir = get_cwd()

Expand Down Expand Up @@ -324,8 +328,8 @@ def __date_format(self, x):
span = self.axes.xaxis.get_view_interval()
tmin = max(1.0, span[0])
tmax = max(2.0, span[1])
tmin = dates.num2date(tmin).timestamp()
tmax = dates.num2date(tmax).timestamp()
tmin = dates.num2date(tmin, tz=self.dates_tzinfo).timestamp()
tmax = dates.num2date(tmax, tz=self.dates_tzinfo).timestamp()
nsec = (tmax - tmin)
fmt = "%H:%M\n%S"
frac = None
Expand All @@ -344,7 +348,7 @@ def __date_format(self, x):
else:
fmt = "%m/%d"

dtval = dates.num2date(x)
dtval = dates.num2date(x, tz=self.dates_tzinfo)
try:
out = dtval.strftime(fmt)
except ValueError:
Expand Down
17 changes: 11 additions & 6 deletions wxmplot/plotpanel.py
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
from numpy import nonzero, where
import matplotlib as mpl
from matplotlib.dates import date2num, datestr2num, num2date
from matplotlib.dates import AutoDateFormatter, AutoDateLocator

from matplotlib.figure import Figure
from matplotlib.ticker import FuncFormatter
Expand All @@ -26,7 +27,7 @@

to_rgba = colorConverter.to_rgba

def format_date(x, xrange):
def format_date(x, xrange, tz=None):
if xrange > 12: # 12 days
dtformat = "%Y-%m-%d"
elif xrange > 3: # 3 days
Expand All @@ -39,7 +40,7 @@ def format_date(x, xrange):
dtformat = "%H:%M:%S.%f"
else:
dtformat = "%M:%S.%f"
return datetime.strftime(num2date(x), dtformat)
return datetime.strftime(num2date(x, tz=tz), dtformat)


class PlotPanel(BasePanel):
Expand Down Expand Up @@ -136,8 +137,8 @@ def oplot(self, xdata, ydata, label=None, xlabel=None, ylabel=None,
bgcolor=None, framecolor=None, gridcolor=None, textcolor=None,
labelfontsize=None, titlefontsize=None, legendfontsize=None,
fullbox=None, axes_style=None, zorder=None, viewpad=None,
theme=None, use_dates=None, dates_style=None, yaxes=1, side=None,
yaxes_tracecolor=None, **kws):
theme=None, use_dates=None, dates_style=None, timezone=None,
yaxes=1, side=None, yaxes_tracecolor=None, **kws):

"""
basic plot method, adding to an existing display
Expand All @@ -161,6 +162,7 @@ def oplot(self, xdata, ydata, label=None, xlabel=None, ylabel=None,

self.dates_style = ifnot_none(dates_style, self.dates_style)
self.use_dates = ifnot_none(use_dates, self.use_dates)

if isinstance(xdata[0], datetime):
self.use_dates = True

Expand All @@ -174,9 +176,12 @@ def oplot(self, xdata, ydata, label=None, xlabel=None, ylabel=None,
if dstyle is None:
dstyle = ''
if isinstance(x0, datetime):
self.dates_tzinfo = xdata[0].tzinfo
xdata = date2num(xdata)
elif isinstance(x0, str) or dstyle.lower().startswith('str'):
xdata = datestr2num(xdata)
if timezone is not None:
self.dates_tzinfo = timezone

linewidth = ifnot_none(linewidth, 2)
conf.viewpad = ifnot_none(viewpad, conf.viewpad)
Expand Down Expand Up @@ -931,7 +936,7 @@ def report_leftdown(self, event=None):
if self.use_dates:
xlims = ax.get_xlim()
xrange = abs(xlims[1] - xlims[0])
x = format_date(x, xrange)
x = format_date(x, xrange, tz=self.dates_tzinfo)
else:
x = f"{x:g}"
msg = f"X,Y= {x}, {y:g}"
Expand Down Expand Up @@ -981,7 +986,7 @@ def report_motion(self, event=None):
ax = self.canvas.figure.get_axes()[0]
xlims = ax.get_xlim()
xrange = abs(xlims[1] - xlims[0])
x = format_date(x, xrange)
x = format_date(x, xrange, tz=self.dates_tzinfo)
else:
x = f"{x:g}"
msg = f"X,Y= {x}, {y:g}"
Expand Down

0 comments on commit 9c9b69d

Please sign in to comment.