Colorbars are a visualization of the mapping from scalar values to colors. In Matplotlib they are drawn into a dedicated Axes
.
Note
Colorbars are typically created through Figure.colorbar
or its pyplot wrapper pyplot.colorbar
, which internally use Colorbar
together with make_axes_gridspec
(for GridSpec
-positioned axes) or make_axes
(for non-GridSpec
-positioned axes).
End-users most likely won't need to directly use this module's API.
Bases: object
Draw a colorbar in an existing axes.
Typically, colorbars are created using Figure.colorbar
or pyplot.colorbar
and associated with ScalarMappable
s (such as an AxesImage
generated via imshow
).
In order to draw a colorbar not associated with other elements in the figure, e.g. when showing a colormap by itself, one can create an empty ScalarMappable
, or directly pass cmap and norm instead of mappable to Colorbar
.
Useful public methods are set_label()
and add_lines()
.
Axes
The Axes
instance in which the colorbar is drawn.
ScalarMappable
The mappable whose colormap and norm will be used.
To show the under- and over- value colors, the mappable's norm should be specified as
norm = colors.Normalize(clip=False)
To show the colors versus index instead of on a 0-1 scale, use:
norm=colors.NoNorm()
Colormap
, default: rcParams["image.cmap"]
(default: 'viridis'
)
The colormap to use. This parameter is ignored, unless mappable is None.
Normalize
The normalization to use. This parameter is ignored, unless mappable is None.
The colorbar transparency between 0 (transparent) and 1 (opaque).
If unset, the colormap will be displayed on a 0-1 scale.
Locator
or array-like of float
Formatter
Axes
The Axes
instance in which the colorbar is drawn.
A list of LineCollection
(empty if no lines were drawn).
LineCollection
A LineCollection (empty if drawedges is False
).
Draw lines on the colorbar.
The lines are appended to the list lines
.
The positions of the lines.
Either a single color applying to all lines or one color value for each line.
Either a single linewidth applying to all lines or one linewidth for each line.
Whether to remove any previously added lines.
Alternatively, this method can also be called with the signature colorbar.add_lines(contour_set, erase=True)
, in which case levels, colors, and linewidths are taken from contour_set.
Calculate any free parameters based on the current cmap and norm, and do all the drawing.
Return the ticks as a list of locations.
if True return the minor ticks.
Turn the minor ticks of the colorbar off.
Turn on colorbar minor ticks.
Remove this colorbar from the figure.
If the colorbar was created with use_gridspec=True
the previous gridspec is restored.
Set the transparency between 0 (transparent) and 1 (opaque).
If an array is provided, alpha will be set to None to use the transparency values associated with the colormap.
Add a label to the long axis of the colorbar.
The label text.
The location of the label.
Defaults to rcParams["xaxis.labellocation"]
(default: 'center'
) or rcParams["yaxis.labellocation"]
(default: 'center'
) depending on the orientation.
Keyword arguments are passed to set_xlabel
/ set_ylabel
. Supported keywords are labelpad and Text
properties.
Set tick labels.
Discouraged
The use of this method is discouraged, because of the dependency on tick positions. In most cases, you'll want to use set_ticks(positions, labels=labels)
instead.
If you are using this method, you should always fix the tick positions before, e.g. by using Colorbar.set_ticks
or by explicitly setting a FixedLocator
on the long axis of the colorbar. Otherwise, ticks are free to move and the labels may end up in unexpected positions.
Text
Texts for labeling each tick location in the sequence set by Colorbar.set_ticks
; the number of labels must match the number of locations.
This keyword argument is ignored and will be be removed. Deprecated
If True, set minor ticks instead of major ticks.
Text
properties for the labels.
Set tick locations.
List of tick locations.
List of tick labels. If not set, the labels show the data value.
If False
, set the major ticks; if True
, the minor ticks.
Text
properties for the labels. These take effect only if you pass labels. In other cases, please use tick_params
.
Update solid patches, lines, etc.
This is meant to be called when the norm of the image or contour plot to which this colorbar belongs changes.
If the norm on the mappable is different than before, this resets the locator and formatter for the axis, so if these have been customized, they will need to be customized again. However, if the norm only changes values of vmin, vmax or cmap then the old formatter and locator will be preserved.
Setup the ticks and ticklabels. This should not be needed by users.
alias of matplotlib.colorbar.Colorbar
Bases: matplotlib.colorbar.Colorbar
[Deprecated]
Deprecated since version 3.4:
[Deprecated] Create a colorbar on the given axes for the given mappable.
Note
This is a low-level function to turn an existing axes into a colorbar axes. Typically, you'll want to use colorbar
instead, which automatically handles creation and placement of a suitable axes as well.
Axes
The Axes
to turn into a colorbar.
ScalarMappable
The mappable to be described by the colorbar.
Keyword arguments are passed to the respective colorbar class.
Colorbar
The created colorbar instance.
Deprecated since version 3.4.
Create an Axes
suitable for a colorbar.
The axes is placed in the figure of the parents axes, by resizing and repositioning parents.
Axes
or list of Axes
The Axes to use as parents for placing the colorbar.
The location, relative to the parent axes, where the colorbar axes is created. It also determines the orientation of the colorbar (colorbars on the left and right are vertical, colorbars at the top and bottom are horizontal). If None, the location will come from the orientation if it is set (vertical colorbars on the right, horizontal ones at the bottom), or default to 'right' if orientation is unset.
The orientation of the colorbar. It is preferable to set the location of the colorbar, as that also determines the orientation; passing incompatible values for location and orientation raises an exception.
Fraction of original axes to use for colorbar.
Fraction by which to multiply the size of the colorbar.
Ratio of long to short dimensions.
Axes
The child axes.
The reduced keyword dictionary to be passed when creating the colorbar instance.
Fraction of original axes between colorbar and new image axes.
The anchor point of the colorbar axes. Defaults to (0.0, 0.5) if vertical; (0.5, 1.0) if horizontal.
The anchor point of the colorbar parent axes. If False, the parent axes' anchor will be unchanged. Defaults to (1.0, 0.5) if vertical; (0.5, 0.0) if horizontal.
Create a SubplotBase
suitable for a colorbar.
The axes is placed in the figure of the parent axes, by resizing and repositioning parent.
This function is similar to make_axes
. Primary differences are
make_axes_gridspec
should only be used with a SubplotBase
parent.make_axes
creates an Axes
; make_axes_gridspec
creates a SubplotBase
.make_axes
updates the position of the parent. make_axes_gridspec
replaces the grid_spec
attribute of the parent with a new one.While this function is meant to be compatible with make_axes
, there could be some minor differences.
Axes
The Axes to use as parent for placing the colorbar.
The location, relative to the parent axes, where the colorbar axes is created. It also determines the orientation of the colorbar (colorbars on the left and right are vertical, colorbars at the top and bottom are horizontal). If None, the location will come from the orientation if it is set (vertical colorbars on the right, horizontal ones at the bottom), or default to 'right' if orientation is unset.
The orientation of the colorbar. It is preferable to set the location of the colorbar, as that also determines the orientation; passing incompatible values for location and orientation raises an exception.
Fraction of original axes to use for colorbar.
Fraction by which to multiply the size of the colorbar.
Ratio of long to short dimensions.
SubplotBase
The child axes.
The reduced keyword dictionary to be passed when creating the colorbar instance.
Fraction of original axes between colorbar and new image axes.
The anchor point of the colorbar axes. Defaults to (0.0, 0.5) if vertical; (0.5, 1.0) if horizontal.
The anchor point of the colorbar parent axes. If False, the parent axes' anchor will be unchanged. Defaults to (1.0, 0.5) if vertical; (0.5, 0.0) if horizontal.
© 2012–2021 Matplotlib Development Team. All rights reserved.
Licensed under the Matplotlib License Agreement.
https://matplotlib.org/3.5.1/api/colorbar_api.html