Skip to content

Commit 8d40dce

Browse files
authored
Merge pull request #8600 from patniharshit/colorbar_only_tut
Colorbar only tut
2 parents 753dfa7 + 2996c49 commit 8d40dce

File tree

2 files changed

+109
-78
lines changed

2 files changed

+109
-78
lines changed

examples/api/colorbar_only.py

Lines changed: 0 additions & 78 deletions
This file was deleted.

tutorials/colors/colorbar_only.py

Lines changed: 109 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,109 @@
1+
"""
2+
=============================
3+
Customized Colorbars Tutorial
4+
=============================
5+
6+
This tutorial shows how to build colorbars without an attached plot.
7+
8+
Customized Colorbars
9+
====================
10+
11+
:class:`~matplotlib.colorbar.ColorbarBase` derives from
12+
:mod:`~matplotlib.cm.ScalarMappable` and puts a colorbar in a specified axes,
13+
so it has everything needed for a standalone colorbar. It can be used as is to
14+
make a colorbar for a given colormap and does not need a mappable object like
15+
an image. In this tutorial we will explore what can be done with standalone
16+
colorbar.
17+
18+
Basic continuous colorbar
19+
-------------------------
20+
21+
Set the colormap and norm to correspond to the data for which the colorbar
22+
will be used. Then create the colorbar by calling
23+
:class:`~matplotlib.colorbar.ColorbarBase` and specify axis, colormap, norm
24+
and orientation as parameters. Here we create a basic continuous colorbar
25+
with ticks and labels. More information on colorbar api can be found
26+
`here <https://matplotlib.org/api/colorbar_api.html>`.
27+
"""
28+
29+
import matplotlib.pyplot as plt
30+
import matplotlib as mpl
31+
32+
fig, ax = plt.subplots()
33+
34+
cmap = mpl.cm.cool
35+
norm = mpl.colors.Normalize(vmin=5, vmax=10)
36+
37+
cb1 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
38+
norm=norm,
39+
orientation='horizontal')
40+
cb1.set_label('Some Units')
41+
fig.show()
42+
43+
###############################################################################
44+
# Discrete intervals colorbar
45+
# ---------------------------
46+
#
47+
# The second example illustrates the use of a
48+
# :class:`~matplotlib.colors.ListedColormap` which generates a colormap from a
49+
# set of listed colors, :func:`colors.BoundaryNorm` which generates a colormap
50+
# index based on discrete intervals and extended ends to show the "over" and
51+
# "under" value colors. Over and under are used to display data outside of the
52+
# normalized [0,1] range. Here we pass colors as gray shades as a string
53+
# encoding a float in the 0-1 range.
54+
#
55+
# If a :class:`~matplotlib.colors.ListedColormap` is used, the length of the
56+
# bounds array must be one greater than the length of the color list. The
57+
# bounds must be monotonically increasing.
58+
#
59+
# This time we pass some more arguments in addition to previous arguments to
60+
# :class:`~matplotlib.colorbar.ColorbarBase`. For the out-of-range values to
61+
# display on the colorbar, we have to use the *extend* keyword argument. To use
62+
# *extend*, you must specify two extra boundaries. Finally spacing argument
63+
# ensures that intervals are shown on colorbar proportionally.
64+
65+
fig, ax = plt.subplots()
66+
67+
cmap = mpl.colors.ListedColormap(['red', 'green', 'blue', 'cyan'])
68+
cmap.set_over('0.25')
69+
cmap.set_under('0.75')
70+
71+
bounds = [1, 2, 4, 7, 8]
72+
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()
82+
83+
###############################################################################
84+
# Colorbar with custom extension lengths
85+
# --------------------------------------
86+
#
87+
# Here we illustrate the use of custom length colorbar extensions, used on a
88+
# colorbar with discrete intervals. To make the length of each extension same
89+
# as the length of the interior colors, use ``extendfrac='auto'``.
90+
91+
fig, ax = plt.subplots()
92+
93+
cmap = mpl.colors.ListedColormap(['royalblue', 'cyan',
94+
'yellow', 'orange'])
95+
cmap.set_over('red')
96+
cmap.set_under('blue')
97+
98+
bounds = [-1.0, -0.5, 0.0, 0.5, 1.0]
99+
norm = mpl.colors.BoundaryNorm(bounds, cmap.N)
100+
cb3 = mpl.colorbar.ColorbarBase(ax, cmap=cmap,
101+
norm=norm,
102+
boundaries=[-10] + bounds + [10],
103+
extend='both',
104+
extendfrac='auto',
105+
ticks=bounds,
106+
spacing='uniform',
107+
orientation='horizontal')
108+
cb3.set_label('Custom extension lengths, some other units')
109+
fig.show()

0 commit comments

Comments
 (0)