matplotlib.colorbar¶
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 use make_axes and Colorbar
internally.
As an end-user, you most likely won't have to call the methods or instantiate the classes in this module explicitly.
ColorbarBase- The base class with full colorbar drawing functionality. It can be used as-is to make a colorbar for a given colormap; a mappable object (e.g., image) is not needed.
Colorbar- On top of
ColorbarBasethis connects the colorbar with aScalarMappablesuch as an image or contour plot. ColorbarPatch- A specialized
Colorbarto support hatched contour plots. make_axes()- Create an
Axessuitable for a colorbar. This functions can be used with figures containing a single axes or with freely placed axes. make_axes_gridspec()- Create a
SubplotBasesuitable for a colorbar. This function should be used for adding a colorbar to aGridSpec.
-
class
matplotlib.colorbar.Colorbar(ax, mappable, **kwargs)[source]¶ Bases:
matplotlib.colorbar.ColorbarBaseThis class connects a
ColorbarBaseto aScalarMappablesuch as anAxesImagegenerated viaimshow.Note
This class is not intended to be instantiated directly; instead, use
Figure.colorbarorpyplot.colorbarto create a colorbar.-
add_lines(self, CS, erase=True)[source]¶ Add the lines from a non-filled
ContourSetto the colorbar.Parameters: - CS
ContourSet The line positions are taken from the ContourSet levels. The ContourSet must not be filled.
- erasebool, default: True
Whether to remove any previously added lines.
- CS
-
on_mappable_changed(self, mappable)[source]¶ [Deprecated] Update this colorbar to match the mappable's properties.
Typically this is automatically registered as an event handler by
colorbar_factory()and should not be called manually.Notes
Deprecated since version 3.3.
-
remove(self)[source]¶ Remove this colorbar from the figure.
If the colorbar was created with
use_gridspec=Truethe previous gridspec is restored.
-
update_bruteforce(self, mappable)[source]¶ [Deprecated] Destroy and rebuild the colorbar. This is intended to become obsolete, and will probably be deprecated and then removed. It is not called when the pyplot.colorbar function or the Figure.colorbar method are used to create the colorbar.
Notes
Deprecated since version 3.3.
-
update_normal(self, mappable)[source]¶ 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.
-
-
class
matplotlib.colorbar.ColorbarBase(ax, *, cmap=None, norm=None, alpha=None, values=None, boundaries=None, orientation='vertical', ticklocation='auto', extend=None, spacing='uniform', ticks=None, format=None, drawedges=False, filled=True, extendfrac=None, extendrect=False, label='')[source]¶ Bases:
objectDraw a colorbar in an existing axes.
There are only some rare cases in which you would work directly with a
ColorbarBaseas an end-user. Typically, colorbars are used withScalarMappables such as anAxesImagegenerated viaimshow. For these cases you will useColorbarand likely create it viapyplot.colorbarorFigure.colorbar.The main application of using a
ColorbarBaseexplicitly is drawing colorbars that are not associated with other elements in the figure, e.g. when showing a colormap by itself.If the cmap kwarg is given but boundaries and values are left as None, then the colormap will be displayed on a 0-1 scale. To show the under- and over-value colors, specify the norm as:
norm=colors.Normalize(clip=False)
To show the colors versus index instead of on the 0-1 scale, use:
norm=colors.NoNorm()
Useful public methods are
set_label()andadd_lines().Parameters: - ax
Axes The
Axesinstance in which the colorbar is drawn.- cmap
Colormap, default:rcParams["image.cmap"](default:'viridis') The colormap to use.
- norm
Normalize - alphafloat
The colorbar transparency between 0 (transparent) and 1 (opaque).
- values
- boundaries
- orientation{'vertical', 'horizontal'}
- ticklocation{'auto', 'left', 'right', 'top', 'bottom'}
- extend{'neither', 'both', 'min', 'max'}
- spacing{'uniform', 'proportional'}
- ticks
Locatoror array-like of float - formatstr or
Formatter - drawedgesbool
- filledbool
- extendfrac
- extendrec
- labelstr
Attributes: - ax
Axes The
Axesinstance in which the colorbar is drawn.- lineslist
A list of
LineCollectionif lines were drawn, otherwise an empty list.- dividers
LineCollection A LineCollection if drawedges is
True, otherwiseNone.
-
add_lines(self, levels, colors, linewidths, erase=True)[source]¶ Draw lines on the colorbar.
The lines are appended to the list
lines.Parameters: - levelsarray-like
The positions of the lines.
- colorscolor or list of colors
Either a single color applying to all lines or one color value for each line.
- linewidthsfloat or array-like
Either a single linewidth applying to all lines or one linewidth for each line.
- erasebool, default: True
Whether to remove any previously added lines.
-
draw_all(self)[source]¶ Calculate any free parameters based on the current cmap and norm, and do all the drawing.
-
minorticks_on(self)[source]¶ Turn the minor ticks of the colorbar on without extruding into the "extend regions".
-
n_rasterize= 50¶
-
set_label(self, label, *, loc=None, **kwargs)[source]¶ Add a label to the long axis of the colorbar.
-
set_ticklabels(self, ticklabels, update_ticks=True)[source]¶ Set tick labels.
Tick labels are updated immediately unless update_ticks is False, in which case one should call
update_ticksexplicitly.
-
set_ticks(self, ticks, update_ticks=True)[source]¶ Set tick locations.
Parameters: - ticksarray-like or
Locatoror None The tick positions can be hard-coded by an array of values; or they can be defined by a
Locator. Setting to None reverts to using a default locator.- update_ticksbool, default: True
If True, tick locations are updated immediately. If False, the user has to call
update_tickslater to update the ticks.
- ticksarray-like or
- ax
-
class
matplotlib.colorbar.ColorbarPatch(ax, mappable, **kw)[source]¶ Bases:
matplotlib.colorbar.ColorbarA Colorbar that uses a list of
Patchinstances rather than the defaultPatchCollectioncreated bypcolor, because the latter does not allow the hatch pattern to vary among the members of the collection.
-
matplotlib.colorbar.colorbar_factory(cax, mappable, **kwargs)[source]¶ 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
colorbarinstead, which automatically handles creation and placement of a suitable axes as well.Parameters: - cax
Axes The
Axesto turn into a colorbar.- mappable
ScalarMappable The mappable to be described by the colorbar.
- **kwargs
Keyword arguments are passed to the respective colorbar class.
Returns: ColorbarorColorbarPatchThe created colorbar instance.
ColorbarPatchis only used if mappable is aContourSetwith hatches.
- cax
-
matplotlib.colorbar.make_axes(parents, location=None, orientation=None, fraction=0.15, shrink=1.0, aspect=20, **kw)[source]¶ Create an
Axessuitable for a colorbar.The axes is placed in the figure of the parents axes, by resizing and repositioning parents.
Parameters: - parents
Axesor list ofAxes The Axes to use as parents for placing the colorbar.
- locationNone or {'left', 'right', 'top', 'bottom'}
The position, relative to parents, where the colorbar axes should be created. If None, the value will either come from the given
orientation, else it will default to 'right'.- orientationNone or {'vertical', 'horizontal'}
The orientation of the colorbar. Typically, this keyword shouldn't be used, as it can be derived from the
locationkeyword.- fractionfloat, default: 0.15
Fraction of original axes to use for colorbar.
- shrinkfloat, default: 1.0
Fraction by which to multiply the size of the colorbar.
- aspectfloat, default: 20
Ratio of long to short dimensions.
Returns: - cax
Axes The child axes.
- kwdict
The reduced keyword dictionary to be passed when creating the colorbar instance.
Other Parameters: - padfloat, default: 0.05 if vertical, 0.15 if horizontal
Fraction of original axes between colorbar and new image axes.
- anchor(float, float), optional
The anchor point of the colorbar axes. Defaults to (0.0, 0.5) if vertical; (0.5, 1.0) if horizontal.
- panchor(float, float), or False, optional
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.
- parents
-
matplotlib.colorbar.make_axes_gridspec(parent, *, fraction=0.15, shrink=1.0, aspect=20, **kw)[source]¶ Create a
SubplotBasesuitable 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 aremake_axes_gridspeconly handles the orientation keyword and cannot handle the location keyword.make_axes_gridspecshould only be used with aSubplotBaseparent.make_axescreates anAxes;make_axes_gridspeccreates aSubplotBase.make_axesupdates the position of the parent.make_axes_gridspecreplaces thegrid_specattribute of the parent with a new one.
While this function is meant to be compatible with
make_axes, there could be some minor differences.Parameters: - parent
Axes The Axes to use as parent for placing the colorbar.
- fractionfloat, default: 0.15
Fraction of original axes to use for colorbar.
- shrinkfloat, default: 1.0
Fraction by which to multiply the size of the colorbar.
- aspectfloat, default: 20
Ratio of long to short dimensions.
Returns: - cax
SubplotBase The child axes.
- kwdict
The reduced keyword dictionary to be passed when creating the colorbar instance.
Other Parameters: - orientation{'vertical', 'horizontal'}, default: 'vertical'
The orientation of the colorbar.
- padfloat, default: 0.05 if vertical, 0.15 if horizontal
Fraction of original axes between colorbar and new image axes.
- anchor(float, float), optional
The anchor point of the colorbar axes. Defaults to (0.0, 0.5) if vertical; (0.5, 1.0) if horizontal.
- panchor(float, float), or False, optional
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.