8000 Merge pull request #16037 from anntzer/colorbarbaseless · matplotlib/matplotlib@9c2ad59 · GitHub
[go: up one dir, main page]

Skip to content
< 8000 header class="HeaderMktg header-logged-out js-details-container js-header Details f4 py-3" role="banner" data-is-top="true" data-color-mode=light data-light-theme=light data-dark-theme=dark>

Navigation Menu

Sign in
Appearance settings

Search code, repositories, users, issues, pull requests...

Provide feedback

We read every piece of feedback, and take your input very seriously.

Saved searches

Use saved searches to filter your results more quickly

Appearance settings

Commit 9c2ad59

Browse files
authored
Merge pull request 8000 #16037 from anntzer/colorbarbaseless
Doc: use empty ScalarMappable for colorbars with no associated image.
2 parents cc2677e + 000c193 commit 9c2ad59

File tree

2 files changed

+55
-66
lines changed

2 files changed

+55
-66
lines changed

examples/specialty_plots/leftventricle_bulleye.py

Lines changed: 16 additions & 30 deletions
Original file line numberDiff line numberDiff line change
@@ -147,30 +147,17 @@ 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+
# 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+
fig.colorbar(mpl.cm.ScalarMappable(cmap=cmap, norm=norm),
153+
cax=axl, orientation='horizontal', label='Some Units')
150154

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')
158-
cb1.set_label('Some Units')
159155

160-
161-
# Set the colormap and norm to correspond to the data for which
162-
# the colorbar will be used.
156+
# And again for the second colorbar.
163157
cmap2 = mpl.cm.cool
164158
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')
173-
cb2.set_label('Some other units')
159+
fig.colorbar(mpl.cm.ScalarMappable(cmap=cmap2, norm=norm2),
160+
cax=axl2, orientation='horizontal', label='Some other units')
174161

175162

176163
# The second example illustrates the use of a ListedColormap, a
@@ -179,21 +166,20 @@ def bullseye_plot(ax, data, seg_bold=None, cmap=None, norm=None):
179166
cmap3 = mpl.colors.ListedColormap(['r', 'g', 'b', 'c'])
180167
cmap3.set_over('0.35')
181168
cmap3.set_under('0.75')
182-
183169
# If a ListedColormap is used, the length of the bounds array must be
8000
184170
# one greater than the length of the color list. The bounds must be
185171
# monotonically increasing.
186172
bounds = [2, 3, 7, 9, 15]
187173
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')
196-
cb3.set_label('Discrete intervals, some other units')
174+
fig.colorbar(mpl.cm.ScalarMappable(cmap=cmap3, norm=norm3),
175+
cax=axl3,
176+
# to use 'extend', you must specify two extra boundaries:
177+
boundaries=[0] + bounds + [18],
178+
extend='both',
179+
ticks=bounds, # optional
180+
spacing='proportional',
181+
orientation='horizontal',
182+
label='Discrete intervals, some other units')
197183

198184

199185
# Create the 17 segment model

tutorials/colors/colorbar_only.py

Lines changed: 39 additions & 36 deletions
7287
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,8 @@
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')
39-
cb1.set_label('Some Units')
40-
fig.show()
38+
fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
39+
cax=ax, orientation='horizontal', label='Some Units')
4140

4241
###############################################################################
4342
# Discrete intervals colorbar
@@ -56,7 +55,7 @@
5655
# bounds must be monotonically increasing.
5756
#
5857
# 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
58+
# `~.Figure.colorbar`. For the out-of-range values to
6059
# display on the colorbar, we have to use the *extend* keyword argument. To use
6160
# *extend*, you must specify two extra boundaries. Finally spacing argument
6261
# ensures that intervals are shown on colorbar proportionally.
@@ -70,15 +69,16 @@
7069

7170
bounds = [1, 2, 4, 7, 8]
7271
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')
80-
cb2.set_label('Discrete intervals, some other units')
81-
fig.show()
72+
fig.colorbar(
73+
mpl.cm.ScalarMappable(cmap=cmap, norm=norm),
74+
cax=ax,
75+
boundaries=[0] + bounds + [13],
76+
extend='both',
77+
ticks=bounds,
78+
spacing='proportional',
79+
orientation='horizontal',
80+
label='Discrete intervals, some other units',
81+
)
8282

8383
###############################################################################
8484
# Colorbar with custom extension lengths
@@ -98,13 +98,16 @@
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')
109-
cb3.set_label('Custom extension lengths, some other units')
110-
fig.show()
101+
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',
110+
label='Custom extension lengths, some other units',
111+
)
112+
113+
plt.show()

0 commit comments

Comments
 (0)
0