
matplotlib.colorbar¶Colorbar toolkit with two classes and a function:
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- the derived class for use with images or contour plots.
make_axes()- a function for resizing an axes and adding a second axessuitable for a colorbar
Thecolorbar() method usesmake_axes()andColorbar; thecolorbar() functionis a thin wrapper overcolorbar().
matplotlib.colorbar.Colorbar(ax,mappable,**kw)¶Bases:matplotlib.colorbar.ColorbarBase
This class connects aColorbarBase to aScalarMappable such as aAxesImage generated viaimshow().
It is not intended to be instantiated directly; instead,usecolorbar() orcolorbar() to make your colorbar.
add_lines(CS,erase=True)¶Add the lines from a non-filledContourSet to the colorbar.
Seterase to False if these lines should be added toany pre-existing lines.
on_mappable_changed(mappable)¶Updates this colorbar to match the mappable’s properties.
Typically this is automatically registered as an event handlerbycolorbar_factory() and should not be called manually.
remove()¶Remove this colorbar from the figure. If the colorbar was created withuse_gridspec=True then restore the gridspec to its previous value.
update_bruteforce(mappable)¶Destroy and rebuild the colorbar. This isintended to become obsolete, and will probably bedeprecated and then removed. It is not called whenthe pyplot.colorbar function or the Figure.colorbarmethod are used to create the colorbar.
update_normal(mappable)¶update solid, lines, etc. Unlike update_bruteforce, it doesnot clear the axes. This is meant to be called when the imageor contour plot to which this colorbar belongs is changed.
matplotlib.colorbar.ColorbarBase(ax,cmap=None,norm=None,alpha=None,values=None,boundaries=None,orientation='vertical',ticklocation='auto',extend='neither',spacing='uniform',ticks=None,format=None,drawedges=False,filled=True,extendfrac=None,extendrect=False,label='')¶Bases:matplotlib.cm.ScalarMappable
Draw a colorbar in an existing axes.
This is a base class for theColorbar class, which is thebasis for thecolorbar() function and thecolorbar() method, which are theusual ways of creating a colorbar.
It is also useful by itself for showing a colormap. If thecmapkwarg is given butboundaries andvalues are left as None,then the colormap will be displayed on a 0-1 scale. To show theunder- and over-value colors, specify thenorm as:
colors.Normalize(clip=False)
To show the colors versus index instead of on the 0-1 scale,use:
norm=colors.NoNorm.
Useful attributes:
ax- the Axes instance in which the colorbar is drawn
lines- a list of LineCollection if lines were drawn, otherwisean empty list
dividers- a LineCollection ifdrawedges is True, otherwise None
Useful public methods areset_label() andadd_lines().
add_lines(levels,colors,linewidths,erase=True)¶Draw lines on the colorbar.
colors andlinewidths must be scalars orsequences the same length aslevels.
Seterase to False to add lines without firstremoving any previously added lines.
ax = None¶The axes that this colorbar lives in.
config_axis()¶draw_all()¶Calculate any free parameters based on the current cmap and norm,and do all the drawing.
n_rasterize = 50¶remove()¶Remove this colorbar from the figure
set_alpha(alpha)¶set_label(label,**kw)¶Label the long axis of the colorbar
set_ticklabels(ticklabels,update_ticks=True)¶set tick labels. Tick labels are updated immediately unlessupdate_ticks isFalse. To manually update the ticks, callupdate_ticks method explicitly.
set_ticks(ticks,update_ticks=True)¶set tick locations. Tick locations are updated immediately unlessupdate_ticks isFalse. To manually update the ticks, callupdate_ticks method explicitly.
update_ticks()¶Force the update of the ticks and ticklabels. This must becalled whenever the tick locator and/or tick formatter changes.
matplotlib.colorbar.ColorbarPatch(ax,mappable,**kw)¶Bases:matplotlib.colorbar.Colorbar
A Colorbar which is created usingPatchrather than the defaultpcolor().
It uses a list of Patch instances instead of aPatchCollection because thelatter does not allow the hatch pattern to vary among themembers of the collection.
matplotlib.colorbar.colorbar_factory(cax,mappable,**kwargs)¶Creates a colorbar on the given axes for the given mappable.
Typically, for automatic colorbar placement given only a mappable usecolorbar().
matplotlib.colorbar.make_axes(parents,location=None,orientation=None,fraction=0.15,shrink=1.0,aspect=20,**kw)¶Resize and reposition parent axes, and return a childaxes suitable for a colorbar.
Keyword arguments may include the following (with defaults):
- location:[None|’left’|’right’|’top’|’bottom’]
- The position, relative toparents, where the colorbar axesshould be created. If None, the value will either come from thegiven
orientation, else it will default to ‘right’.- orientation:[None|’vertical’|’horizontal’]
- The orientation of the colorbar. Typically, this keyword shouldn’tbe used, as it can be derived from the
locationkeyword.
| Property | Description |
|---|---|
| orientation | vertical or horizontal |
| fraction | 0.15; fraction of original axes to use for colorbar |
| pad | 0.05 if vertical, 0.15 if horizontal; fractionof original axes between colorbar and new image axes |
| shrink | 1.0; fraction by which to shrink the colorbar |
| aspect | 20; ratio of long to short dimensions |
| anchor | (0.0, 0.5) if vertical; (0.5, 1.0) if horizontal;the anchor point of the colorbar axes |
| panchor | (1.0, 0.5) if vertical; (0.5, 0.0) if horizontal;the anchor point of the colorbar parent axes. IfFalse, the parent axes’ anchor will be unchanged |
Returns (cax, kw), the child axes and the reduced kw dictionary to bepassed when creating the colorbar instance.
matplotlib.colorbar.make_axes_gridspec(parent,**kw)¶Resize and reposition a parent axes, and return a child axessuitable for a colorbar. This function is similar tomake_axes. Prmary differences are
- make_axes_gridspec only handles theorientation keywordand cannot handle the “location” keyword.
- make_axes_gridspec should only be used with a subplot parent.
- make_axes creates an instance of Axes.make_axes_gridspec
- creates an instance of Subplot.
- make_axes updates the position of the
- parent.make_axes_gridspec replaces the grid_spec attributeof the parent with a new one.
While this function is meant to be compatible withmake_axes,there could be some minor differences.
Keyword arguments may include the following (with defaults):
- orientation
- ‘vertical’ or ‘horizontal’
| Property | Description |
|---|---|
| orientation | vertical or horizontal |
| fraction | 0.15; fraction of original axes to use for colorbar |
| pad | 0.05 if vertical, 0.15 if horizontal; fractionof original axes between colorbar and new image axes |
| shrink | 1.0; fraction by which to shrink the colorbar |
| aspect | 20; ratio of long to short dimensions |
| anchor | (0.0, 0.5) if vertical; (0.5, 1.0) if horizontal;the anchor point of the colorbar axes |
| panchor | (1.0, 0.5) if vertical; (0.5, 0.0) if horizontal;the anchor point of the colorbar parent axes. IfFalse, the parent axes’ anchor will be unchanged |
All but the first of these are stripped from the input kw set.
Returns (cax, kw), the child axes and the reduced kw dictionary to bepassed when creating the colorbar instance.