Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Clean up units.py #10370

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

Merged
merged 2 commits into from
Feb 6, 2018
Merged
Changes from 1 commit
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
60 changes: 36 additions & 24 deletions lib/matplotlib/units.py
Original file line number Diff line number Diff line change
@@ -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::

Expand Down Expand Up @@ -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()

"""
Expand All @@ -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 :
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

„The colon must be omitted if the type is absent.“ https://github.com/numpy/numpy/blob/master/doc/HOWTO_DOCUMENT.rst.txt#sections
Also in the following lines.

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
Expand All @@ -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'
'''
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tripple double quotes (PEP 257)

Return an `~units.AxisInfo` instance for axis with the specified units.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

for the axis

'''
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.'
Copy link
Member

@timhoffm timhoffm Feb 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tripple double quotes

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:
Expand All @@ -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``.'
Copy link
Member

@timhoffm timhoffm Feb 3, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Tripple double quotes


if not len(self):
return None # nothing registered
Expand Down Expand Up @@ -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

Expand Down