Thanks to visit codestin.com
Credit goes to github.com

Skip to content

Commit 3ca96ad

Browse files
committed
DOC: add pad discussion [ci doc]
1 parent 94b8a76 commit 3ca96ad

File tree

1 file changed

+32
-8
lines changed

1 file changed

+32
-8
lines changed

galleries/users_explain/axes/colorbar_placement.py

Lines changed: 32 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,31 @@
5555
fig.colorbar(pcm, ax=axs[1:, :], location='right', shrink=0.6)
5656
fig.colorbar(pcm, ax=[axs[2, 1]], location='left')
5757

58+
# %%
59+
# Adjusting the spacing between colorbars and parent axes
60+
# =======================================================
61+
#
62+
# The distance a colorbar is from the parent axes can be adjusted with the
63+
# *pad* keyword argument. This is in units of fraction of the parent axes
64+
# width, and the default for a vertical axes is 0.05 (or 0.15 for a horizontal
65+
# axes).
66+
67+
fig, axs = plt.subplots(3, 1, layout='constrained', figsize=(5, 5))
68+
for ax, pad in zip(axs, [0.025, 0.05, 0.1]):
69+
pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis')
70+
fig.colorbar(pcm, ax=ax, pad=pad, label=f'pad: {pad}')
71+
fig.suptitle("layout='constrained'")
72+
73+
# %%
74+
# Note that if you do not use constrained layout, the pad command makes the
75+
# parent axes shrink:
76+
77+
fig, axs = plt.subplots(3, 1, figsize=(5, 5))
78+
for ax, pad in zip(axs, [0.025, 0.05, 0.1]):
79+
pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis')
80+
fig.colorbar(pcm, ax=ax, pad=pad, label=f'pad: {pad}')
81+
fig.suptitle("No layout manager")
82+
5883
# %%
5984
# Colorbars with fixed-aspect-ratio axes
6085
# ======================================
@@ -99,7 +124,6 @@
99124
cax = ax.inset_axes([1.04, 0.2, 0.05, 0.6])
100125
fig.colorbar(pcm, cax=cax)
101126

102-
103127
# %%
104128
# Manually placing colorbars
105129
# ==========================
@@ -111,30 +135,30 @@
111135
# relative to the parent axes. Here we add a colorbar centered near the bottom
112136
# of the parent axes.
113137

114-
fig, ax = plt.subplots(layout='constrained')
138+
fig, ax = plt.subplots(layout='constrained', figsize=(4, 4))
115139
pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis')
116140
ax.set_ylim([-4, 20])
117-
cax = ax.inset_axes([0.3, 0.06, 0.4, 0.04])
141+
cax = ax.inset_axes([0.3, 0.07, 0.4, 0.04])
118142
fig.colorbar(pcm, cax=cax, orientation='horizontal')
119143

120144
# %%
121145
# `.Axes.inset_axes` can also specify its position in data coordinates
122146
# using the *transform* keyword argument if you want your axes at a
123147
# certain data position on the graph:
124148

125-
fig, ax = plt.subplots(layout='constrained')
149+
fig, ax = plt.subplots(layout='constrained', figsize=(4, 4))
126150
pcm = ax.pcolormesh(np.random.randn(20, 20), cmap='viridis')
127151
ax.set_ylim([-4, 20])
128-
cax = ax.inset_axes([7.5, -2, 5, 1.2], transform=ax.transData)
152+
cax = ax.inset_axes([7.5, -1.7, 5, 1.2], transform=ax.transData)
129153
fig.colorbar(pcm, cax=cax, orientation='horizontal')
130154

131155
plt.show()
132156

133157
# %%
134158
# .. seealso::
135159
#
136-
# The :ref:`axes_grid` has methods for creating colorbar axes as well:
160+
# :ref:`axes_grid` has methods for creating colorbar axes as well:
137161
#
138-
# - :ref:`demo-colorbar-with-inset-locator`
139-
# - :ref:`demo-colorbar-with-axes-divider`
162+
# - :ref:`demo-colorbar-with-inset-locator`
163+
# - :ref:`demo-colorbar-with-axes-divider`
140164
#

0 commit comments

Comments
 (0)