|
11 | 11 | import matplotlib.category # Register category unit converter as side effect. |
12 | 12 | import matplotlib.cbook as cbook |
13 | 13 | import matplotlib.collections as mcoll |
| 14 | +import matplotlib.colorbar as mcolorbar # noqa: F401, needed for docstring substitution |
14 | 15 | import matplotlib.colorizer as mcolorizer |
15 | 16 | import matplotlib.colors as mcolors |
16 | 17 | import matplotlib.contour as mcontour |
@@ -355,6 +356,74 @@ def legend(self, *args, **kwargs): |
355 | 356 | def _remove_legend(self, legend): |
356 | 357 | self.legend_ = None |
357 | 358 |
|
| 359 | + @_docstring.interpd |
| 360 | + def colorbar(self, mappable=None, *, use_gridspec=True, **kwargs): |
| 361 | + """ |
| 362 | + Add a colorbar next to the Axes. |
| 363 | +
|
| 364 | + Parameters |
| 365 | + ---------- |
| 366 | + mappable : `matplotlib.colorizer.ColorizingArtist`, optional |
| 367 | + The `.ColorizingArtist` (i.e., `.AxesImage`, `.ContourSet`, etc.) described |
| 368 | + by this colorbar. |
| 369 | +
|
| 370 | + If not given, the mappable is inferred from the Axes. If there is exactly |
| 371 | + one image or collection, this is used as mappable. Otherwise, an error is |
| 372 | + raised and you must specify the mappable explicitly. |
| 373 | +
|
| 374 | + Note that one can create a `.colorizer.ColorizingArtist` "on-the-fly" |
| 375 | + to generate colorbars not attached to a previously drawn artist, e.g. |
| 376 | +
|
| 377 | + :: |
| 378 | +
|
| 379 | + cr = colorizer.Colorizer(norm=norm, cmap=cmap) |
| 380 | + ax.colorbar(colorizer.ColorizingArtist(cr)) |
| 381 | +
|
| 382 | + Returns |
| 383 | + ------- |
| 384 | + colorbar : `~matplotlib.colorbar.Colorbar` |
| 385 | +
|
| 386 | + Other Parameters |
| 387 | + ---------------- |
| 388 | + use_gridspec : bool, optional |
| 389 | + If *ax* is positioned with a subplotspec and *use_gridspec* |
| 390 | + is ``True``, then *cax* is also positioned with a subplotspec. |
| 391 | +
|
| 392 | + %(_make_axes_kw_doc)s |
| 393 | + %(_colormap_kw_doc)s |
| 394 | +
|
| 395 | + Notes |
| 396 | + ----- |
| 397 | + This method is a convenience shortcut if you want to place a colorbar |
| 398 | + right next to an Axes. ``ax.colorbar(mappable)`` is equivalent to |
| 399 | + ``fig.colorbar(mappable, ax=ax)``. In particular, if you have exactly one |
| 400 | + mappable in the Axes, the general :: |
| 401 | +
|
| 402 | + im = ax.imshow(data) |
| 403 | + fig.colorbar(im, ax=ax) |
| 404 | +
|
| 405 | + can be written more concisely as :: |
| 406 | +
|
| 407 | + ax.imshow(data) |
| 408 | + ax.colorbar() |
| 409 | +
|
| 410 | + Use `.Figure.colorbar` if you need more control on placing the colorbar. |
| 411 | + """ |
| 412 | + if mappable is None: |
| 413 | + # autodetect the mappable |
| 414 | + colormapped_artists = [*self.images, *self.collections] |
| 415 | + if len(colormapped_artists) != 1: |
| 416 | + raise RuntimeError( |
| 417 | + "Axes.colormap() requires exactly one colormapped Artist in the " |
| 418 | + f"Axes, but found {len(colormapped_artists)}. In ambiguous cases, " |
| 419 | + "please specify the mappable for the colorbar explicitly." |
| 420 | + ) |
| 421 | + mappable = colormapped_artists[0] |
| 422 | + |
| 423 | + return self.figure.colorbar( |
| 424 | + mappable, ax=self, use_gridspec=use_gridspec, **kwargs |
| 425 | + ) |
| 426 | + |
358 | 427 | def inset_axes(self, bounds, *, transform=None, zorder=5, **kwargs): |
359 | 428 | """ |
360 | 429 | Add a child inset Axes to this existing Axes. |
|
0 commit comments