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 c3a17ab

Browse filesBrowse files
committed
DOC: add colorbar example
1 parent 4f58efa commit c3a17ab
Copy full SHA for c3a17ab

File tree

Expand file treeCollapse file tree

2 files changed

+56
-2
lines changed
Filter options
Expand file treeCollapse file tree

2 files changed

+56
-2
lines changed
+54Lines changed: 54 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,54 @@
1+
"""
2+
=================
3+
Placing Colorbars
4+
=================
5+
6+
Colorbars indicate the quantitative extent of image data. Placing in
7+
a figure is non-trivial because room needs to be made for them.
8+
9+
The simplest case is just attaching a colorbar to each axes:
10+
"""
11+
import matplotlib.pyplot as plt
12+
import numpy as np
13+
14+
fig, axs = plt.subplots(2, 2)
15+
cm = ['RdBu_r', 'viridis']
16+
for col in range(2):
17+
for row in range(2):
18+
ax = axs[row, col]
19+
pcm = ax.pcolormesh(np.random.random((20, 20)) * (col + 1),
20+
cmap=cm[col])
21+
fig.colorbar(pcm, ax=ax)
22+
plt.show()
23+
24+
######################################################################
25+
# The first column has the same type of data in both rows, so it may
26+
# be desirable to combine the colorbar which we do by calling
27+
# `.Figure.colorbar` with a list of axes instead of a single axes.
28+
29+
fig, axs = plt.subplots(2, 2)
30+
cm = ['RdBu_r', 'viridis']
31+
for col in range(2):
32+
for row in range(2):
33+
ax = axs[row, col]
34+
pcm = ax.pcolormesh(np.random.random((20, 20)) * (col + 1),
35+
cmap=cm[col])
36+
fig.colorbar(pcm, ax=axs[:, col], shrink=0.6)
37+
plt.show()
38+
39+
######################################################################
40+
# Relatively complicated colorbar layouts are possible using this
41+
# paradigm. Note that this example works far better with
42+
# ``constrained_layout=True``
43+
44+
fig, axs = plt.subplots(3, 3, constrained_layout=True)
45+
for ax in axs.flat:
46+
pcm = ax.pcolormesh(np.random.random((20, 20)))
47+
48+
fig.colorbar(pcm, ax=axs[0, :2], shrink=0.6, location='bottom')
49+
fig.colorbar(pcm, ax=[axs[0, 2]], location='bottom')
50+
fig.colorbar(pcm, ax=axs[1:, :], location='right', shrink=0.6)
51+
fig.colorbar(pcm, ax=[axs[2, 1]], location='left')
52+
53+
54+
plt.show()

‎tutorials/intermediate/constrainedlayout_guide.py

Copy file name to clipboardExpand all lines: tutorials/intermediate/constrainedlayout_guide.py
+2-2Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ def example_plot(ax, fontsize=12, nodec=False):
121121
# For the `~.axes.Axes.pcolormesh` kwargs (``pc_kwargs``) we use a
122122
# dictionary. Below we will assign one colorbar to a number of axes each
123123
# containing a `~.cm.ScalarMappable`; specifying the norm and colormap
124-
# ensuresthe colorbar is accurate for all the axes.
124+
# ensures the colorbar is accurate for all the axes.
125125

126126
arr = np.arange(100).reshape((10, 10))
127127
norm = mcolors.Normalize(vmin=0., vmax=100.)
@@ -471,7 +471,7 @@ def docomplicated(suptitle=None):
471471
#
472472
# ``constrained_layout`` usually adjusts the axes positions on each draw
473473
# of the figure. If you want to get the spacing provided by
474-
# ``constrained_layout`` but then not have it update, then do the initial
474+
# ``constrained_layout`` but not have it update, then do the initial
475475
# draw and then call ``fig.set_constrained_layout(False)``.
476476
# This is potentially useful for animations where the tick labels may
477477
# change length.

0 commit comments

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