Matplotlib colorbar() Function
Matplotlib Reference Documentation
colorbar() Used to add a color bar (color scale) to a chart, mapping colors to a range of values.
The color bar is an essential element used together with a colormap, allowing readers to map colors back to the original data values.
Function definition
matplotlib.pyplot.colorbar(mappable=None, cax=None, ax=None, **kwargs)
Figure.colorbar(mappable=None, cax=None, ax=None, use_gridspec=True,
**kwargs)
Parameter Description
| Parameters | Description |
|---|---|
| mappable | An object whose colors can be mapped, such as the return value of imshow(), scatter(), or contourf() (required) |
| ax | The Axes to which the color bar is attached (recommended, automatically adjusts the layout) |
| cax | A dedicated color bar Axes (used when fine control is needed) |
| shrink | The scaling ratio of the color bar relative to the Axes, default 1.0, commonly 0.8 |
| aspect | The aspect ratio of the color bar, default 20 |
| label | The label text of the color bar |
| orientation | 'vertical' (default) or 'horizontal' |
| ticks | Custom tick positions |
| extend | 'neither'/'both'/'min'/'max', color indication for out-of-range values |
| pad | The spacing between the color bar and the Axes |
Use
fig.colorbar(im, ax=ax)is the recommended way; it automatically adjusts the subplot layout to make room for the color bar.
Usage examples
Example 1: imshow + vertical color bar
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
data
=
np.
random
.
rand
(
10
,
10
)
fig
,
ax
=
plt.
subplots
(
figsize
=
(
6
,
5
)
,
layout
=
'constrained'
)
im
=
ax.
imshow
(
data
,
cmap
=
'viridis'
,
aspect
=
'auto'
)
Add a vertical color bar
cbar
=
fig.
colorbar
(
im
,
ax
=
ax
,
label
=
'Value'
,
shrink
=
0.8
)
ax.
set_title
(
'imshow with Colorbar'
)
ax.
set_xlabel
(
'Column'
)
ax.
set_ylabel
(
'Row'
)
plt.
show
(
)
Example 2: horizontal color bar + custom ticks
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
x
=
np.
linspace
(
3
,
3
,
100
)
y
=
np.
linspace
(
3
,
3
,
100
)
X
,
Y
=
np.
meshgrid
(
x
,
y
)
Z
=
np.
sin
(
X
)
- np.
cos
(
Y
)
fig
,
ax
=
plt.
subplots
(
figsize
=
(
7
,
5
)
,
layout
=
'constrained'
)
im
=
ax.
contourf
(
X
,
Y
,
Z
,
levels
=
15
,
cmap
=
'coolwarm'
)
Horizontal color bar (below the Axes)
cbar
=
fig.
colorbar
(
im
,
ax
=
ax
,
orientation
=
'horizontal'
,
label
=
'Amplitude'
,
shrink
=
0.8
,
pad
=
0.08
,
ticks
=
[
0.8
,
0.4
,
0
,
0.4
,
0.8
]
)
ax.
set_title
(
'Horizontal Colorbar'
)
ax.
set_xlabel
(
'X'
)
ax.
set_ylabel
(
'Y'
)
plt.
show
(
)
Example 3: scatter + color bar
Example
import
matplotlib.
pyplot
as
plt
import
numpy
as
np
np.
random
.
seed
(
42
)
n
=
100
x
=
np.
random
.
rand
(
n
)
10
y
=
np.
random
.
rand
(
n
)
10
values
=
np.
random
.
rand
(
n
)
100
The data to be color-mapped
fig
,
ax
=
plt.
subplots
(
figsize
=
(
7
,
5
)
,
layout
=
'constrained'
)
sc
=
ax.
scatter
(
x
,
y
,
c
=
values
,
cmap
=
'YlOrRd'
,
s
=
100
,
edgecolors
=
'white'
,
linewidth
=
0.5
)
The PathCollection returned by scatter can also be passed to colorbar
cbar
=
fig.
colorbar
(
sc
,
ax
=
ax
,
label
=
'Score'
,
shrink
=
0.8
)
ax.
set_title
(
'Scatter with Colorbar'
)
ax.
set_xlabel
(
'X'
)
ax.
set_ylabel
(
'Y'
)
plt.
show
(
)
Common questions
Color bar causes inconsistent subplot sizes?
Use layout='constrained' The parameter can handle this automatically.
Or call on the Figure fig.colorbar(im, ax=axes) Pass the list of all affected Axes.
How can multiple subplots share one color bar?
When associating one colorbar with multiple subplots, pass in ax=axes_list。