Skip to content

Commit 5c968a0

Browse files
committed
Bugfix: propagate timezone info in plot_date, xaxis_date, etc.
The tz kwarg was being ignored in these axes methods, and DateConverter.default_units was not supplying the timezone when available from its input argument.
1 parent 0c886b8 commit 5c968a0

File tree

4 files changed

+60
-24
lines changed

4 files changed

+60
-24
lines changed

lib/matplotlib/axes.py

Lines changed: 8 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -2716,18 +2716,20 @@ def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs):
27162716
def xaxis_date(self, tz=None):
27172717
"""Sets up x-axis ticks and labels that treat the x data as dates.
27182718
2719-
*tz* is the time zone to use in labeling dates. Defaults to rc value.
2719+
*tz* is a timezone string or :class:`tzinfo` instance.
2720+
Defaults to rc value.
27202721
"""
27212722
# should be enough to inform the unit conversion interface
2722-
# dates are comng in
2723-
self.xaxis.axis_date()
2723+
# dates are coming in
2724+
self.xaxis.axis_date(tz)
27242725

27252726
def yaxis_date(self, tz=None):
27262727
"""Sets up y-axis ticks and labels that treat the y data as dates.
27272728
2728-
*tz* is the time zone to use in labeling dates. Defaults to rc value.
2729+
*tz* is a timezone string or :class:`tzinfo` instance.
2730+
Defaults to rc value.
27292731
"""
2730-
self.yaxis.axis_date()
2732+
self.yaxis.axis_date(tz)
27312733

27322734
def format_xdata(self, x):
27332735
"""
@@ -3845,7 +3847,7 @@ def plot_date(self, x, y, fmt='bo', tz=None, xdate=True, ydate=False,
38453847
*fmt*: string
38463848
The plot format string.
38473849
3848-
*tz*: [ None | timezone string ]
3850+
*tz*: [ None | timezone string | :class:`tzinfo` instance]
38493851
The time zone to use in labeling dates. If *None*, defaults to rc
38503852
value.
38513853

lib/matplotlib/axis.py

Lines changed: 16 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -1218,21 +1218,21 @@ def grid(self, b=None, which='major', **kwargs):
12181218
def update_units(self, data):
12191219
"""
12201220
introspect *data* for units converter and update the
1221-
axis.converter instance if necessary. Return *True* is *data* is
1222-
registered for unit conversion
1221+
axis.converter instance if necessary. Return *True*
1222+
if *data* is registered for unit conversion.
12231223
"""
12241224

12251225
converter = munits.registry.get_converter(data)
1226-
if converter is None: return False
1226+
if converter is None:
1227+
return False
12271228

12281229
neednew = self.converter!=converter
12291230
self.converter = converter
12301231
default = self.converter.default_units(data, self)
1231-
#print 'update units: default="%s", units=%s"'%(default, self.units)
1232+
#print 'update units: default=%s, units=%s'%(default, self.units)
12321233
if default is not None and self.units is None:
12331234
self.set_units(default)
12341235

1235-
12361236
if neednew:
12371237
self._update_axisinfo()
12381238
return True
@@ -1453,14 +1453,21 @@ def zoom(self, direction):
14531453
self.major.locator.zoom(direction)
14541454

14551455

1456-
def axis_date(self):
1456+
def axis_date(self, tz=None):
14571457
"""
14581458
Sets up x-axis ticks and labels that treat the x data as dates.
1459+
*tz* is a :class:`tzinfo` instance or a timezone string.
1460+
This timezone is used to create date labels.
14591461
"""
1462+
# By providing a sample datetime instance with the desired
1463+
# timezone, the registered converter can be selected,
1464+
# and the "units" attribute, which is the timezone, can
1465+
# be set.
14601466
import datetime
1461-
# should be enough to inform the unit conversion interface
1462-
# dates are comng in
1463-
self.update_units(datetime.date(2009,1,1))
1467+
if isinstance(tz, (str, unicode)):
1468+
import pytz
1469+
tz = pytz.timezone(tz)
1470+
self.update_units(datetime.datetime(2009,1,1,0,0,0,0,tz))
14641471

14651472

14661473
class XAxis(Axis):

lib/matplotlib/dates.py

Lines changed: 34 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -1104,15 +1104,26 @@ def weeks(w):
11041104

11051105

11061106
class DateConverter(units.ConversionInterface):
1107-
"""The units are equivalent to the timezone."""
1107+
"""
1108+
Converter for datetime.date and datetime.datetime data,
1109+
or for date/time data represented as it would be converted
1110+
by :func:`date2num`.
1111+
1112+
The 'unit' tag for such data is None or a tzinfo instance.
1113+
"""
11081114

11091115
@staticmethod
11101116
def axisinfo(unit, axis):
1111-
'return the unit AxisInfo'
1112-
# make sure that the axis does not start at 0
1117+
"""
1118+
Return the :class:`~matplotlib.units.AxisInfo` for *unit*.
1119+
1120+
*unit* is a tzinfo instance or None.
1121+
The *axis* argument is required but not used.
1122+
"""
1123+
tz = unit
11131124

1114-
majloc = AutoDateLocator(tz=unit)
1115-
majfmt = AutoDateFormatter(majloc, tz=unit)
1125+
majloc = AutoDateLocator(tz=tz)
1126+
majfmt = AutoDateFormatter(majloc, tz=tz)
11161127
datemin = datetime.date(2000, 1, 1)
11171128
datemax = datetime.date(2010, 1, 1)
11181129

@@ -1121,12 +1132,28 @@ def axisinfo(unit, axis):
11211132

11221133
@staticmethod
11231134
def convert(value, unit, axis):
1124-
if units.ConversionInterface.is_numlike(value): return value
1135+
"""
1136+
If *value* is not already a number or sequence of numbers,
1137+
convert it with :func:`date2num`.
1138+
1139+
The *unit* and *axis* arguments are not used.
1140+
"""
1141+
if units.ConversionInterface.is_numlike(value):
1142+
return value
11251143
return date2num(value)
11261144

11271145
@staticmethod
11281146
def default_units(x, axis):
1129-
'Return the default unit for *x* or None'
1147+
'Return the tzinfo instance of *x* or of its first element, or None'
1148+
try:
1149+
x = x[0]
1150+
except (TypeError, IndexError):
1151+
pass
1152+
1153+
try:
1154+
return x.tzinfo
1155+
except AttributeError:
1156+
pass
11301157
return None
11311158

11321159

lib/matplotlib/units.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,7 +7,7 @@
77
are unit aware. We don't assume any particular units implementation,
88
rather a units implementation must provide a ConversionInterface, and
99
the register with the Registry converter dictionary. For example,
10-
here is a complete implementation which support plotting with native
10+
here is a complete implementation which supports plotting with native
1111
datetime objects::
1212
1313
@@ -48,7 +48,7 @@ def default_units(x, axis):
4848
class AxisInfo:
4949
'information to support default axis labeling and tick labeling, and default limits'
5050
def __init__(self, majloc=None, minloc=None,
51-
majfmt=None, minfmt=None, label=None,
51+
majfmt=None, minfmt=None, label=None,
5252
default_limits=None):
5353
"""
5454
majloc and minloc: TickLocators for the major and minor ticks

0 commit comments

Comments
 (0)