From 84641678835d449f619e92c7f70b422cfdf8ade8 Mon Sep 17 00:00:00 2001 From: David Stansby Date: Fri, 2 Feb 2018 15:28:50 +0000 Subject: [PATCH 1/2] Clean up units.py --- lib/matplotlib/units.py | 60 ++++++++++++++++++++++++----------------- 1 file changed, 36 insertions(+), 24 deletions(-) diff --git a/lib/matplotlib/units.py b/lib/matplotlib/units.py index 793fad3cb01f..b84602f9a413 100644 --- a/lib/matplotlib/units.py +++ b/lib/matplotlib/units.py @@ -1,12 +1,12 @@ """ The classes here provide support for using custom classes with -matplotlib, e.g., those that do not expose the array interface but know +Matplotlib, e.g., those that do not expose the array interface but know how to convert themselves to arrays. It also supports classes with units and units conversion. Use cases include converters for custom objects, e.g., a list of datetime objects, as well as for objects that are unit aware. We don't assume any particular units implementation; rather a units implementation must provide the register with the Registry -converter dictionary and a ConversionInterface. For example, +converter dictionary and a `ConversionInterface`. For example, here is a complete implementation which supports plotting with native datetime objects:: @@ -37,7 +37,7 @@ def default_units(x, axis): 'return the default unit for x or None' return 'date' - # finally we register our object type with a converter + # Finally we register our object type with a converter units.registry[datetime.date] = DateConverter() """ @@ -51,17 +51,29 @@ def default_units(x, axis): class AxisInfo(object): - """information to support default axis labeling and tick labeling, and - default limits""" + """ + Information to support default axis labeling, tick labeling, and + default limits. + """ def __init__(self, majloc=None, minloc=None, majfmt=None, minfmt=None, label=None, default_limits=None): """ - majloc and minloc: TickLocators for the major and minor ticks - majfmt and minfmt: TickFormatters for the major and minor ticks - label: the default axis label - default_limits: the default min, max of the axis if no data is present - If any of the above are None, the axis will simply use the default + Parameters + ---------- + majloc, minloc : + TickLocators for the major and minor ticks. + majfmt, minfmt : + TickFormatters for the major and minor ticks. + label : + The default axis label + default_limits : + The default min, max of the axis if no data is present + + Notes + ----- + If any of the above are ``None``, the axis will simply use the + default value. """ self.majloc = majloc self.minloc = minloc @@ -74,37 +86,37 @@ def __init__(self, majloc=None, minloc=None, class ConversionInterface(object): """ The minimal interface for a converter to take custom instances (or - sequences) and convert them to values mpl can use + sequences) and convert them to values Matplotlib can use. """ @staticmethod def axisinfo(unit, axis): - 'return an units.AxisInfo instance for axis with the specified units' + ''' + Return an `~units.AxisInfo` instance for axis with the specified units. + ''' return None @staticmethod def default_units(x, axis): - 'return the default unit for x or None for the given axis' + 'Return the default unit for *x* or ``None`` for the given axis.' return None @staticmethod def convert(obj, unit, axis): """ - convert obj using unit for the specified axis. If obj is a sequence, - return the converted sequence. The output must be a sequence of - scalars that can be used by the numpy array layer + Convert *obj* using *unit* for the specified *axis*. + If *obj* is a sequence, return the converted sequence. + The output must be a sequence of scalars that can be used by the numpy + array layer. """ return obj @staticmethod def is_numlike(x): """ - The matplotlib datalim, autoscaling, locators etc work with + The Matplotlib datalim, autoscaling, locators etc work with scalars which are the units converted to floats given the current unit. The converter may be passed these floats, or - arrays of them, even when units are set. Derived conversion - interfaces may opt to pass plain-ol unitless numbers through - the conversion interface and this is a helper function for - them. + arrays of them, even when units are set. """ if iterable(x): for thisx in x: @@ -115,14 +127,14 @@ def is_numlike(x): class Registry(dict): """ - register types with conversion interface + Register types with conversion interface. """ def __init__(self): dict.__init__(self) self._cached = {} def get_converter(self, x): - 'get the converter interface instance for x, or None' + 'Get the converter interface instance for *x*, or ``None``.' if not len(self): return None # nothing registered @@ -153,7 +165,7 @@ def get_converter(self, x): # not ever return a non-subclass for a single element. next_item = xravel[0] if (not isinstance(next_item, np.ndarray) or - next_item.shape != x.shape): + next_item.shape != x.shape): converter = self.get_converter(next_item) return converter From 5464f3f79577bb0e90924ea968cf2f4f4201bfaa Mon Sep 17 00:00:00 2001 From: David Stansby Date: Sun, 4 Feb 2018 19:23:45 +0000 Subject: [PATCH 2/2] Small fixes --- lib/matplotlib/units.py | 27 ++++++++++++++++----------- 1 file changed, 16 insertions(+), 11 deletions(-) diff --git a/lib/matplotlib/units.py b/lib/matplotlib/units.py index b84602f9a413..f19004dcd92c 100644 --- a/lib/matplotlib/units.py +++ b/lib/matplotlib/units.py @@ -61,14 +61,14 @@ def __init__(self, majloc=None, minloc=None, """ Parameters ---------- - majloc, minloc : + majloc, minloc TickLocators for the major and minor ticks. - majfmt, minfmt : + majfmt, minfmt TickFormatters for the major and minor ticks. - label : - The default axis label - default_limits : - The default min, max of the axis if no data is present + label + The default axis label. + default_limits + The default min, max of the axis if no data is present. Notes ----- @@ -90,14 +90,17 @@ class ConversionInterface(object): """ @staticmethod def axisinfo(unit, axis): - ''' - Return an `~units.AxisInfo` instance for axis with the specified units. - ''' + """ + Return an `~units.AxisInfo` instance for the axis with the + specified units. + """ return None @staticmethod def default_units(x, axis): - 'Return the default unit for *x* or ``None`` for the given axis.' + """ + Return the default unit for *x* or ``None`` for the given axis. + """ return None @staticmethod @@ -134,7 +137,9 @@ def __init__(self): self._cached = {} def get_converter(self, x): - 'Get the converter interface instance for *x*, or ``None``.' + """ + Get the converter interface instance for *x*, or ``None``. + """ if not len(self): return None # nothing registered