Skip to content

Commit 8298a60

Browse files
authored
fix doctests (#139)
1 parent 303dad4 commit 8298a60

File tree

1 file changed

+22
-75
lines changed

1 file changed

+22
-75
lines changed

mplotutils/_colorbar.py

Lines changed: 22 additions & 75 deletions
Original file line numberDiff line numberDiff line change
@@ -170,24 +170,32 @@ def colorbar(
170170
>>> import mplotutils as mpu
171171
>>> import cartopy.crs as ccrs
172172
173+
>>> # example data
174+
>>> da = mpu.sample_dataarray(36, 18)
175+
>>> # plotting options
176+
>>> opt = {"add_colorbar": False, "transform": ccrs.PlateCarree()}
177+
173178
>>> # =========================
174179
>>> # example with 1 axes
175180
176181
>>> f, ax = plt.subplots(subplot_kw={"projection": ccrs.PlateCarree()})
177-
>>> h = ax.pcolormesh([[0, 1]])
178-
>>> ax.coastlines()
179-
>>> mpu.colorbar(h, ax)
182+
>>> ax.coastlines() # doctest: +SKIP
180183
>>> ax.set_global()
184+
>>> h = da.plot(ax=ax, **opt)
185+
186+
>>> cbar = mpu.colorbar(h, ax)
181187
182188
>>> # =========================
183189
>>> # example with 2 axes
184190
185191
>>> f, axs = plt.subplots(2, 1, subplot_kw={"projection": ccrs.PlateCarree()})
186192
187193
>>> for ax in axs:
188-
>>> ax.coastlines()
189-
>>> ax.set_global()
190-
>>> h = ax.pcolormesh([[0, 1]])
194+
... ax.coastlines() # doctest: +SKIP
195+
... ax.set_global()
196+
197+
>>> h = da.plot(ax=axs[0], **opt)
198+
>>> h = da.plot(ax=axs[1], **opt)
191199
192200
>>> cbar = mpu.colorbar(h, axs)
193201
@@ -197,15 +205,15 @@ def colorbar(
197205
>>> f, axs = plt.subplots(3, 1, subplot_kw={"projection": ccrs.PlateCarree()})
198206
199207
>>> for ax in axs:
200-
>>> ax.coastlines()
201-
>>> ax.set_global()
208+
... ax.coastlines() # doctest: +SKIP
209+
... ax.set_global()
202210
203-
>>> h0 = ax.pcolormesh([[0, 1]])
204-
>>> h1 = ax.pcolormesh([[0, 1]])
205-
>>> h2 = ax.pcolormesh([[0, 1]], cmap='Blues')
211+
>>> h0 = da.plot(ax=axs[0], **opt)
212+
>>> h1 = da.plot(ax=axs[1], **opt)
213+
>>> h2 = da.plot(ax=axs[2], cmap='Blues', **opt)
206214
207-
>>> cbar = mpu.colorbar(h, [axs[0], axs[1]], size=0.05)
208-
>>> cbar = mpu.colorbar(h, axs[2], size=0.05)
215+
>>> cbar = mpu.colorbar(h0, [axs[0], axs[1]], size=0.05)
216+
>>> cbar = mpu.colorbar(h2, axs[2], size=0.05)
209217
210218
Notes
211219
-----
@@ -244,7 +252,7 @@ def colorbar(
244252
# ensure mpu.colorbar does not change the current axes
245253
plt.sca(gca)
246254

247-
cbar = plt.colorbar(mappable, orientation=orientation, cax=cbax, **kwargs)
255+
cbar = f.colorbar(mappable, orientation=orientation, cax=cbax, **kwargs)
248256

249257
if orientation == "vertical":
250258
func = _resize_colorbar_vert(
@@ -298,37 +306,6 @@ def _resize_colorbar_vert(
298306
shift="symmetric",
299307
shrink=None,
300308
):
301-
"""
302-
automatically resize colorbars on draw
303-
304-
see 'colorbar'
305-
306-
Examples
307-
--------
308-
>>> import matplotlib.pyplot as plt
309-
>>> import mplotutils as mpu
310-
>>> import cartopy.crs as ccrs
311-
312-
>>> f = plt.figure()
313-
>>> ax = plt.axes(projection=ccrs.PlateCarree())
314-
>>> h = ax.pcolormesh([[0, 1]])
315-
316-
>>> ax.coastlines()
317-
318-
>>> cbax = f.add_axes([0, 0, 0.1, 0.1])
319-
>>> cbar = plt.colorbar(h, orientation='vertical', cax=cbax)
320-
321-
>>> func = mpu._resize_colorbar_vert(cbax, ax)
322-
>>> f.canvas.mpl_connect('draw_event', func)
323-
324-
>>> ax.set_global()
325-
326-
>>> plt.draw()
327-
328-
See Also
329-
--------
330-
_resize_colorbar_horz
331-
"""
332309

333310
shift, shrink = _parse_shift_shrink(shift, shrink)
334311

@@ -384,36 +361,6 @@ def _resize_colorbar_horz(
384361
shift="symmetric",
385362
shrink=None,
386363
):
387-
"""
388-
automatically resize colorbars on draw
389-
390-
see 'colorbar'
391-
392-
Examples
393-
--------
394-
>>> import matplotlib.pyplot as plt
395-
>>> import mplotutils as mpu
396-
>>> import cartopy.crs as ccrs
397-
398-
>>> f = plt.figure()
399-
>>> ax = plt.axes(projection=ccrs.PlateCarree())
400-
401-
>>> ax.coastlines()
402-
403-
>>> cbax = f.add_axes([0, 0, 0.1, 0.1])
404-
>>> cbar = plt.colorbar(h, orientation='horizontal', cax=cbax)
405-
406-
>>> func = mpu._resize_colorbar_horz(cbax, ax)
407-
>>> f.canvas.mpl_connect('draw_event', func)
408-
409-
>>> ax.set_global()
410-
411-
>>> plt.draw()
412-
413-
See Also
414-
--------
415-
_resize_colorbar_vert
416-
"""
417364

418365
shift, shrink = _parse_shift_shrink(shift, shrink)
419366

0 commit comments

Comments
 (0)