@@ -355,6 +355,73 @@ def legend(self, *args, **kwargs):
355355 def _remove_legend (self , legend ):
356356 self .legend_ = None
357357
358+ def colorbar (self , mappable = None , * , use_gridspec = True , ** kwargs ):
359+ """
360+ Add a colorbar next to the Axes.
361+
362+ Parameters
363+ ----------
364+ mappable : `matplotlib.colorizer.ColorizingArtist`, optional
365+ The `matplotlib.colorizer.ColorizingArtist` (i.e., `.AxesImage`,
366+ `.ContourSet`, etc.) described by this colorbar.
367+
368+ If not given, the mappable is inferred from the Axes. If there is exactly
369+ one image or collection, this is used as mappable. Otherwise, an error is
370+ raised and you must specify the mappable explicitly.
371+
372+ Note that one can create a `.colorizer.ColorizingArtist` "on-the-fly"
373+ to generate colorbars not attached to a previously drawn artist, e.g.
374+ ::
375+
376+ cr = colorizer.Colorizer(norm=norm, cmap=cmap)
377+ ax.colorbar(colorizer.ColorizingArtist(cr))
378+
379+ Returns
380+ -------
381+ colorbar : `~matplotlib.colorbar.Colorbar`
382+
383+ Other Parameters
384+ ----------------
385+ use_gridspec : bool, optional
386+ If *ax* is positioned with a subplotspec and *use_gridspec*
387+ is ``True``, then *cax* is also positioned with a subplotspec.
388+
389+ %(_make_axes_kw_doc)s
390+ %(_colormap_kw_doc)s
391+
392+ Notes
393+ -----
394+ This method is a convenience shortcut for the most common case that you have
395+ one mappable in the Axes and want to create a colorbar for it, which is
396+ located right beside the Axes.
397+
398+ ::
399+ ax.imshow(data)
400+ ax.colorbar()
401+
402+ is equivalent to
403+
404+ ::
405+ im = ax.imshow(data)
406+ fig.colorbar(im, ax=ax)
407+
408+ Use `.Figure.colorbar` if you need more control on placing the colorbar.
409+ """
410+ if mappable is None :
411+ # autodetect the mappable
412+ colormapped_artists = [* self .images , * self .collections ]
413+ if len (colormapped_artists ) != 1 :
414+ raise RuntimeError (
415+ "Axes.colormap() requires exactly one colormapped Artist in the "
416+ f"Axes, but found { len (colormapped_artists )} . In ambiguous cases, "
417+ "please specify the mappable for the colorbar explicitly."
418+ )
419+ mappable = colormapped_artists [0 ]
420+
421+ return self .figure .colorbar (
422+ mappable , ax = self , use_gridspec = use_gridspec , ** kwargs
423+ )
424+
358425 def inset_axes (self , bounds , * , transform = None , zorder = 5 , ** kwargs ):
359426 """
360427 Add a child inset Axes to this existing Axes.
0 commit comments