Note
Go to the endto download the full example code.
Customized Colorbars Tutorial#
This tutorial shows how to build and customize standalone colorbars, i.e.without an attached plot.
Acolorbar requires amatplotlib.colorizer.ColorizingArtist whichcontains amatplotlib.colorizer.Colorizer that holds the data-to-color pipeline(norm and colormap). To create a colorbar without an attached plot one candirectly instantiate the base classColorizingArtist, which has no associateddata.
importmatplotlib.pyplotaspltimportmatplotlibasmpl
Basic continuous colorbar#
Here, we create a basic continuous colorbar with ticks and labels.
The arguments to thecolorbar call are aColorizingArtist,the axes where the colorbar should be drawn, and the colorbar's orientation.To crate aColorizingArtist one must first makeColorizer that holds thedesirednorm andcmap.
For more information see thecolorbar API.
fig,ax=plt.subplots(figsize=(6,1),layout='constrained')norm=mpl.colors.Normalize(vmin=5,vmax=10)colorizer=mpl.colorizer.Colorizer(norm=norm,cmap="cool")fig.colorbar(mpl.colorizer.ColorizingArtist(colorizer),cax=ax,orientation='horizontal',label='Some Units')

Colorbar attached next to a pre-existing axes#
All examples in this tutorial (except this one) show a standalone colorbar onits own figure, but it is possible to display the colorbarnext to apre-existing Axesax by passingax=ax to the colorbar() call (meaning"draw the colorbar next toax") rather thancax=ax (meaning "draw thecolorbar onax").
fig,ax=plt.subplots(layout='constrained')colorizer=mpl.colorizer.Colorizer(norm=mpl.colors.Normalize(0,1),cmap='magma')fig.colorbar(mpl.colorizer.ColorizingArtist(colorizer),ax=ax,orientation='vertical',label='a colorbar label')

Discrete and extended colorbar with continuous colorscale#
The following example shows how to make a discrete colorbar based on acontinuous cmap. We usematplotlib.colors.BoundaryNorm to describe theinterval boundaries (which must be in increasing order), and further pass theextend argument to it to further display "over" and "under" colors (whichare used for data outside of the norm range).
fig,ax=plt.subplots(figsize=(6,1),layout='constrained')cmap=mpl.colormaps["viridis"]bounds=[-1,2,5,7,12,15]norm=mpl.colors.BoundaryNorm(bounds,cmap.N,extend='both')colorizer=mpl.colorizer.Colorizer(norm=norm,cmap='viridis')fig.colorbar(mpl.colorizer.ColorizingArtist(colorizer),cax=ax,orientation='horizontal',label="Discrete intervals with extend='both' keyword")

Colorbar with arbitrary colors#
The following example still uses aBoundaryNorm to describe discreteinterval boundaries, but now uses amatplotlib.colors.ListedColormap toassociate each interval with an arbitrary color (there must be as manyintervals than there are colors).
We also pass additional arguments tocolorbar:
To display the out-of-range values on the colorbar, we use theextendargument in the colorbar() call. (This is equivalent to passing theextend argument in the
BoundaryNormconstructor as done in theprevious example.)To make the length of each colorbar segment proportional to itscorresponding interval, we use thespacing argument in the colorbar()call.
fig,ax=plt.subplots(figsize=(6,1),layout='constrained')cmap=mpl.colors.ListedColormap(['red','green','blue','cyan'],under='yellow',over='magenta')bounds=[1,2,4,7,8]norm=mpl.colors.BoundaryNorm(bounds,cmap.N)colorizer=mpl.colorizer.Colorizer(norm=norm,cmap=cmap)fig.colorbar(mpl.colorizer.ColorizingArtist(colorizer),cax=ax,orientation='horizontal',extend='both',spacing='proportional',label='Discrete intervals, some other units',)

Colorbar with custom extension lengths#
We can customize the length colorbar extensions, on a colorbar with discreteintervals. To make the length of each extension thesame as the length of the interior colors, useextendfrac='auto'.
fig,ax=plt.subplots(figsize=(6,1),layout='constrained')cmap=mpl.colors.ListedColormap(['royalblue','cyan','yellow','orange'],over='red',under='blue')bounds=[-1.0,-0.5,0.0,0.5,1.0]norm=mpl.colors.BoundaryNorm(bounds,cmap.N)colorizer=mpl.colorizer.Colorizer(norm=norm,cmap=cmap)fig.colorbar(mpl.colorizer.ColorizingArtist(colorizer),cax=ax,orientation='horizontal',extend='both',extendfrac='auto',spacing='uniform',label='Custom extension lengths, some other units',)plt.show()

Total running time of the script: (0 minutes 1.130 seconds)