Skip to content

Commit 3beaebf

Browse files
committed
Apply ax.colorbar() to examples
1 parent 6d57fc1 commit 3beaebf

25 files changed

+149
-172
lines changed

galleries/examples/color/colorbar_basics.py

Lines changed: 7 additions & 13 deletions
Original file line numberDiff line numberDiff line change
@@ -22,26 +22,20 @@
2222

2323
fig, (ax1, ax2, ax3) = plt.subplots(figsize=(13, 3), ncols=3)
2424

25-
# plot just the positive data and save the
26-
# color "mappable" object returned by ax1.imshow
27-
pos = ax1.imshow(Zpos, cmap='Blues', interpolation='none')
28-
29-
# add the colorbar using the figure's method,
30-
# telling which mappable we're talking about and
31-
# which Axes object it should be near
32-
fig.colorbar(pos, ax=ax1)
25+
# plot just the positive data and add a colorbar
26+
ax1.imshow(Zpos, cmap='Blues', interpolation='none')
27+
ax1.colorbar()
3328

3429
# repeat everything above for the negative data
3530
# you can specify location, anchor and shrink the colorbar
36-
neg = ax2.imshow(Zneg, cmap='Reds_r', interpolation='none')
37-
fig.colorbar(neg, ax=ax2, location='right', anchor=(0, 0.3), shrink=0.7)
31+
ax2.imshow(Zneg, cmap='Reds_r', interpolation='none')
32+
ax2.colorbar(location='right', anchor=(0, 0.3), shrink=0.7)
3833

3934
# Plot both positive and negative values between +/- 1.2
40-
pos_neg_clipped = ax3.imshow(Z, cmap='RdBu', vmin=-1.2, vmax=1.2,
41-
interpolation='none')
35+
ax3.imshow(Z, cmap='RdBu', vmin=-1.2, vmax=1.2, interpolation='none')
4236
# Add minorticks on the colorbar to make it easy to read the
4337
# values off the colorbar.
44-
cbar = fig.colorbar(pos_neg_clipped, ax=ax3, extend='both')
38+
cbar = ax3.colorbar(extend='both')
4539
cbar.minorticks_on()
4640
plt.show()
4741

galleries/examples/color/custom_cmap.py

Lines changed: 11 additions & 11 deletions
Original file line numberDiff line numberDiff line change
@@ -122,9 +122,9 @@
122122
# Create the colormap
123123
cmap = LinearSegmentedColormap.from_list(cmap_name, colors, N=n_bin)
124124
# Fewer bins will result in "coarser" colomap interpolation
125-
im = ax.imshow(Z, origin='lower', cmap=cmap)
125+
ax.imshow(Z, origin='lower', cmap=cmap)
126+
ax.colorbar()
126127
ax.set_title("N bins: %s" % n_bin)
127-
fig.colorbar(im, ax=ax)
128128

129129

130130
# %%
@@ -226,20 +226,20 @@
226226
fig, axs = plt.subplots(2, 2, figsize=(6, 9))
227227
fig.subplots_adjust(left=0.02, bottom=0.06, right=0.95, top=0.94, wspace=0.05)
228228

229-
im1 = axs[0, 0].imshow(Z, cmap=blue_red1)
230-
fig.colorbar(im1, ax=axs[0, 0])
229+
axs[0, 0].imshow(Z, cmap=blue_red1)
230+
axs[0, 0].colorbar()
231231

232-
im2 = axs[1, 0].imshow(Z, cmap='BlueRed2')
233-
fig.colorbar(im2, ax=axs[1, 0])
232+
axs[1, 0].imshow(Z, cmap='BlueRed2')
233+
axs[1, 0].colorbar()
234234

235235
# Now we will set the third cmap as the default. One would
236236
# not normally do this in the middle of a script like this;
237237
# it is done here just to illustrate the method.
238238

239239
plt.rcParams['image.cmap'] = 'BlueRed3'
240240

241-
im3 = axs[0, 1].imshow(Z)
242-
fig.colorbar(im3, ax=axs[0, 1])
241+
axs[0, 1].imshow(Z)
242+
axs[0, 1].colorbar()
243243
axs[0, 1].set_title("Alpha = 1")
244244

