Skip to content

Bugfix: propagate timezone info in plot_date, xaxis_date, etc. #116

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
wants to merge 1 commit into from
Closed
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 8 additions & 6 deletions lib/matplotlib/axes.py
Original file line number Diff line number Diff line change
Expand Up @@ -2716,18 +2716,20 @@ def set_yticklabels(self, labels, fontdict=None, minor=False, **kwargs):
def xaxis_date(self, tz=None):
"""Sets up x-axis ticks and labels that treat the x data as dates.

*tz* is the time zone to use in labeling dates. Defaults to rc value.
*tz* is a timezone string or :class:`tzinfo` instance.
Defaults to rc value.
"""
# should be enough to inform the unit conversion interface
# dates are comng in
self.xaxis.axis_date()
# dates are coming in
self.xaxis.axis_date(tz)

def yaxis_date(self, tz=None):
"""Sets up y-axis ticks and labels that treat the y data as dates.

*tz* is the time zone to use in labeling dates. Defaults to rc value.
*tz* is a timezone string or :class:`tzinfo` instance.
Defaults to rc value.
"""
self.yaxis.axis_date()
self.yaxis.axis_date(tz)

def format_xdata(self, x):
"""
Expand Down Expand Up @@ -3845,7 +3847,7 @@ def plot_date(self, x, y, fmt='bo', tz=None, xdate=True, ydate=False,
*fmt*: string
The plot format string.

*tz*: [ None | timezone string ]
*tz*: [ None | timezone string | :class:`tzinfo` instance]
The time zone to use in labeling dates. If *None*, defaults to rc
value.

Expand Down
25 changes: 16 additions & 9 deletions lib/matplotlib/axis.py
Original file line number Diff line number Diff line change
Expand Up @@ -1218,21 +1218,21 @@ def grid(self, b=None, which='major', **kwargs):
def update_units(self, data):
"""
introspect *data* for units converter and update the
axis.converter instance if necessary. Return *True* is *data* is
registered for unit conversion
axis.converter instance if necessary. Return *True*
if *data* is registered for unit conversion.
"""

converter = munits.registry.get_converter(data)
if converter is None: return False
if converter is None:
return False

neednew = self.converter!=converter
self.converter = converter
default = self.converter.default_units(data, self)
#print 'update units: default="%s", units=%s"'%(default, self.units)
#print 'update units: default=%s, units=%s'%(default, self.units)
if default is not None and self.units is None:
self.set_units(default)


if neednew:
self._update_axisinfo()
return True
Expand Down Expand Up @@ -1453,14 +1453,21 @@ def zoom(self, direction):
self.major.locator.zoom(direction)


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


class XAxis(Axis):
Expand Down
41 changes: 34 additions & 7 deletions lib/matplotlib/dates.py
Original file line number Diff line number Diff line change
Expand Up @@ -1104,15 +1104,26 @@ def weeks(w):


class DateConverter(units.ConversionInterface):
"""The units are equivalent to the timezone."""
"""
Converter for datetime.date and datetime.datetime data,
or for date/time data represented as it would be converted
by :func:`date2num`.

The 'unit' tag for such data is None or a tzinfo instance.
"""

@staticmethod
def axisinfo(unit, axis):
'return the unit AxisInfo'
# make sure that the axis does not start at 0
"""
Return the :class:`~matplotlib.units.AxisInfo` for *unit*.

*unit* is a tzinfo instance or None.
The *axis* argument is required but not used.
"""
tz = unit

majloc = AutoDateLocator(tz=unit)
majfmt = AutoDateFormatter(majloc, tz=unit)
majloc = AutoDateLocator(tz=tz)
majfmt = AutoDateFormatter(majloc, tz=tz)
datemin = datetime.date(2000, 1, 1)
datemax = datetime.date(2010, 1, 1)

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

@staticmethod
def convert(value, unit, axis):
if units.ConversionInterface.is_numlike(value): return value
"""
If *value* is not already a number or sequence of numbers,
convert it with :func:`date2num`.

The *unit* and *axis* arguments are not used.
"""
if units.ConversionInterface.is_numlike(value):
return value
return date2num(value)

@staticmethod
def default_units(x, axis):
'Return the default unit for *x* or None'
'Return the tzinfo instance of *x* or of its first element, or None'
try:
x = x[0]
except (TypeError, IndexError):
pass

try:
return x.tzinfo
except AttributeError:
pass
return None


Expand Down
4 changes: 2 additions & 2 deletions lib/matplotlib/units.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,7 +7,7 @@
are unit aware. We don't assume any particular units implementation,
rather a units implementation must provide a ConversionInterface, and
the register with the Registry converter dictionary. For example,
here is a complete implementation which support plotting with native
here is a complete implementation which supports plotting with native
datetime objects::


Expand Down Expand Up @@ -48,7 +48,7 @@ def default_units(x, axis):
class AxisInfo:
'information to support default axis labeling and tick labeling, and default limits'
def __init__(self, majloc=None, minloc=None,
majfmt=None, minfmt=None, label=None,
majfmt=None, minfmt=None, label=None,
default_limits=None):
"""
majloc and minloc: TickLocators for the major and minor ticks
Expand Down