Previous topic

Next topic

This Page

Quick search

The classes here provide support for using custom classes with
matplotlib, e.g., those that do not expose the array interface but know
how to converter themselves to arrays. It also supoprts 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 a ConversionInterface, and
the register with the Registry converter dictionary. For example,
here is a complete implementation which supports plotting with native
datetime 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()

information to support default axis labeling and tick labeling, and
default limits

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

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.