245245
# Or as yet another variation, we can replace the rcParams
@@ -252,12 +252,12 @@
252252
# Draw a line with low zorder so it will be behind the image.
253253
axs[1, 1].plot([0, 10 * np.pi], [0, 20 * np.pi], color='c', lw=20, zorder=-1)
254254

255-
im4 = axs[1, 1].imshow(Z)
256-
fig.colorbar(im4, ax=axs[1, 1])
255+
im = axs[1, 1].imshow(Z)
256+
axs[1, 1].colorbar()
257257

258258
# Here it is: changing the colormap for the current image and its
259259
# colorbar after they have been plotted.
260-
im4.set_cmap('BlueRedAlpha')
260+
im.set_cmap('BlueRedAlpha')
261261
axs[1, 1].set_title("Varying alpha")
262262

263263
fig.suptitle('Custom Blue-Red colormaps', fontsize=16)

galleries/examples/images_contours_and_fields/colormap_interactive_adjustment.py

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -24,10 +24,9 @@
2424
data2d = np.sin(t)[:, np.newaxis] * np.cos(t)[np.newaxis, :]
2525

2626
fig, ax = plt.subplots()
27-
im = ax.imshow(data2d)
27+
ax.imshow(data2d)
28+
ax.colorbar(label='Interactive colorbar')
2829
ax.set_title('Pan on the colorbar to shift the color mapping\n'
2930
'Zoom on the colorbar to scale the color mapping')
3031

31-
fig.colorbar(im, ax=ax, label='Interactive colorbar')
32-
3332
plt.show()

galleries/examples/images_contours_and_fields/colormap_normalizations.py

Lines changed: 29 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -32,12 +32,12 @@
3232

3333
fig, ax = plt.subplots(2, 1)
3434

35-
pcm = ax[0].pcolor(X, Y, Z, cmap='PuBu_r', shading='nearest')
36-
fig.colorbar(pcm, ax=ax[0], extend='max', label='linear scaling')
35+
ax[0].pcolor(X, Y, Z, cmap='PuBu_r', shading='nearest')
36+
ax[0].colorbar(extend='max', label='linear scaling')
3737

38-
pcm = ax[1].pcolor(X, Y, Z, cmap='PuBu_r', shading='nearest',
39-
norm=colors.LogNorm(vmin=Z.min(), vmax=Z.max()))
40-
fig.colorbar(pcm, ax=ax[1], extend='max', label='LogNorm')
38+
ax[1].pcolor(X, Y, Z, cmap='PuBu_r', shading='nearest',
39+
norm=colors.LogNorm(vmin=Z.min(), vmax=Z.max()))
40+
ax[1].colorbar(extend='max', label='LogNorm')
4141

4242
# %%
4343
# PowerNorm
@@ -53,12 +53,12 @@
5353

5454
fig, ax = plt.subplots(2, 1)
5555

56-
pcm = ax[0].pcolormesh(X, Y, Z, cmap='PuBu_r', shading='nearest')
57-
fig.colorbar(pcm, ax=ax[0], extend='max', label='linear scaling')
56+
ax[0].pcolormesh(X, Y, Z, cmap='PuBu_r', shading='nearest')
57+
ax[0].colorbar(extend='max', label='linear scaling')
5858

59-
pcm = ax[1].pcolormesh(X, Y, Z, cmap='PuBu_r', shading='nearest',
60-
norm=colors.PowerNorm(gamma=0.5))
61-
fig.colorbar(pcm, ax=ax[1], extend='max', label='PowerNorm')
59+
ax[1].pcolormesh(X, Y, Z, cmap='PuBu_r', shading='nearest',
60+
norm=colors.PowerNorm(gamma=0.5))
61+
ax[1].colorbar(extend='max', label='PowerNorm')
6262

6363
# %%
6464
# SymLogNorm
@@ -79,14 +79,13 @@
7979

8080
fig, ax = plt.subplots(2, 1)
8181

82-
pcm = ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
83-
vmin=-np.max(Z))
84-
fig.colorbar(pcm, ax=ax[0], extend='both', label='linear scaling')
82+
ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest', vmin=-np.max(Z))
83+
ax[0].colorbar(extend='both', label='linear scaling')
8584

