Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit a494587

Browse files
committed
Doc: use empty ScalarMappable for colorbars with no associated image.
This is consistent with the docstring of colorbar(), which explicitly suggests this approach; this would also later allow deprecating ColorbarBase, merging it into Colorbar.
1 parent 9984f9c commit a494587

2 files changed

Lines changed: 50 additions & 60 deletions

File tree

examples/specialty_plots/leftventricle_bulleye.py

Lines changed: 15 additions & 27 deletions
Original file line numberDiff line numberDiff line change
@@ -147,29 +147,18 @@ def bullseye_plot(ax, data, seg_bold=None, cmap=None, norm=None):
147147
# the colorbar will be used.
148148
cmap = mpl.cm.viridis
149149
norm = mpl.colors.Normalize(vmin=1, vmax=17)
150-
151-
# ColorbarBase derives from ScalarMappable and puts a colorbar
152-
# in a specified axes, so it has everything needed for a
153-
# standalone colorbar. There are many more kwargs, but the
154-
# following gives a basic continuous colorbar with ticks
155-
# and labels.
156-
cb1 = mpl.colorbar.ColorbarBase(axl, cmap=cmap, norm=norm,
157-
orientation='horizontal')
150+
# Create an empty ScalarMappable to set the colorbar's colormap and norm.
151+
# The following gives a basic continuous colorbar with ticks and labels.
152+
cb1 = fig.colorbar(mpl.cm.ScalarMappable(cmap=cmap, norm=norm),
153+
cax=axl, orientation='horizontal')
158154
cb1.set_label('Some Units')
159155

160156

161-
# Set the colormap and norm to correspond to the data for which
162-
# the colorbar will be used.
157+
# And again for the second colorbar.
163158
cmap2 = mpl.cm.cool
164159
norm2 = mpl.colors.Normalize(vmin=1, vmax=17)
165-
166-
# ColorbarBase derives from ScalarMappable and puts a colorbar
167-
# in a specified axes, so it has everything needed for a
168-
# standalone colorbar. There are many more kwargs, but the
169-
# following gives a basic continuous colorbar with ticks
170-
# and labels.
171-
cb2 = mpl.colorbar.ColorbarBase(axl2, cmap=cmap2, norm=norm2,
172-
orientation='horizontal')
160+
cb2 = fig.colorbar(mpl.cm.ScalarMappable(cmap=cmap2, norm=norm2),
161+
cax=axl2, orientation='horizontal')
173162
cb2.set_label('Some other units')
174163

175164

@@ -179,20 +168,19 @@ def bullseye_plot(ax, data, seg_bold=None, cmap=None, norm=None):
179168
cmap3 = mpl.colors.ListedColormap(['r', 'g', 'b', 'c'])
180169
cmap3.set_over('0.35')
181170
cmap3.set_under('0.75')
182-
183171
# If a ListedColormap is used, the length of the bounds array must be
184172
# one greater than the length of the color list. The bounds must be
185173
# monotonically increasing.
186174
bounds = [2, 3, 7, 9, 15]
187175
norm3 = mpl.colors.BoundaryNorm(bounds, cmap3.N)
188-
cb3 = mpl.colorbar.ColorbarBase(axl3, cmap=cmap3, norm=norm3,
189-
# to use 'extend', you must
190-
# specify two extra boundaries:
191-
boundaries=[0] + bounds + [18],
192-
extend='both',
193-
ticks=bounds, # optional
194-
spacing='proportional',
195-
orientation='horizontal')
176+
cb3 = fig.colorbar(mpl.cm.ScalarMappable(cmap=cmap3, norm=norm3),
177+
cax=axl3,
178+
# to use 'extend', you must specify two extra boundaries:
179+
boundaries=[0] + bounds + [18],
180+
extend='both',
181+
ticks=bounds, # optional
182+
spacing='proportional',
183+
orientation='horizontal')
196184
cb3.set_label('Discrete intervals, some other units')
197185

198186

tutorials/colors/colorbar_only.py

