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 convert themselves to arrays. It also supports 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 register with the Registry converterdictionary and provide aConversionInterface
. For example,here is a complete implementation which supports plotting with nativedatetime objects:
importmatplotlib.unitsasunitsimportmatplotlib.datesasdatesimportmatplotlib.tickerastickerimportdatetimeclassDateConverter(units.ConversionInterface):@staticmethoddefconvert(value,unit,axis):"Convert a datetime 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)returnunits.AxisInfo(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 the Matplotlib units registry.units.registry[datetime.date]=DateConverter()
- classmatplotlib.units.AxisInfo(majloc=None,minloc=None,majfmt=None,minfmt=None,label=None,default_limits=None)[source]#
Bases:
object
Information to support default axis labeling, tick labeling, and limits.
An instance of this class must be returned by
ConversionInterface.axisinfo
.- Parameters:
- majloc, minlocLocator, optional
Tick locators for the major and minor ticks.
- majfmt, minfmtFormatter, optional
Tick formatters for the major and minor ticks.
- labelstr, optional
The default axis label.
- default_limitsoptional
The default min and max limits of the axis if no data hasbeen plotted.
Notes
If any of the above are
None
, the axis will simply use thedefault value.
- classmatplotlib.units.ConversionInterface[source]#
Bases:
object
The minimal interface for a converter to take custom data types (orsequences) and convert them to values Matplotlib can use.
- classmatplotlib.units.DecimalConverter[source]#
Bases:
ConversionInterface
Converter for decimal.Decimal data to float.