Matplotlib supports the addition of custom procedures that transform the data before it is displayed.
There is an important distinction between two kinds of transformations. Separable transformations, working on a single dimension, are called "scales", and non-separable transformations, that handle data in two or more dimensions at a time, are called "projections".
From the user's perspective, the scale of a plot can be set with
set_xscale()
and
set_yscale()
. Projections can be chosen
using the projection
keyword argument to the
plot()
or subplot()
functions, e.g.:
plot(x, y, projection="custom")
This document is intended for developers and advanced users who need to create new scales and projections for matplotlib. The necessary code for scales and projections can be included anywhere: directly within a plot script, in third-party code, or in the matplotlib source tree itself.
Adding a new scale consists of defining a subclass of
matplotlib.scale.ScaleBase
, that includes the following
elements:
limit_range_for_scale()
). A log scale, for instance, would
prevent the range from including values less than or equal to zero.limit_range_for_scale()
, which is
always enforced, the range setting here is only used when
automatically setting the range of the plot.Once the class is defined, it must be registered with matplotlib so that the user can select it.
A full-fledged and heavily annotated example is in
Custom scale. There are also some classes
in matplotlib.scale
that may be used as starting points.
Adding a new projection consists of defining a projection axes which
subclasses matplotlib.axes.Axes
and includes the following
elements:
cla()
),
since the defaults for a rectilinear axes may not be appropriate.matplotlib.projections.polar
.Once the projection axes is defined, it can be used in one of two ways:
By defining the class attribute name
, the projection axes can be
registered with matplotlib.projections.register_projection()
and subsequently simply invoked by name:
plt.axes(projection='my_proj_name')
For more complex, parameterisable projections, a generic "projection" object
may be defined which includes the method _as_mpl_axes
. _as_mpl_axes
should take no arguments and return the projection's axes subclass and a
dictionary of additional arguments to pass to the subclass' __init__
method. Subsequently a parameterised projection can be initialised with:
plt.axes(projection=MyProjection(param1=param1_value))
where MyProjection is an object which implements a _as_mpl_axes
method.
A full-fledged and heavily annotated example is in
Custom projection. The polar plot
functionality in matplotlib.projections.polar
may also be of
interest.