8000 DOC: add a couple more placement examples, crosslink axes_grid [ci doc] · matplotlib/matplotlib@94b8a76 · GitHub
[go: up one dir, main page]

Skip to content

Commit 94b8a76

Browse files
committed
DOC: add a couple more placement examples, crosslink axes_grid [ci doc]
1 parent 37f26da commit 94b8a76

File tree

4 files changed

+65
-11
lines changed

4 files changed

+65
-11
lines changed

doc/conf.py

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -47,8 +47,9 @@ def _parse_skip_subdirs_file():
4747
but you can skip subdirectories of 'users'. Doing this
4848
can make partial builds very fast.
4949
"""
50-
default_skip_subdirs = ['users/prev_whats_new/*', 'api/*', 'gallery/*',
51-
'tutorials/*', 'plot_types/*', 'devel/*']
50+
default_skip_subdirs = [
51+
'users/prev_whats_new/*', 'users/explain/*', 'api/*', 'gallery/*',
52+
'tutorials/*', 'plot_types/*', 'devel/*']
5253
try:
5354
with open(".mpl_skip_subdirs.yaml", 'r') as fin:
5455
print('Reading subdirectories to skip from',

galleries/examples/axes_grid1/demo_colorbar_with_axes_divider.py

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,6 @@
11
"""
2+
.. _demo-colorbar-with-axes-divider:
3+
24
=========================
35
Colorbar with AxesDivider
46
=========================
@@ -8,6 +10,10 @@
810
method of the `.AxesDivider` can then be used to create a new axes on a given
911
side ("top", "right", "bottom", or "left") of the original axes. This example
1012
uses `.append_axes` to add colorbars next to axes.
13+
14+
Users should consider simply passing a list of axes to the *ax* keyword argument of
15+
`~.Figure.colorbar` instead of creating a locatable axes manually like this.
16+
See :ref:`colorbar_placement`.
1117
"""
1218

1319
import matplotlib.pyplot as plt

galleries/examples/axes_grid1/demo_colorbar_with_inset_locator.py

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,15 +1,21 @@
11
"""
2+
.. _demo-colorbar-with-inset-locator:
3+
24
==============================================================
35
Controlling the position and size of colorbars with Inset Axes
46
==============================================================
57
6-
This example shows how to control the position, height, and width of
7-
colorbars using `~mpl_toolkits.axes_grid1.inset_locator.inset_axes`.
8+
This example shows how to control the position, height, and width of colorbars
9+
using `~mpl_toolkits.axes_grid1.inset_locator.inset_axes`.
810
911
Inset axes placement is controlled as for legends: either by providing a *loc*
1012
option ("upper right", "best", ...), or by providing a locator with respect to
1113
the parent bbox. Parameters such as *bbox_to_anchor* and *borderpad* likewise
1214
work in the same way, and are also demonstrated here.
15+
16+
Users should consider using `.Axes.inset_axes` instead (see
17+
:ref:`colorbar_placement`).
18+
1319
"""
1420

1521
import matplotlib.pyplot as plt

galleries/users_explain/axes/colorbar_placement.py

Lines changed: 48 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -28,9 +28,9 @@
2828
fig.colorbar(pcm, ax=ax)
2929

3030
# %%
31-
# The first column has the same type of data in both rows, so it may
32-
# be desirable to combine the colorbar which we do by calling
33-
# `.Figure.colorbar` with a list of axes instead of a single axes.
31+
# The first column has the same type of data in both rows, so it may be
32+
# desirable to have just one colorbar. We do by passing `.Figure.colorbar`
33+
# a list of axes to the *ax* kwarg.
3434

3535
fig, axs = plt.subplots(2, 2)
3636
cmaps = ['RdBu_r', 'viridis']
@@ -77,9 +77,12 @@
7777
fig.colorbar(pcm, ax=ax, shrink=0.6)
7878

7979
# %%
80-
# One way around this issue is to use an `.Axes.inset_axes` to locate the
81-
# axes in axes coordinates. Note that if you zoom in on the axes, and
82-
# change the shape of the axes, the colorbar will also change position.
80+
# One way around this issue is to use an `.Axes.inset_axes` to locate the axes
81+
# in axes coordinates. Here instead of populating the *ax* keyword argument,
82+
# we create an axes named ``cax`` to put the colorbar in, and pass it to the
83+
# ``colorbar`` in the *cax* keyword argument. Note that if you zoom in on the
84+
# axes, and change the shape of the axes, the colorbar will also change
85+
# position.
8386

8487
fig, axs = plt.subplots(2, 2, layout='constrained')
8588
cmaps = ['RdBu_r', 'viridis']
@@ -94,6 +97,44 @@
9497
ax.set_aspect(1/2)
9598
if row == 1:
9699
cax = ax.inset_axes([1.04, 0.2, 0.05, 0.6])
97-
fig.colorbar(pcm, ax=ax, cax=cax)
100+
fig.colorbar(pcm, cax=cax)
101+
102+
103+
# %%
104+
# Manually placing colorbars
105+
# ==========================
106+
#
107+
# As the case above indicates, sometimes we want to place a colorbar manually
108+
# on the figure, which typically involves creating a host axes manually and
109+
# passing it to the *cax* keyword argument. You can do this with any type of
110+
# axes, but an `.Axes.inset_axes` is useful because it can be positioned
111+
# relative to the parent axes. Here we add a colorbar centered near the bottom
112+
# of the parent axes.
113+
114+
fig, ax = plt.subplots(layout='constrained')
115+
pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis')
116+
ax.set_ylim([-4, 20])
117+
cax = ax.inset_axes([0.3, 0.06, 0.4, 0.04])
118+
fig.colorbar(pcm, cax=cax, orientation='horizontal')
119+
120+
# %%
121+
# `.Axes.inset_axes` can also specify its position in data coordinates
122+
# using the *transform* keyword argument if you want your axes at a
123+
# certain data position on the graph:
124+
125+
fig, ax = plt.subplots(layout='constrained')
126+
pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis')
127+
ax.set_ylim([-4, 20])
128+
cax = ax.inset_axes([7.5, -2, 5, 1.2], transform=ax.transData)
129+
fig.colorbar(pcm, cax=cax, orientation='horizontal')
98130

99131
plt.show()
132+
133+
# %%
134+
# .. seealso::
135+
#
136+
# The :ref:`axes_grid` has methods for creating colorbar axes as well:
137+
#
138+
# - :ref:`demo-colorbar-with-inset-locator`
139+
# - :ref:`demo-colorbar-with-axes-divider`
140+
#

0 commit comments

Comments
 (0)
0