Lines changed: 35 additions & 33 deletions
Original file line numberDiff line numberDiff line change
@@ -3,25 +3,27 @@
33
Customized Colorbars Tutorial
44
=============================
55
6-
This tutorial shows how to build colorbars without an attached plot.
6+
This tutorial shows how to build and customize standalone colorbars, i.e.
7+
without an attached plot.
78
89
Customized Colorbars
910
====================
1011
11-
`~matplotlib.colorbar.ColorbarBase` puts a colorbar in a specified axes,
12-
and can make a colorbar for a given colormap; it does not need a mappable
13-
object like an image. In this tutorial we will explore what can be done with
14-
standalone colorbar.
12+
A `~.Figure.colorbar` needs a "mappable" (`matplotlib.cm.ScalarMappable`)
13+
object (typically, an image) which indicates the colormap and the norm to be
14+
used. In order to create a colorbar without an attached image, one can instead
15+
use a `.ScalarMappable` with no associated data.
1516
1617
Basic continuous colorbar
1718
-------------------------
1819
19-
Set the colormap and norm to correspond to the data for which the colorbar
20-
will be used. Then create the colorbar by calling
21-
:class:`~matplotlib.colorbar.ColorbarBase` and specify axis, colormap, norm
22-
and orientation as parameters. Here we create a basic continuous colorbar
23-
with ticks and labels. For more information see the
24-
:mod:`~matplotlib.colorbar` API.
20+
Here we create a basic continuous colorbar with ticks and labels.
21+
22+
The arguments to the `~.Figure.colorbar` call are the `.ScalarMappable`
23+
(constructed using the *norm* and *cmap* arguments), the axes where the
24+
colorbar should be drawn, and the colorbar's orientation.
25+
26+
For more information see the :mod:`~matplotlib.colorbar` API.
2527
"""
2628

2729
import matplotlib.pyplot as plt
@@ -33,11 +35,9 @@
3335
cmap = mpl.cm.cool
3436
norm = mpl.colors.Normalize(vmin=5, vmax=10)
3537

36-
cb1 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
37-
norm=norm,
38-
orientation='horizontal')
38+
cb1 = fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
39+
cax=ax, orientation='horizontal')
3940
cb1.set_label('Some Units')
40-
fig.show()
4141

4242
###############################################################################
4343
# Discrete intervals colorbar
@@ -56,7 +56,7 @@
5656
# bounds must be monotonically increasing.
5757
#
5858
# This time we pass some more arguments in addition to previous arguments to
59-
# :class:`~matplotlib.colorbar.ColorbarBase`. For the out-of-range values to
59+
# `~.Figure.colorbar`. For the out-of-range values to
6060
# display on the colorbar, we have to use the *extend* keyword argument. To use
6161
# *extend*, you must specify two extra boundaries. Finally spacing argument
6262
# ensures that intervals are shown on colorbar proportionally.
@@ -70,15 +70,15 @@
7070

7171
bounds = [1, 2, 4, 7, 8]
7272
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
73-
cb2 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
74-
norm=norm,
75-
boundaries=[0] + bounds + [13],
76-
extend='both',
77-
ticks=bounds,
78-
spacing='proportional',
79-
orientation='horizontal')
73+
cb2 = fig.colorbar(
74+
mpl.cm.ScalarMappable(cmap=cmap, norm=norm),
75+
cax=ax,
76+
boundaries=[0] + bounds + [13],
77+
extend='both',
78+
ticks=bounds,
79+
spacing='proportional',
80+
orientation='horizontal')
8081
cb2.set_label('Discrete intervals, some other units')
81-
fig.show()
8282

8383
###############################################################################
8484
# Colorbar with custom extension lengths
@@ -98,13 +98,15 @@
9898

9999
bounds = [-1.0, -0.5, 0.0, 0.5, 1.0]
100100
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
101-
cb3 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
102-
norm=norm,
103-
boundaries=[-10] + bounds + [10],
104-
extend='both',
105-
extendfrac='auto',
106-
ticks=bounds,
107-
spacing='uniform',
108-
orientation='horizontal')
101+
cb3 = fig.colorbar(
102+
mpl.cm.ScalarMappable(cmap=cmap, norm=norm),
103+
cax=ax,
104+
boundaries=[-10] + bounds + [10],
105+
extend='both',
106+
extendfrac='auto',
107+
ticks=bounds,
108+
spacing='uniform',
109+
orientation='horizontal')
109110
cb3.set_label('Custom extension lengths, some other units')
110-
fig.show()
111+
112+
plt.show()

0 commit comments

Comments
 (0)