W3cubDocs

/Matplotlib 3.5

matplotlib.units

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 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 register with the Registry converter dictionary and provide a ConversionInterface. For example, here is a complete implementation which supports plotting with native datetime objects:

import matplotlib.units as units
import matplotlib.dates as dates
import matplotlib.ticker as ticker
import datetime

class DateConverter(units.ConversionInterface):

    @staticmethod
    def convert(value, unit, axis):
        "Convert a datetime value to a scalar or array."
        return dates.date2num(value)

    @staticmethod
    def axisinfo(unit, axis):
        "Return major and minor tick locators and formatters."
        if unit != 'date':
            return None
        majloc = dates.AutoDateLocator()
        majfmt = dates.AutoDateFormatter(majloc)
        return AxisInfo(majloc=majloc, majfmt=majfmt, label='date')

    @staticmethod
    def default_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 has been plotted.

Notes

If any of the above are None, the axis will simply use the default value.

exceptionmatplotlib.units.ConversionError[source]

Bases: TypeError

classmatplotlib.units.ConversionInterface[source]

Bases: object

The minimal interface for a converter to take custom data types (or sequences) and convert them to values Matplotlib can use.

staticaxisinfo(unit, axis)[source]

Return an AxisInfo for the axis with the specified units.

staticconvert(obj, unit, axis)[source]

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.

staticdefault_units(x, axis)[source]

Return the default unit for x or None for the given axis.

staticis_numlike(x)[source]

[Deprecated] 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.

Notes

Deprecated since version 3.5.

classmatplotlib.units.DecimalConverter[source]

Bases: matplotlib.units.ConversionInterface

Converter for decimal.Decimal data to float.

staticconvert(value, unit, axis)[source]

Convert Decimals to floats.

The unit and axis 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 for x, or None.

© 2012–2021 Matplotlib Development Team. All rights reserved.
Licensed under the Matplotlib License Agreement.
https://matplotlib.org/3.5.1/api/units_api.html