matplotlib.scale#

Scales define the distribution of data values on an axis, e.g. a log scaling.

The mapping is implemented through Transform subclasses.

The following scales are built-in:

A user will often only use the scale name, e.g. when setting the scale through set_xscale: ax.set_xscale("log").

See also the scales examples in the documentation.

Custom scaling can be achieved through FuncScale, or by creating your own ScaleBase subclass and corresponding transforms (see Custom scale). Third parties can register their scales by name through register_scale.

classmatplotlib.scale.ScaleBase(axis)[source] #

Bases: object

The base class for all scales.

Scales are separable transformations, working on a single dimension.

Subclasses should override

name

The scale's name.

get_transform()

A method returning a Transform, which converts data coordinates to scaled coordinates. This transform should be invertible, so that e.g. mouse positions can be converted back to data coordinates.

set_default_locators_and_formatters()

A method that sets default locators and formatters for an Axis that uses this scale.

limit_range_for_scale()

An optional method that "fixes" the axis range to acceptable values, e.g. restricting log-scaled axes to positive values.

Construct a new scale.

Notes

The following note is for scale implementers.

For back-compatibility reasons, scales take an Axis object as the first argument.

Deprecated since version 3.11: The axis parameter is now optional, i.e. matplotlib is compatible with ScaleBase subclasses that do not take an axis parameter.

The axis parameter is pending-deprecated. It will be deprecated in matplotlib 3.13, and removed in matplotlib 3.15.

3rd-party scales are recommended to remove the axis parameter now if they can afford to restrict compatibility to matplotlib >= 3.11 already. Otherwise, they may keep the axis parameter and remove it in time for matplotlib 3.13.

get_transform()[source] #

Return the Transform object associated with this scale.

set_default_locators_and_formatters(axis)[source] #

Set the locators and formatters of axis to instances suitable for this scale.

limit_range_for_scale(vmin, vmax, minpos)[source] #

Return the range vmin, vmax, restricted to the domain supported by this scale (if any).

minpos should be the minimum positive value in the data. This is used by log scales to determine a minimum value.

classmatplotlib.scale.LinearScale(axis)[source] #

Bases: ScaleBase

The default linear scale.

name='linear'#
set_default_locators_and_formatters(axis)[source] #

Set the locators and formatters of axis to instances suitable for this scale.

get_transform()[source] #

Return the transform for linear scaling, which is just the IdentityTransform.

classmatplotlib.scale.FuncTransform(forward, inverse)[source] #

Bases: Transform

A simple transform that takes and arbitrary function for the forward and inverse transform.

Parameters:
forwardcallable

The forward function for the transform. This function must have an inverse and, for best behavior, be monotonic. It must have the signature:

defforward(values: array-like) -> array-like
inversecallable

The inverse of the forward function. Signature as forward.

input_dims=1#

The number of input dimensions of this transform. Must be overridden (with integers) in the subclass.

output_dims=1#

The number of output dimensions of this transform. Must be overridden (with integers) in the subclass.

transform_non_affine(values)[source] #

Apply only the non-affine part of this transformation.

transform(values) is always equivalent to transform_affine(transform_non_affine(values)).

In non-affine transformations, this is generally equivalent to transform(values). In affine transformations, this is always a no-op.

Parameters:
valuesarray

The input values as an array of length input_dims or shape (N, input_dims).

Returns:
array

The output values as an array of length output_dims or shape (N, output_dims), depending on the input.

inverted()[source] #

Return the corresponding inverse transformation.

It holds x == self.inverted().transform(self.transform(x)).

The return value of this method should be treated as temporary. An update to self does not cause a corresponding update to its inverted copy.

has_inverse=True#

True if this transform has a corresponding inverse transform.

is_separable=True#

True if this transform is separable in the x- and y- dimensions.

classmatplotlib.scale.FuncScale(axis, functions)[source] #

Bases: ScaleBase

Provide an arbitrary scale with user-supplied function for the axis.

Parameters:
axisAxis

The axis for the scale.

Note

This parameter is unused and will be removed in an imminent release. It can already be left out because of special preprocessing, so that FuncScale(functions) is valid.

functions(callable, callable)

two-tuple of the forward and inverse functions for the scale. The forward function must be monotonic.

Both functions must have the signature:

defforward(values: array-like) -> array-like
name='function'#
get_transform()[source] #

