Skip to content

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 177ae32

Browse filesBrowse files
committed
Copy-edit the standalone colorbar tutorial
- The tutorial is mostly about making standalone colorbars, so use that as title. - Most users will still want the colorbar next to an existing axes, so explain how to do that. - "Extended colorbar with continuous colorscale" is first about discrete colorbars (extensions are a second point), so adjust the title; also it already needs to introduce BoundaryNorm. Adjust the next example's text accordingly as well, as it now only needs to introduce ListedColormap. - In that next example, describe the additional kwargs to colorbar() in a bullet list. - More consistently use fully qualified names the first time a new class is introduced, and unqualified names thereafter. - Use constrained layout instead of manually adjusting figure geometries. - Minor additional tweaks.
1 parent f05167c commit 177ae32
Copy full SHA for 177ae32

File tree

Expand file treeCollapse file tree

1 file changed

+63
-68
lines changed
Filter options
Expand file treeCollapse file tree

1 file changed

+63
-68
lines changed

‎galleries/users_explain/colors/colorbar_only.py

Copy file name to clipboardExpand all lines: galleries/users_explain/colors/colorbar_only.py
+63-68Lines changed: 63 additions & 68 deletions
Original file line numberDiff line numberDiff line change
@@ -1,56 +1,65 @@
11
"""
22
.. redirect-from:: /tutorials/colors/colorbar_only
33
4-
=============================
5-
Customized Colorbars Tutorial
6-
=============================
4+
====================
5+
Standalone colorbars
6+
====================
77
88
This tutorial shows how to build and customize standalone colorbars, i.e.
99
without an attached plot.
1010
11-
Customized Colorbars
12-
====================
13-
1411
A `~.Figure.colorbar` needs a "mappable" (`matplotlib.cm.ScalarMappable`)
1512
object (typically, an image) which indicates the colormap and the norm to be
1613
used. In order to create a colorbar without an attached image, one can instead
1714
use a `.ScalarMappable` with no associated data.
18-
19-
Basic continuous colorbar
20-
-------------------------
21-
22-
Here we create a basic continuous colorbar with ticks and labels.
23-
24-
The arguments to the `~.Figure.colorbar` call are the `.ScalarMappable`
25-
(constructed using the *norm* and *cmap* arguments), the axes where the
26-
colorbar should be drawn, and the colorbar's orientation.
27-
28-
For more information see the :mod:`~matplotlib.colorbar` API.
2915
"""
3016

3117
import matplotlib.pyplot as plt
32-
3318
import matplotlib as mpl
3419

35-
fig, ax = plt.subplots(figsize=(6, 1))
36-
fig.subplots_adjust(bottom=0.5)
20+
# %%
21+
# Basic continuous colorbar
22+
# -------------------------
23+
# Here, we create a basic continuous colorbar with ticks and labels.
24+
#
25+
# The arguments to the `~.Figure.colorbar` call are the `.ScalarMappable`
26+
# (constructed using the *norm* and *cmap* arguments), the axes where the
27+
# colorbar should be drawn, and the colorbar's orientation.
28+
#
29+
# For more information see the `~matplotlib.colorbar` API.
30+
31+
fig, ax = plt.subplots(figsize=(6, 1), layout='constrained')
3732

3833
cmap = mpl.cm.cool
3934
norm = mpl.colors.Normalize(vmin=5, vmax=10)
4035

4136
fig.colorbar(mpl.cm.ScalarMappable(norm=norm, cmap=cmap),
4237
cax=ax, orientation='horizontal', label='Some Units')
4338

39+
# %%
40+
# Colorbar attached next to a pre-existing axes
41+
# ---------------------------------------------
42+
# All examples in this tutorial (except this one) show a standalone colorbar on
43+
# its own figure, but it is possible to display the colorbar *next* to a
44+
# pre-existing Axes *ax* by passing ``ax=ax`` to the colorbar() call (meaning
45+
# "draw the colorbar next to *ax*") rather than ``cax=ax`` (meaning "draw the
46+
# colorbar on *ax*").
47+
48+
fig, ax = plt.subplots(layout='constrained')
49+
50+
fig.colorbar(mpl.cm.ScalarMappable(norm=mpl.colors.Normalize(0, 1), cmap='magma'),
51+
ax=ax, orientation='vertical', label='a colorbar label')
4452

4553
# %%
46-
# Extended colorbar with continuous colorscale
47-
# --------------------------------------------
48-
#
49-
# The second example shows how to make a discrete colorbar based on a
50-
# continuous cmap. With the "extend" keyword argument the appropriate colors
51-
# are chosen to fill the colorspace, including the extensions:
52-
fig, ax = plt.subplots(figsize=(6, 1))
53-
fig.subplots_adjust(bottom=0.5)
54+
# Discrete and extended colorbar with continuous colorscale
55+
# ---------------------------------------------------------
56+
# The following example shows how to make a discrete colorbar based on a
57+
# continuous cmap. We use `matplotlib.colors.BoundaryNorm` to describe the
58+
# interval boundaries (which must be in increasing order), and further pass the
59+
# *extend* argument to it to further display "over" and "under" colors (which
60+
# are used for data outside of the norm range).
61+
62+
fig, ax = plt.subplots(figsize=(6, 1), layout='constrained')
5463

