55
66This tutorial shows how to build colorbars without an attached plot.
77
8- """
8+ Customized Colorbars
9+ ====================
910
10- ###############################################################################
11- # Customized Colorbars
12- # ====================
13- #
14- # ColorbarBase derives from ScalarMappable and puts a colorbar in a specified
15- # axes, so it has everything needed for a standalone colorbar. It can be used
16- # as is to make a colorbar for a given colormap and does not need a mappable
17- # object like an image. In this tutorial we will explore what can be done with
18- # standalone colorbar.
19- #
20- # We will start by making a figure of desired size and adding thress axes.
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+ We will start by making a figure of desired size and adding three axes.
19+ """
2120
2221import matplotlib .pyplot as plt
2322import matplotlib as mpl
2928# -------------------------
3029#
3130# Set the colormap and norm to correspond to the data for which the colorbar
32- # will be used. Then create the colorbar by calling `ColorbarBase` and
33- # specify axis, colormap, norm and orientation as parameters. Here we create
34- # a basic continuous colorbar with ticks and labels. There are many more kwargs
35- # which can be used to further modify the colorbar.
31+ # will be used. Then create the colorbar by calling
32+ # :class:`~matplotlib.colorbar.ColorbarBase` and specify axis, colormap, norm
33+ # and orientation as parameters. Here we create a basic continuous colorbar
34+ # with ticks and labels. More information on colorbar api can be found
35+ # `here <https://matplotlib.org/api/colorbar_api.html>`.
3636
3737cmap = mpl .cm .cool
3838norm = mpl .colors .Normalize (vmin = 5 , vmax = 10 )
4646# Discrete intervals colorbar
4747# ---------------------------
4848#
49- # The second example illustrates the use of a ListedColormap which generates a
50- # colormap from a set of listed colors, a BoundaryNorm which generates a
51- # colormap index based on discrete intervals and extended ends to show the
52- # "over" and "under" value colors. Over and under are used to display data
53- # outside of the normalized [0,1] range. Here we pass colors as gray shades as
54- # a string encoding a float in the 0-1 range.
49+ # The second example illustrates the use of a
50+ # :class:`~matplotlib.colors.ListedColormap` which generates a colormap from a
51+ # set of listed colors, :func:`colors.BoundaryNorm` which generates a colormap
52+ # index based on discrete intervals and extended ends to show the "over" and
53+ # "under" value colors. Over and under are used to display data outside of the
54+ # normalized [0,1] range. Here we pass colors as gray shades as a string
55+ # encoding a float in the 0-1 range.
5556#
56- # If a ListedColormap is used, the length of the bounds array must be
57- # one greater than the length of the color list. The bounds must be
58- # monotonically increasing.
57+ # If a :class:`~matplotlib.colors. ListedColormap` is used, the length of the
58+ # bounds array must be one greater than the length of the color list. The
59+ # bounds must be monotonically increasing.
5960#
6061# This time we pass some more arguments in addition to previous arguments to
61- # ColorBase. For the out-of-range values to display on the colorbar, we have to
62- # use the extend keyword argument. To use ' extend', you must specify two extra
63- # boundaries. Finally spacing argument ensures that intervals are shown on
64- # colorbar proportionally.
62+ # :class:`~matplotlib.colorbar.ColorbarBase`. For the out-of-range values to
63+ # display on the colorbar, we have to use the * extend* keyword argument. To use
64+ # *extend*, you must specify two extra boundaries. Finally spacing argument
65+ # ensures that intervals are shown on colorbar proportionally.
6566
66- cmap = mpl .colors .ListedColormap (['r ' , 'g ' , 'b ' , 'c ' ])
67+ cmap = mpl .colors .ListedColormap (['red ' , 'green ' , 'blue ' , 'cyan ' ])
6768cmap .set_over ('0.25' )
6869cmap .set_under ('0.75' )
6970
8384# --------------------------------------
8485#
8586# Here we illustrate the use of custom length colorbar extensions, used on a
86- # colorbar with discrete intervals. Here we pass colors as RGB triplet. To make
87- # the length of each extension the same as the length of the interior colors
88- # pass the extendfrac argument as auto.
87+ # colorbar with discrete intervals. To make the length of each extension the
88+ # same as the length of the interior colors pass the *extendfrac* argument as
89+ # * auto* .
8990
90- cmap = mpl .colors .ListedColormap ([[ 0. , .4 , 1. ], [ 0. , .8 , 1. ] ,
91- [ 1. , .8 , 0. ], [ 1. , .4 , 0. ] ])
92- cmap .set_over (( 1. , 0. , 0. ) )
93- cmap .set_under (( 0. , 0. , 1. ) )
91+ cmap = mpl .colors .ListedColormap (['royalblue' , 'cyan' ,
92+ 'yellow' , 'orange' ])
93+ cmap .set_over ('red' )
94+ cmap .set_under ('blue' )
9495
95- bounds = [- 1. , - .5 , 0. , .5 , 1. ]
96+ bounds = [- 1.0 , - 0 .5 , 0.0 , 0 .5 , 1.0 ]
9697norm = mpl .colors .BoundaryNorm (bounds , cmap .N )
9798cb3 = mpl .colorbar .ColorbarBase (ax3 , cmap = cmap ,
9899 norm = norm ,
105106cb3 .set_label ('Custom extension lengths, some other units' )
106107
107108plt .tight_layout ()
108- plt .show ()
109+ plt .show ()
0 commit comments