Return the FuncTransform associated with this scale.

set_default_locators_and_formatters(axis)[source] #

Set the locators and formatters of axis to instances suitable for this scale.

classmatplotlib.scale.LogTransform(base, nonpositive='clip')[source] #

Bases: Transform

Parameters:
shorthand_namestr

A string representing the "name" of the transform. The name carries no significance other than to improve the readability of str(transform) when DEBUG=True.

input_dims=1#

The number of input dimensions of this transform. Must be overridden (with integers) in the subclass.

output_dims=1#

The number of output dimensions of this transform. Must be overridden (with integers) in the subclass.

transform_non_affine(values)[source] #

Apply only the non-affine part of this transformation.

transform(values) is always equivalent to transform_affine(transform_non_affine(values)).

In non-affine transformations, this is generally equivalent to transform(values). In affine transformations, this is always a no-op.

Parameters:
valuesarray

The input values as an array of length input_dims or shape (N, input_dims).

Returns:
array

The output values as an array of length output_dims or shape (N, output_dims), depending on the input.

inverted()[source] #

Return the corresponding inverse transformation.

It holds x == self.inverted().transform(self.transform(x)).

The return value of this method should be treated as temporary. An update to self does not cause a corresponding update to its inverted copy.

has_inverse=True#

True if this transform has a corresponding inverse transform.

is_separable=True#

True if this transform is separable in the x- and y- dimensions.

classmatplotlib.scale.InvertedLogTransform(base)[source] #

Bases: Transform

Parameters:
shorthand_namestr

A string representing the "name" of the transform. The name carries no significance other than to improve the readability of str(transform) when DEBUG=True.

input_dims=1#

The number of input dimensions of this transform. Must be overridden (with integers) in the subclass.

output_dims=1#

The number of output dimensions of this transform. Must be overridden (with integers) in the subclass.

transform_non_affine(values)[source] #

Apply only the non-affine part of this transformation.

transform(values) is always equivalent to transform_affine(transform_non_affine(values)).

In non-affine transformations, this is generally equivalent to transform(values). In affine transformations, this is always a no-op.

Parameters:
valuesarray

The input values as an array of length input_dims or shape (N, input_dims).

Returns:
array

The output values as an array of length output_dims or shape (N, output_dims), depending on the input.

inverted()[source] #

Return the corresponding inverse transformation.

It holds x == self.inverted().transform(self.transform(x)).

The return value of this method should be treated as temporary. An update to self does not cause a corresponding update to its inverted copy.

has_inverse=True#

True if this transform has a corresponding inverse transform.

is_separable=True#

True if this transform is separable in the x- and y- dimensions.

classmatplotlib.scale.LogScale(axis=None, *, base=10, subs=None, nonpositive='clip')[source] #

Bases: ScaleBase

A standard logarithmic scale. Care is taken to only plot positive values.

Parameters:
axisAxis

The axis for the scale.

Note

This parameter is unused and about to be removed in the future. It can already now be left out because of special preprocessing, so that LogScale(base=2) is valid.

basefloat, default: 10

The base of the logarithm.

nonpositive{'clip', 'mask'}, default: 'clip'

Determines the behavior for non-positive values. They can either be masked as invalid, or clipped to a very small positive number.

subssequence of int, default: None

Where to place the subticks between each major tick. For example, in a log10 scale, [2, 3, 4, 5, 6, 7, 8, 9] will place 8 logarithmically spaced minor ticks between each major tick.

name='log'#
propertybase#
set_default_locators_and_formatters(axis)[source] #

Set the locators and formatters of axis to instances suitable for this scale.

get_transform()[source] #

Return the LogTransform associated with this scale.

limit_range_for_scale(vmin, vmax, minpos)[source] #

Limit the domain to positive values.

classmatplotlib.scale.FuncScaleLog(axis, functions, base=10)[source] #

Bases: LogScale

Provide an arbitrary scale with user-supplied function for the axis and then put on a logarithmic axes.

Parameters:
axisAxis

The axis for the scale.

functions(callable, callable)

two-tuple of the forward and inverse functions for the scale. The forward function must be monotonic.

Both functions must have the signature:

defforward(values: array-like) -> array-like
basefloat, default: 10

Logarithmic base of the scale.

name='functionlog'#
propertybase#
get_transform()[source] #

Return the Transform associated with this scale.

