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 byConversionInterface.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 areNone, the axis will simply use thedefault value.

exceptionmatplotlib.units.ConversionError[source]#

Bases:TypeError

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.

staticaxisinfo(unit,axis)[source]#

Return anAxisInfo for the axis with the specified units.

staticconvert(obj,unit,axis)[source]#

Convertobj usingunit for the specifiedaxis.

Ifobj is a sequence, return the converted sequence. The output mustbe a sequence of scalars that can be used by the numpy array layer.

staticdefault_units(x,axis)[source]#

Return the default unit forx orNone for the given axis.

classmatplotlib.units.DecimalConverter[source]#

Bases:ConversionInterface

Converter for decimal.Decimal data to float.

staticconvert(value,unit,axis)[source]#

Convert Decimals to floats.

Theunit andaxis arguments are not used.

Parameters:
valuedecimal.Decimal or iterable

Decimal or list of Decimal need to be converted

classmatplotlib.units.Registry[source]#

Bases:dict

Register types with conversion interface.

get_converter(x)[source]#

Get the converter interface instance forx, or None.