86-
pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
87-
norm=colors.SymLogNorm(linthresh=0.015,
88-
vmin=-10.0, vmax=10.0, base=10))
89-
fig.colorbar(pcm, ax=ax[1], extend='both', label='SymLogNorm')
85+
ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
86+
norm=colors.SymLogNorm(linthresh=0.015,
87+
vmin=-10.0, vmax=10.0, base=10))
88+
ax[1].colorbar(extend='both', label='SymLogNorm')
9089

9190
# %%
9291
# Custom Norm
@@ -112,13 +111,12 @@ def __call__(self, value, clip=None):
112111
# %%
113112
fig, ax = plt.subplots(2, 1)
114113

115-
pcm = ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
116-
vmin=-np.max(Z))
117-
fig.colorbar(pcm, ax=ax[0], extend='both', label='linear scaling')
114+
ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest', vmin=-np.max(Z))
115+
ax[0].colorbar(extend='both', label='linear scaling')
118116

119-
pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
120-
norm=MidpointNormalize(midpoint=0))
121-
fig.colorbar(pcm, ax=ax[1], extend='both', label='Custom norm')
117+
ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
118+
norm=MidpointNormalize(midpoint=0))
119+
ax[1].colorbar(extend='both', label='Custom norm')
122120

123121
# %%
124122
# BoundaryNorm
@@ -129,25 +127,22 @@ def __call__(self, value, clip=None):
129127

130128
fig, ax = plt.subplots(3, 1, layout='constrained')
131129

132-
pcm = ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
133-
vmin=-np.max(Z))
134-
fig.colorbar(pcm, ax=ax[0], extend='both', orientation='vertical',
135-
label='linear scaling')
130+
ax[0].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest', vmin=-np.max(Z))
131+
ax[0].colorbar(extend='both', orientation='vertical', label='linear scaling')
136132

137133
# Evenly-spaced bounds gives a contour-like effect.
138134
bounds = np.linspace(-2, 2, 11)
139135
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
140-
pcm = ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
136+
ax[1].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
141137
norm=norm)
142-
fig.colorbar(pcm, ax=ax[1], extend='both', orientation='vertical',
143-
label='BoundaryNorm\nlinspace(-2, 2, 11)')
138+
ax[1].colorbar(extend='both', orientation='vertical',
139+
label='BoundaryNorm\nlinspace(-2, 2, 11)')
144140

145141
# Unevenly-spaced bounds changes the colormapping.
146142
bounds = np.array([-1, -0.5, 0, 2.5, 5])
147143
norm = colors.BoundaryNorm(boundaries=bounds, ncolors=256)
148-
pcm = ax[2].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest',
149-
norm=norm)
150-
fig.colorbar(pcm, ax=ax[2], extend='both', orientation='vertical',
151-
label='BoundaryNorm\n[-1, -0.5, 0, 2.5, 5]')
144+
ax[2].pcolormesh(X, Y, Z, cmap='RdBu_r', shading='nearest', norm=norm)
145+
ax[2].colorbar(extend='both', orientation='vertical',
146+
label='BoundaryNorm\n[-1, -0.5, 0, 2.5, 5]')
152147

153148
plt.show()

galleries/examples/images_contours_and_fields/colormap_normalizations_symlognorm.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -40,16 +40,14 @@ def rbf(x, y):
4040

4141
fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)
4242

43-
pcm = ax[0].pcolormesh(X, Y, Z,
44-
norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
45-
vmin=-gain, vmax=gain, base=10),
46-
**shadeopts)
47-
fig.colorbar(pcm, ax=ax[0], extend='both')
43+
ax[0].pcolormesh(X, Y, Z, norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
44+
vmin=-gain, vmax=gain, base=10),
45+
**shadeopts)
46+
ax[0].colorbar(extend='both')
4847
ax[0].text(-2.5, 1.5, 'symlog')
4948

