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

Skip to content

Commit 752c376

Browse files
authored
Merge pull request #19326 from Ptrskay3/doc-colormap-whitespace
DOC: Whitespace in Choosing Colormaps tutorial plots
2 parents b52aad8 + f9b9930 commit 752c376

File tree

1 file changed

+10
-9
lines changed

1 file changed

+10
-9
lines changed

tutorials/colors/colormaps.py

Lines changed: 10 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -199,30 +199,31 @@
199199
# First, we'll show the range of each colormap. Note that some seem
200200
# to change more "quickly" than others.
201201

202-
nrows = max(len(cmap_list) for cmap_category, cmap_list in cmaps.items())
203202
gradient = np.linspace(0, 1, 256)
204203
gradient = np.vstack((gradient, gradient))
205204

206205

207-
def plot_color_gradients(cmap_category, cmap_list, nrows):
208-
fig, axs = plt.subplots(nrows=nrows)
209-
fig.subplots_adjust(top=0.95, bottom=0.01, left=0.2, right=0.99)
206+
def plot_color_gradients(cmap_category, cmap_list):
207+
# Create figure and adjust figure height to number of colormaps
208+
nrows = len(cmap_list)
209+
figh = 0.35 + 0.15 + (nrows + (nrows - 1) * 0.1) * 0.22
210+
fig, axs = plt.subplots(nrows=nrows + 1, figsize=(6.4, figh))
211+
fig.subplots_adjust(top=1 - 0.35 / figh, bottom=0.15 / figh,
212+
left=0.2, right=0.99)
210213
axs[0].set_title(cmap_category + ' colormaps', fontsize=14)
211214

212215
for ax, name in zip(axs, cmap_list):
213216
ax.imshow(gradient, aspect='auto', cmap=plt.get_cmap(name))
214-
pos = list(ax.get_position().bounds)
215-
x_text = pos[0] - 0.01
216-
y_text = pos[1] + pos[3]/2.
217-
fig.text(x_text, y_text, name, va='center', ha='right', fontsize=10)
217+
ax.text(-0.01, 0.5, name, va='center', ha='right', fontsize=10,
218+
transform=ax.transAxes)
218219

219220
# Turn off *all* ticks & spines, not just the ones with colormaps.
220221
for ax in axs:
221222
ax.set_axis_off()
222223

223224

224225
for cmap_category, cmap_list in cmaps.items():
225-
plot_color_gradients(cmap_category, cmap_list, nrows)
226+
plot_color_gradients(cmap_category, cmap_list)
226227

227228
plt.show()
228229

0 commit comments

Comments
 (0)