8000 Convert colorbar_only example to tutorial · matplotlib/matplotlib@f8d96be · GitHub
[go: up one dir, main page]

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 f8d96be

Browse files
committed
Convert colorbar_only example to tutorial
1 parent 74fa27b commit f8d96be

File tree

2 files changed

+175
-28
lines changed

2 files changed

+175
-28
lines changed

examples/api/colorbar_only.py

Lines changed: 63 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -1,60 +1,97 @@
1-
'''
2-
====================
3-
Customized colorbars
4-
====================
1+
"""
2+
=============================
3+
Customized Colorbars Tutorial
4+
=============================
55
6-
This example shows how to build colorbars without an attached mappable.
7-
'''
6+
This tutorial shows how to build colorbars without an attached mappable.
7+
8+
"""
9+
10+
###############################################################################
11+
# Customized Colorbars
12+
# ====================
13+
#
14+
# `matplotlib.colorbar.ColorbarBase` derives from `ScalarMappable` and puts a
15+
# colorbar in specified axes, it is the base class with standalone colorbar
16+
# drawing functionality. It can be used as-is to make a colorbar for a given
17+
# colormap and does not need a mappable object like an image. In this tutorial
18+
# we will explore what can be done with standalone colorbar.
19+
#
20+
# We will start by making a figure of desired size and adding axis at position
21+
# [left, bottom, width, height] where all quantities are in fractions of figure
22+
# width and height.
823

924
import matplotlib.pyplot as plt
1025
import matplotlib as mpl
1126

12-
# Make a figure and axes with dimensions as desired.
1327
fig = plt.figure(figsize=(8, 3))
1428
ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
1529
ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])
1630
ax3 = fig.add_axes([0.05, 0.15, 0.9, 0.15])
1731

18-
# Set the colormap and norm to correspond to the data for which
19-
# the colorbar will be used.
32+
###############################################################################
33+
# Basic continuous colorbar
34+
# -------------------------
35+
#
36+
# Set the colormap and norm to correspond to the data for which the colorbar
37+
# will be used. Then create the colorbar by calling `ColorbarBase` and
38+
# specify axis, colormap, norm and orientation as parameters. Here we create
39+
# a basic continuous colorbar with ticks and labels. There are many more kwargs
40+
# which can be used to further modify the colorbar.
41+
2042
cmap = mpl.cm.cool
2143
norm = mpl.colors.Normalize(vmin=5, vmax=10)
2244

23-
# ColorbarBase derives from ScalarMappable and puts a colorbar
24-
# in a specified axes, so it has everything needed for a
25-
# standalone colorbar. There are many more kwargs, but the
26-
# following gives a basic continuous colorbar with ticks
27-
# and labels.
2845
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap,
2946
norm=norm,
3047
orientation='horizontal')
3148
cb1.set_label('Some Units')
3249

33-
# The second example illustrates the use of a ListedColormap, a
34-
# BoundaryNorm, and extended ends to show the "over" and "under"
35-
# value colors.
50+
###############################################################################
51+
# Discrete intervals colorbar
52+
# ---------------------------
53+
#
54+
# The second example illustrates the use of a ListedColormap which generates
55+
# colormap from a set of listed colors, a BoundaryNorm which generates a
56+
# colormap index based on discrete interval and extended ends to show the
57+
# "over" and "under" value colors. Over and under are used to display data
58+
# outside of the normalized [0,1] range. Here we pass colors as gray shades as
59+
# a string encoding a float in the 0-1 range.
60+
#
61+
# If a ListedColormap is used, the length of the bounds array must be
62+
# one greater than the length of the color list. The bounds must be
63+
# monotonically increasing.
64+
#
65+
# This time we pass some more arguments in addition to previous arguments to
66+
# ColorBase. For the out-of-range values to display on the colorbar, we have to
67+
# use the extend keyword argument. To use 'extend', you must specify two extra
68+
# boundaries. Finally spacing argument ensures that intervals are shown on
69+
# colorbar proportionally.
70+
3671
cmap = mpl.colors.ListedColormap(['r', 'g', 'b', 'c'])
3772
cmap.set_over('0.25')
3873
cmap.set_under('0.75')
3974

40-
# If a ListedColormap is used, the length of the bounds array must be
41-
# one greater than the length of the color list. The bounds must be
42-
# monotonically increasing.
4375
bounds = [1, 2, 4, 7, 8]
4476
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
4577
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap,
4678
norm=norm,
47-
# to use 'extend', you must
48-
# specify two extra boundaries:
4979
boundaries=[0] + bounds + [13],
5080
extend='both',
51-
ticks=bounds, # optional
81+
ticks=bounds,
5282
spacing='proportional',
5383
orientation='horizontal')
5484
cb2.set_label('Discrete intervals, some other units')
5585

56-
# The third example illustrates the use of custom length colorbar
57-
# extensions, used on a colorbar with discrete intervals.
86+
###############################################################################
87+
# Colorbar with custom extension lengths
88+
# --------------------------------------
89+
#
90+
# Now in the third example we illustrate the use of custom length colorbar
91+
# extensions, used on a colorbar with discrete intervals. Here we pass colors
92+
# as RGB triplet. To make the length of each extension the same as the length
93+
# of the interior colors pass extendfrac argument as auto
94+
5895
cmap = mpl.colors.ListedColormap([[0., .4, 1.], [0., .8, 1.],
5996
[1., .8, 0.], [1., .4, 0.]])
6097
cmap.set_over((1., 0., 0.))
@@ -66,13 +103,11 @@
66103
norm=norm,
67104
boundaries=[-10] + bounds + [10],
68105
extend='both',
69-
# Make the length of each extension
70-
# the same as the length of the
71-
# interior colors:
72106
extendfrac='auto',
73107
ticks=bounds,
74108
spacing='uniform',
75109
orientation='horizontal')
76110
cb3.set_label('Custom extension lengths, some other units')
77111

78112
plt.show()
113+

tutorials/colors/colorbar_only.py

Lines changed: 112 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
"""
2+
=============================
3+
Customized Colorbars Tutorial
4+
=============================
5+
6+
This tutorial shows how to build colorbars without an attached mappable.
7+
8+
"""
9+
10+
###############################################################################
11+
# Customized Colorbars
12+
# ====================
13+
#
14+
# `matplotlib.colorbar.ColorbarBase` derives from `ScalarMappable` and puts a
15+
# colorbar in specified axes, it is the base class with standalone colorbar
16+
# drawing functionality. It can be used as-is to make a colorbar for a given
17+
# colormap and does not need a mappable object like an image. In this tutorial
18+
# we will explore what can be done with standalone colorbar.
19+
#
20+
# We will start by making a figure of desired size and adding axis at position
21+
# [left, bottom, width, height] where all quantities are in fractions of figure
22+
# width and height.
23+
24+
import matplotlib.pyplot as plt
25+
import matplotlib as mpl
26+
27+
fig = plt.figure(figsize=(8, 3))
28+
ax1 = fig.add_axes([0.05, 0.80, 0.9, 0.15])
29+
ax2 = fig.add_axes([0.05, 0.475, 0.9, 0.15])
30+
ax3 = fig.add_axes([0.05, 0.15, 0.9, 0.15])
31+
32+
###############################################################################
33+
# Basic continuous colorbar
34+
# -------------------------
35+
#
36+
# Set the colormap and norm to correspond to the data for which the colorbar
37+
# will be used. Then create the colorbar by calling `ColorbarBase` and
38+
# specify axis, colormap, norm and orientation as parameters. Here we create
39+
# a basic continuous colorbar with ticks and labels. There are many more kwargs
40+
# which can be used to further modify the colorbar.
41+
42+
cmap = mpl.cm.cool
43+
norm = mpl.colors.Normalize(vmin=5, vmax=10)
44+
45+
cb1 = mpl.colorbar.ColorbarBase(ax1, cmap=cmap,
46+
norm=norm,
47+
orientation='horizontal')
48+
cb1.set_label('Some Units')
49+
50+
###############################################################################
51+
# Discrete intervals colorbar
52+
# ---------------------------
53+
#
54+
# The second example illustrates the use of a ListedColormap which generates
55+
# colormap from a set of listed colors, a BoundaryNorm which generates a
56+
# colormap index based on discrete interval and extended ends to show the
57+
# "over" and "under" value colors. Over and under are used to display data
58+
# outside of the normalized [0,1] range. Here we pass colors as gray shades as
59+
# a string encoding a float in the 0-1 range.
60+
#
61+
# If a ListedColormap is used, the length of the bounds array must be
62+
# one greater than the length of the color list. The bounds must be
63+
# monotonically increasing.
64+
#
65+
# This time we pass some more arguments in addition to previous arguments to
66+
# ColorBase. For the out-of-range values to display on the colorbar, we have to
67+
# use the extend keyword argument. To use 'extend', you must specify two extra
68+
# boundaries. Finally spacing argument ensures that intervals are shown on
69+
# colorbar proportionally.
70+
71+
cmap = mpl.colors.ListedColormap(['r', 'g', 'b', 'c'])
72+
cmap.set_over('0.25')
73+
cmap.set_under('0.75')
74+
75+
bounds = [1, 2, 4, 7, 8]
76+
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
77+
cb2 = mpl.colorbar.ColorbarBase(ax2, cmap=cmap,
78+
norm=norm,
79+
boundaries=[0] + bounds + [13],
80+
extend='both',
81+
ticks=bounds,
82+
spacing='proportional',
83+
orientation='horizontal')
84+
cb2.set_label('Discrete intervals, some other units')
85+
86+
###############################################################################
87+
# Colorbar with custom extension lengths
88+
# --------------------------------------
89+
#
90+
# Now in the third example we illustrate the use of custom length colorbar
91+
# extensions, used on a colorbar with discrete intervals. Here we pass colors
92+
# as RGB triplet. To make the length of each extension the same as the length
93+
# of the interior colors pass extendfrac argument as auto
94+
95+
cmap = mpl.colors.ListedColormap([[0., .4, 1.], [0., .8, 1.],
96+
[1., .8, 0.], [1., .4, 0.]])
97+
cmap.set_over((1., 0., 0.))
98+
cmap.set_under((0., 0., 1.))
99+
100+
bounds = [-1., -.5, 0., .5, 1.]
101+
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
102+
cb3 = mpl.colorbar.ColorbarBase(ax3, cmap=cmap,
103+
norm=norm,
104+
boundaries=[-10] + bounds + [10],
105+
extend='both',
106+
extendfrac='auto',
107+
ticks=bounds,
108+
spacing='uniform',
109+
orientation='horizontal')
110+
cb3.set_label('Custom extension lengths, some other units')
111+
112+
plt.show()

0 commit comments

Comments
 (0)
0