50-
pcm = ax[1].pcolormesh(X, Y, Z, vmin=-gain, vmax=gain,
51-
**shadeopts)
52-
fig.colorbar(pcm, ax=ax[1], extend='both')
49+
ax[1].pcolormesh(X, Y, Z, vmin=-gain, vmax=gain, **shadeopts)
50+
ax[1].colorbar(extend='both')
5351
ax[1].text(-2.5, 1.5, 'linear')
5452

5553

@@ -67,18 +65,17 @@ def rbf(x, y):
6765

6866
fig, ax = plt.subplots(2, 1, sharex=True, sharey=True)
6967

70-
pcm = ax[0].pcolormesh(X, Y, Z,
71-
norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
72-
vmin=-gain, vmax=gain, base=10),
73-
**shadeopts)
74-
fig.colorbar(pcm, ax=ax[0], extend='both')
68+
ax[0].pcolormesh(X, Y, Z,
69+
norm=colors.SymLogNorm(linthresh=lnrwidth, linscale=1,
70+
vmin=-gain, vmax=gain, base=10),
71+
**shadeopts)
72+
ax[0].colorbar(extend='both')
7573
ax[0].text(-2.5, 1.5, 'symlog')
7674

77-
pcm = ax[1].pcolormesh(X, Y, Z,
78-
norm=colors.AsinhNorm(linear_width=lnrwidth,
79-
vmin=-gain, vmax=gain),
80-
**shadeopts)
81-
fig.colorbar(pcm, ax=ax[1], extend='both')
75+
ax[1].pcolormesh(X, Y, Z,
76+
norm=colors.AsinhNorm(linear_width=lnrwidth, vmin=-gain, vmax=gain),
77+
**shadeopts)
78+
ax[1].colorbar(extend='both')
8279
ax[1].text(-2.5, 1.5, 'asinh')
8380

8481

galleries/examples/images_contours_and_fields/contour_image.py

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -65,8 +65,7 @@
6565

6666
cset3 = axs[0].contour(X, Y, Z, (0,), colors='g', linewidths=2)
6767
axs[0].set_title('Filled contours')
68-
fig.colorbar(cset1, ax=axs[0])
69-
68+
axs[0].colorbar(cset1)
7069

7170
axs[1].imshow(Z, extent=extent, cmap=cmap, norm=norm)
7271
axs[1].contour(Z, levels, colors='k', origin='upper', extent=extent)
@@ -82,13 +81,12 @@
8281
# This is intentional. The Z values are defined at the center of each
8382
# image pixel (each color block on the following subplot), so the
8483
# domain that is contoured does not extend beyond these pixel centers.
85-
im = axs[3].imshow(Z, interpolation='nearest', extent=extent,
86-
cmap=cmap, norm=norm)
84+
im = axs[3].imshow(Z, interpolation='nearest', extent=extent, cmap=cmap, norm=norm)
8785
axs[3].contour(Z, levels, colors='k', origin='image', extent=extent)
8886
ylim = axs[3].get_ylim()
8987
axs[3].set_ylim(ylim[::-1])
9088
axs[3].set_title("Origin from rc, reversed y-axis")
91-
fig.colorbar(im, ax=axs[3])
89+
axs[3].colorbar(im)
9290

9391
fig.tight_layout()
9492
plt.show()

galleries/examples/images_contours_and_fields/contourf_demo.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -96,8 +96,8 @@
9696
fig, axs = plt.subplots(2, 2, layout="constrained")
9797

9898
for ax, extend in zip(axs.flat, extends):
99-
cs = ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend)
100-
fig.colorbar(cs, ax=ax, shrink=0.9)
99+
ax.contourf(X, Y, Z, levels, cmap=cmap, extend=extend)
100+
ax.colorbar(shrink=0.9)
101101
ax.set_title("extend = %s" % extend)
102102
ax.locator_params(nbins=4)
103103

galleries/examples/images_contours_and_fields/contourf_log.py

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -36,16 +36,16 @@
3636
# Automatic selection of levels works; setting the
3737
# log locator tells contourf to use a log scale:
3838
fig, ax = plt.subplots()
39-
cs = ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap="PuBu_r")
39+
ax.contourf(X, Y, z, locator=ticker.LogLocator(), cmap="PuBu_r")
4040