classmatplotlib.scale.SymmetricalLogTransform(base, linthresh, linscale)[source] #

Bases: Transform

Parameters:
shorthand_namestr

A string representing the "name" of the transform. The name carries no significance other than to improve the readability of str(transform) when DEBUG=True.

input_dims=1#

The number of input dimensions of this transform. Must be overridden (with integers) in the subclass.

output_dims=1#

The number of output dimensions of this transform. Must be overridden (with integers) in the subclass.

transform_non_affine(values)[source] #

Apply only the non-affine part of this transformation.

transform(values) is always equivalent to transform_affine(transform_non_affine(values)).

In non-affine transformations, this is generally equivalent to transform(values). In affine transformations, this is always a no-op.

Parameters:
valuesarray

The input values as an array of length input_dims or shape (N, input_dims).

Returns:
array

The output values as an array of length output_dims or shape (N, output_dims), depending on the input.

inverted()[source] #

Return the corresponding inverse transformation.

It holds x == self.inverted().transform(self.transform(x)).

The return value of this method should be treated as temporary. An update to self does not cause a corresponding update to its inverted copy.

has_inverse=True#

True if this transform has a corresponding inverse transform.

is_separable=True#

True if this transform is separable in the x- and y- dimensions.

classmatplotlib.scale.InvertedSymmetricalLogTransform(base, linthresh, linscale)[source] #

Bases: Transform

Parameters:
shorthand_namestr

A string representing the "name" of the transform. The name carries no significance other than to improve the readability of str(transform) when DEBUG=True.

input_dims=1#

The number of input dimensions of this transform. Must be overridden (with integers) in the subclass.

output_dims=1#

The number of output dimensions of this transform. Must be overridden (with integers) in the subclass.

transform_non_affine(values)[source] #

Apply only the non-affine part of this transformation.

transform(values) is always equivalent to transform_affine(transform_non_affine(values)).

In non-affine transformations, this is generally equivalent to transform(values). In affine transformations, this is always a no-op.

Parameters:
valuesarray

The input values as an array of length input_dims or shape (N, input_dims).

Returns:
array

The output values as an array of length output_dims or shape (N, output_dims), depending on the input.

inverted()[source] #

Return the corresponding inverse transformation.

It holds x == self.inverted().transform(self.transform(x)).

The return value of this method should be treated as temporary. An update to self does not cause a corresponding update to its inverted copy.

has_inverse=True#

True if this transform has a corresponding inverse transform.

is_separable=True#

True if this transform is separable in the x- and y- dimensions.

classmatplotlib.scale.SymmetricalLogScale(axis=None, *, base=10, linthresh=2, subs=None, linscale=1)[source] #

Bases: ScaleBase

The symmetrical logarithmic scale is logarithmic in both the positive and negative directions from the origin.

Since the values close to zero tend toward infinity, there is a need to have a range around zero that is linear. The parameter linthresh allows the user to specify the size of this range (-linthresh, linthresh).

See Symlog scale for a detailed description.

Parameters:
axisAxis

The axis for the scale.

Note

This parameter is unused and about to be removed in the future. It can already now be left out because of special preprocessing, so that SymmetricalLocSacle(base=2) is valid.

basefloat, default: 10

The base of the logarithm.

linthreshfloat, default: 2

Defines the range (-x, x), within which the plot is linear. This avoids having the plot go to infinity around zero.

subssequence of int

Where to place the subticks between each major tick. For example, in a log10 scale: [2, 3, 4, 5, 6, 7, 8, 9] will place 8 logarithmically spaced minor ticks between each major tick.

linscalefloat, optional

This allows the linear range (-linthresh, linthresh) to be stretched relative to the logarithmic range. Its value is the number of decades to use for each half of the linear range. For example, when linscale == 1.0 (the default), the space used for the positive and negative halves of the linear range will be equal to one decade in the logarithmic range.

Construct a new scale.

Notes

The following note is for scale implementers.

For back-compatibility reasons, scales take an Axis object as the first argument.

Deprecated since version 3.11: The axis parameter is now optional, i.e. matplotlib is compatible with ScaleBase subclasses that do not take an axis parameter.

The axis parameter is pending-deprecated. It will be deprecated in matplotlib 3.13, and removed in matplotlib 3.15.