5564
cmap = mpl.cm.viridis
5665
bounds = [-1, 2, 5, 7, 12, 15]
@@ -61,72 +70,58 @@
6170
label="Discrete intervals with extend='both' keyword")
6271

6372
# %%
64-
# Discrete intervals colorbar
65-
# ---------------------------
66-
#
67-
# The third example illustrates the use of a
68-
# :class:`~matplotlib.colors.ListedColormap` which generates a colormap from a
69-
# set of listed colors, `.colors.BoundaryNorm` which generates a colormap
70-
# index based on discrete intervals and extended ends to show the "over" and
71-
# "under" value colors. Over and under are used to display data outside of the
72-
# normalized [0, 1] range. Here we pass colors as gray shades as a string
73-
# encoding a float in the 0-1 range.
73+
# Colorbar with arbitrary colors
74+
# ------------------------------
75+
# The following example still uses a `.BoundaryNorm` to describe discrete
76+
# interval boundaries, but now uses a `matplotlib.colors.ListedColormap` to
77+
# associate each interval with an arbitrary color (there must be as many
78+
# intervals than there are colors). The "over" and "under" colors are set on
79+
# the colormap using `.Colormap.with_extremes`.
7480
#
75-
# If a :class:`~matplotlib.colors.ListedColormap` is used, the length of the
76-
# bounds array must be one greater than the length of the color list. The
77-
# bounds must be monotonically increasing.
81+
# We also pass additional arguments to `~.Figure.colorbar`:
7882
#
79-
# This time we pass additional arguments to
80-
# `~.Figure.colorbar`. For the out-of-range values to display on the colorbar
81-
# without using the *extend* keyword with
82-
# `.colors.BoundaryNorm`, we have to use the *extend* keyword argument directly
83-
# in the colorbar call. Here we also
84-
# use the spacing argument to make
85-
# the length of each colorbar segment proportional to its corresponding
86-
# interval.
87-
88-
fig, ax = plt.subplots(figsize=(6, 1))
89-
fig.subplots_adjust(bottom=0.5)
83+
# - To display the out-of-range values on the colorbar, we use the *extend*
84+
# argument in the colorbar() call. (This is equivalent to passing the
85+
# *extend* argument in the `.BoundaryNorm` constructor as done in the
86+
# previous example.)
87+
# - To make the length of each colorbar segment proportional to its
88+
# corresponding interval, we use the *spacing* argument in the colorbar()
89+
# call.
9090

91-
cmap = (mpl.colors.ListedColormap(['red', 'green', 'blue', 'cyan'])
92-
.with_extremes(over='0.25', under='0.75'))
91+
fig, ax = plt.subplots(figsize=(6, 1), layout='constrained')
9392

93+
cmap = (mpl.colors.ListedColormap(['red', 'green', 'blue', 'cyan'])
94+
.with_extremes(under='yellow', over='magenta'))
9495
bounds = [1, 2, 4, 7, 8]
9596
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
97+
9698
fig.colorbar(
9799
mpl.cm.ScalarMappable(cmap=cmap, norm=norm),
98-
cax=ax,
100+
cax=ax, orientation='horizontal',
99101
extend='both',
100-
ticks=bounds,
101102
spacing='proportional',
102-
orientation='horizontal',
103103
label='Discrete intervals, some other units',
104104
)
105105

106106
# %%
107107
# Colorbar with custom extension lengths
108108
# --------------------------------------
109-
#
110-
# Here we illustrate the use of custom length colorbar extensions, on a
111-
# colorbar with discrete intervals. To make the length of each extension the
109+
# We can customize the length colorbar extensions, on a colorbar with discrete
110+
# intervals. To make the length of each extension the
112111
# same as the length of the interior colors, use ``extendfrac='auto'``.
113112

114-
fig, ax = plt.subplots(figsize=(6, 1))
115-
fig.subplots_adjust(bottom=0.5)
113+
fig, ax = plt.subplots(figsize=(6, 1), layout='constrained')
116114

117115
cmap = (mpl.colors.ListedColormap(['royalblue', 'cyan', 'yellow', 'orange'])
118116
.with_extremes(over='red', under='blue'))
119-
120117
bounds = [-1.0, -0.5, 0.0, 0.5, 1.0]
121118
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
119+
122120
fig.colorbar(
123121
mpl.cm.ScalarMappable(cmap=cmap, norm=norm),
124-
cax=ax,
125-
extend='both',
126-
extendfrac='auto',
127-
ticks=bounds,
122+
cax=ax, orientation='horizontal',
123+
extend='both', extendfrac='auto',
128124
spacing='uniform',
129-
orientation='horizontal',
130125
label='Custom extension lengths, some other units',
131126
)
132127

0 commit comments

Comments
0 (0)
Morty Proxy This is a proxified and sanitized view of the page, visit original site.