Skip to content

Commit eed437a

Browse files
committed
STY: update with use of plt.subplots(), other readability edits
1 parent 8f9bc7b commit eed437a

File tree

1 file changed

+15
-14
lines changed

1 file changed

+15
-14
lines changed

examples/images_contours_and_fields/pcolormesh_levels.py

+15-14
Original file line numberDiff line numberDiff line change
@@ -18,7 +18,7 @@
1818
y, x = np.mgrid[slice(1, 5 + dy, dy),
1919
slice(1, 5 + dx, dx)]
2020

21-
z = np.sin(x) ** 10 + np.cos(10 + y * x) * np.cos(x)
21+
z = np.sin(x)**10 + np.cos(10 + y*x) * np.cos(x)
2222

2323
# x and y are bounds, so z should be the value *inside* those bounds.
2424
# Therefore, remove the last value from the z array.
@@ -31,22 +31,23 @@
3131
cmap = plt.get_cmap('PiYG')
3232
norm = BoundaryNorm(levels, ncolors=cmap.N, clip=True)
3333

34-
plt.subplot(2, 1, 1)
35-
im = plt.pcolormesh(x, y, z, cmap=cmap, norm=norm)
36-
plt.colorbar()
37-
# set the limits of the plot to the limits of the data
38-
plt.axis([x.min(), x.max(), y.min(), y.max()])
39-
plt.title('pcolormesh with levels')
34+
fig, (ax0, ax1) = plt.subplots(nrows=2)
35+
36+
im = ax0.pcolormesh(x, y, z, cmap=cmap, norm=norm)
37+
fig.colorbar(im, ax=ax0)
38+
ax0.set_title('pcolormesh with levels')
4039

4140

42-
plt.subplot(2, 1, 2)
4341
# contours are *point* based plots, so convert our bound into point
4442
# centers
45-
plt.contourf(x[:-1, :-1] + dx / 2.,
46-
y[:-1, :-1] + dy / 2., z, levels=levels,
47-
cmap=cmap)
48-
plt.colorbar()
49-
plt.title('contourf with levels')
50-
43+
cf = ax1.contourf(x[:-1, :-1] + dx/2.,
44+
y[:-1, :-1] + dy/2., z, levels=levels,
45+
cmap=cmap)
46+
fig.colorbar(cf, ax=ax1)
47+
ax1.set_title('contourf with levels')
48+
49+
# adjust spacing between subplots so `ax1` title and `ax0` tick labels
50+
# don't overlap
51+
fig.tight_layout()
5152

5253
plt.show()

0 commit comments

Comments
 (0)