3rd-party scales are recommended to remove the axis parameter now if they can afford to restrict compatibility to matplotlib >= 3.11 already. Otherwise, they may keep the axis parameter and remove it in time for matplotlib 3.13.

name='symlog'#
propertybase#
propertylinthresh#
propertylinscale#
set_default_locators_and_formatters(axis)[source] #

Set the locators and formatters of axis to instances suitable for this scale.

get_transform()[source] #

Return the SymmetricalLogTransform associated with this scale.

classmatplotlib.scale.AsinhTransform(linear_width)[source] #

Bases: Transform

Inverse hyperbolic-sine transformation used by AsinhScale

Parameters:
shorthand_namestr

A string representing the "name" of the transform. The name carries no significance other than to improve the readability of str(transform) when DEBUG=True.

input_dims=1#

The number of input dimensions of this transform. Must be overridden (with integers) in the subclass.

output_dims=1#

The number of output dimensions of this transform. Must be overridden (with integers) in the subclass.

transform_non_affine(values)[source] #

Apply only the non-affine part of this transformation.

transform(values) is always equivalent to transform_affine(transform_non_affine(values)).

In non-affine transformations, this is generally equivalent to transform(values). In affine transformations, this is always a no-op.

Parameters:
valuesarray

The input values as an array of length input_dims or shape (N, input_dims).

Returns:
array

The output values as an array of length output_dims or shape (N, output_dims), depending on the input.

inverted()[source] #

Return the corresponding inverse transformation.

It holds x == self.inverted().transform(self.transform(x)).

The return value of this method should be treated as temporary. An update to self does not cause a corresponding update to its inverted copy.

has_inverse=True#

True if this transform has a corresponding inverse transform.

is_separable=True#

True if this transform is separable in the x- and y- dimensions.

classmatplotlib.scale.InvertedAsinhTransform(linear_width)[source] #

Bases: Transform

Hyperbolic sine transformation used by AsinhScale

Parameters:
shorthand_namestr

A string representing the "name" of the transform. The name carries no significance other than to improve the readability of str(transform) when DEBUG=True.

input_dims=1#

The number of input dimensions of this transform. Must be overridden (with integers) in the subclass.

output_dims=1#

The number of output dimensions of this transform. Must be overridden (with integers) in the subclass.

transform_non_affine(values)[source] #

Apply only the non-affine part of this transformation.

transform(values) is always equivalent to transform_affine(transform_non_affine(values)).

In non-affine transformations, this is generally equivalent to transform(values). In affine transformations, this is always a no-op.

Parameters:
valuesarray

The input values as an array of length input_dims or shape (N, input_dims).

Returns:
array

The output values as an array of length output_dims or shape (N, output_dims), depending on the input.

inverted()[source] #

Return the corresponding inverse transformation.

It holds x == self.inverted().transform(self.transform(x)).

The return value of this method should be treated as temporary. An update to self does not cause a corresponding update to its inverted copy.

has_inverse=True#

True if this transform has a corresponding inverse transform.

is_separable=True#

True if this transform is separable in the x- and y- dimensions.

classmatplotlib.scale.AsinhScale(axis=None, *, linear_width=1.0, base=10, subs='auto', **kwargs)[source] #

Bases: ScaleBase

A quasi-logarithmic scale based on the inverse hyperbolic sine (asinh)

For values close to zero, this is essentially a linear scale, but for large magnitude values (either positive or negative) it is asymptotically logarithmic. The transition between these linear and logarithmic regimes is smooth, and has no discontinuities in the function gradient in contrast to the SymmetricalLogScale ("symlog") scale.

Specifically, the transformation of an axis coordinate \(a\) is \(a \rightarrow a_0 \sinh^{-1} (a / a_0)\) where \(a_0\) is the effective width of the linear region of the transformation. In that region, the transformation is \(a \rightarrow a + \mathcal{O}(a^3)\). For large values of \(a\) the transformation behaves as \(a \rightarrow a_0 ,円 \mathrm{sgn}(a) \ln |a| + \mathcal{O}(1)\).

Note

This API is provisional and may be revised in the future based on early user feedback.

Parameters:
axisAxis

The axis for the scale.

Note

This parameter is unused and about to be removed in the future. It can already now be left out because of special preprocessing, so that AsinhScale() is valid.

linear_widthfloat, default: 1