4141
# Alternatively, you can manually set the levels
4242
# and the norm:
4343
# lev_exp = np.arange(np.floor(np.log10(z.min())-1),
4444
# np.ceil(np.log10(z.max())+1))
4545
# levs = np.power(10, lev_exp)
46-
# cs = ax.contourf(X, Y, z, levs, norm=colors.LogNorm())
46+
# ax.contourf(X, Y, z, levs, norm=colors.LogNorm())
4747

48-
cbar = fig.colorbar(cs)
48+
ax.colorbar()
4949

5050
plt.show()
5151

galleries/examples/images_contours_and_fields/image_masked.py

Lines changed: 15 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -42,29 +42,26 @@
4242
fig, (ax1, ax2) = plt.subplots(nrows=2, figsize=(6, 5.4))
4343

4444
# plot using 'continuous' colormap
45-
im = ax1.imshow(Zm, interpolation='bilinear',
46-
cmap=palette,
47-
norm=colors.Normalize(vmin=-1.0, vmax=1.0),
48-
aspect='auto',
49-
origin='lower',
50-
extent=[x0, x1, y0, y1])
45+
ax1.imshow(Zm, interpolation='bilinear',
46+
cmap=palette,
47+
norm=colors.Normalize(vmin=-1.0, vmax=1.0),
48+
aspect='auto',
49+
origin='lower',
50+
extent=[x0, x1, y0, y1])
51+
ax1.colorbar(extend='both', shrink=0.9, label='uniform')
5152
ax1.set_title('Green=low, Red=high, Blue=masked')
52-
cbar = fig.colorbar(im, extend='both', shrink=0.9, ax=ax1)
53-
cbar.set_label('uniform')
5453
ax1.tick_params(axis='x', labelbottom=False)
5554

5655
# Plot using a small number of colors, with unevenly spaced boundaries.
57-
im = ax2.imshow(Zm, interpolation='nearest',
58-
cmap=palette,
59-
norm=colors.BoundaryNorm([-1, -0.5, -0.2, 0, 0.2, 0.5, 1],
60-
ncolors=palette.N),
61-
aspect='auto',
62-
origin='lower',
63-
extent=[x0, x1, y0, y1])
56+
ax2.imshow(Zm, interpolation='nearest',
57+
cmap=palette,
58+
norm=colors.BoundaryNorm([-1, -0.5, -0.2, 0, 0.2, 0.5, 1],
59+
ncolors=palette.N),
60+
aspect='auto',
61+
origin='lower',
62+
extent=[x0, x1, y0, y1])
63+
ax2.colorbar(extend='both', spacing='proportional', shrink=0.9, label='proportional')
6464
ax2.set_title('With BoundaryNorm')
65-
cbar = fig.colorbar(im, extend='both', spacing='proportional',
66-
shrink=0.9, ax=ax2)
67-
cbar.set_label('proportional')
6865

6966
fig.suptitle('imshow, with out-of-range and masked data')
7067
plt.show()

galleries/examples/images_contours_and_fields/irregulardatagrid.py

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@
5959
ax1.contour(xi, yi, zi, levels=14, linewidths=0.5, colors='k')
6060
cntr1 = ax1.contourf(xi, yi, zi, levels=14, cmap="RdBu_r")
6161

62-
fig.colorbar(cntr1, ax=ax1)
62+
ax1.colorbar(cntr1)
6363
ax1.plot(x, y, 'ko', ms=3)
6464
ax1.set(xlim=(-2, 2), ylim=(-2, 2))
6565
ax1.set_title('grid and contour (%d points, %d grid points)' %
@@ -74,7 +74,7 @@
7474
ax2.tricontour(x, y, z, levels=14, linewidths=0.5, colors='k')
7575
cntr2 = ax2.tricontourf(x, y, z, levels=14, cmap="RdBu_r")
7676

77-
fig.colorbar(cntr2, ax=ax2)
77+
ax2.colorbar(cntr2)
7878
ax2.plot(x, y, 'ko', ms=3)
7979
ax2.set(xlim=(-2, 2), ylim=(-2, 2))
8080
ax2.set_title('tricontour (%d points)' % npts)

0 commit comments

Comments
 (0)