
matplotlib.units¶The classes here provide support for using custom classes withmatplotlib, e.g., those that do not expose the array interface but knowhow to converter themselves to arrays. It also supoprts classes withunits and units conversion. Use cases include converters for customobjects, e.g., a list of datetime objects, as well as for objects thatare unit aware. We don’t assume any particular units implementation,rather a units implementation must provide a ConversionInterface, andthe register with the Registry converter dictionary. For example,here is a complete implementation which supports plotting with nativedatetime objects:
importmatplotlib.unitsasunitsimportmatplotlib.datesasdatesimportmatplotlib.tickerastickerimportdatetimeclassDateConverter(units.ConversionInterface):@staticmethoddefconvert(value,unit,axis):'convert value to a scalar or array'returndates.date2num(value)@staticmethoddefaxisinfo(unit,axis):'return major and minor tick locators and formatters'ifunit!='date':returnNonemajloc=dates.AutoDateLocator()majfmt=dates.AutoDateFormatter(majloc)returnAxisInfo(majloc=majloc,majfmt=majfmt,label='date')@staticmethoddefdefault_units(x,axis):'return the default unit for x or None'return'date'# finally we register our object type with a converterunits.registry[datetime.date]=DateConverter()
matplotlib.units.AxisInfo(majloc=None,minloc=None,majfmt=None,minfmt=None,label=None,default_limits=None)¶Bases:object
information to support default axis labeling and tick labeling, anddefault limits
majloc and minloc: TickLocators for the major and minor ticksmajfmt and minfmt: TickFormatters for the major and minor tickslabel: the default axis labeldefault_limits: the default min, max of the axis if no data is presentIf any of the above are None, the axis will simply use the default
matplotlib.units.ConversionInterface¶Bases:object
The minimal interface for a converter to take custom instances (orsequences) and convert them to values mpl can use
axisinfo(unit,axis)¶return an units.AxisInfo instance for axis with the specified units
convert(obj,unit,axis)¶convert obj using unit for the specified axis. If obj is a sequence,return the converted sequence. The ouput must be a sequence of scalarsthat can be used by the numpy array layer
default_units(x,axis)¶return the default unit for x or None for the given axis
is_numlike(x)¶The matplotlib datalim, autoscaling, locators etc work withscalars which are the units converted to floats given thecurrent unit. The converter may be passed these floats, orarrays of them, even when units are set. Derived conversioninterfaces may opt to pass plain-ol unitless numbers throughthe conversion interface and this is a helper function forthem.