The scale parameter (elsewhere referred to as \(a_0\)) defining the extent of the quasi-linear region, and the coordinate values beyond which the transformation becomes asymptotically logarithmic.

baseint, default: 10

The number base used for rounding tick locations on a logarithmic scale. If this is less than one, then rounding is to the nearest integer multiple of powers of ten.

subssequence of int

Multiples of the number base used for minor ticks. If set to 'auto', this will use built-in defaults, e.g. (2, 5) for base=10.

name='asinh'#
auto_tick_multipliers={3: (2,), 4: (2,), 5: (2,), 8: (2, 4), 10: (2, 5), 16: (2, 4, 8), 64: (4, 16), 1024: (256, 512)}#
propertylinear_width#
get_transform()[source] #

Return the Transform object associated with this scale.

set_default_locators_and_formatters(axis)[source] #

Set the locators and formatters of axis to instances suitable for this scale.

classmatplotlib.scale.LogitTransform(nonpositive='mask')[source] #

Bases: Transform

Parameters:
shorthand_namestr

A string representing the "name" of the transform. The name carries no significance other than to improve the readability of str(transform) when DEBUG=True.

input_dims=1#

The number of input dimensions of this transform. Must be overridden (with integers) in the subclass.

output_dims=1#

The number of output dimensions of this transform. Must be overridden (with integers) in the subclass.

transform_non_affine(values)[source] #

logit transform (base 10), masked or clipped

inverted()[source] #

Return the corresponding inverse transformation.

It holds x == self.inverted().transform(self.transform(x)).

The return value of this method should be treated as temporary. An update to self does not cause a corresponding update to its inverted copy.

has_inverse=True#

True if this transform has a corresponding inverse transform.

is_separable=True#

True if this transform is separable in the x- and y- dimensions.

classmatplotlib.scale.LogisticTransform(nonpositive='mask')[source] #

Bases: Transform

Parameters:
shorthand_namestr

A string representing the "name" of the transform. The name carries no significance other than to improve the readability of str(transform) when DEBUG=True.

input_dims=1#

The number of input dimensions of this transform. Must be overridden (with integers) in the subclass.

output_dims=1#

The number of output dimensions of this transform. Must be overridden (with integers) in the subclass.

transform_non_affine(values)[source] #

logistic transform (base 10)

inverted()[source] #

Return the corresponding inverse transformation.

It holds x == self.inverted().transform(self.transform(x)).

The return value of this method should be treated as temporary. An update to self does not cause a corresponding update to its inverted copy.

has_inverse=True#

True if this transform has a corresponding inverse transform.

is_separable=True#

True if this transform is separable in the x- and y- dimensions.

classmatplotlib.scale.LogitScale(axis=None, nonpositive='mask', *, one_half='\\frac{1}{2}', use_overline=False)[source] #

Bases: ScaleBase

Logit scale for data between zero and one, both excluded.

This scale is similar to a log scale close to zero and to one, and almost linear around 0.5. It maps the interval ]0, 1[ onto ]-infty, +infty[.

Parameters:
axisAxis

The axis for the scale.

Note

This parameter is unused and about to be removed in the future. It can already now be left out because of special preprocessing, so that LogitScale() is valid.

nonpositive{'mask', 'clip'}

Determines the behavior for values beyond the open interval ]0, 1[. They can either be masked as invalid, or clipped to a number very close to 0 or 1.

use_overlinebool, default: False

Indicate the usage of survival notation (overline{x}) in place of standard notation (1-x) for probability close to one.

one_halfstr, default: r"frac{1}{2}"

The string used for ticks formatter to represent 1/2.

name='logit'#
get_transform()[source] #

Return the LogitTransform associated with this scale.

set_default_locators_and_formatters(axis)[source] #

Set the locators and formatters of axis to instances suitable for this scale.

limit_range_for_scale(vmin, vmax, minpos)[source] #

Limit the domain to values between 0 and 1 (excluded).

matplotlib.scale.get_scale_names()[source] #

Return the names of the available scales.

matplotlib.scale.scale_factory(scale, axis, **kwargs)[source] #

Return a scale class by name.

Parameters:
scale{'asinh', 'function', 'functionlog', 'linear', 'log', 'logit', 'symlog'}
axisAxis
matplotlib.scale.register_scale(scale_class)[source] #

Register a new kind of scale.

Parameters:
scale_classsubclass of ScaleBase

The scale to register.

On this page