diff --git a/lib/matplotlib/colorbar.py b/lib/matplotlib/colorbar.py index b8da91c05109..2c68e73e3400 100644 --- a/lib/matplotlib/colorbar.py +++ b/lib/matplotlib/colorbar.py @@ -1015,6 +1015,36 @@ def _set_scale(self, scale, **kwargs): else: self.__scale = scale + def set_aspect(self, aspect): + """ + Set ratio of the long axis to short axis. Note this only works for axes + created by `~.FigureBase.colorbar`, `~.colorbar.make_axes`, or + `~.colorbar.make_axes_grdispec`. User-created axes should be changed by + the user. + + Parameters: + ----------- + aspect : float + """ + if not hasattr(self.ax, '_colorbar_info'): + raise RuntimeError('Colorbar cannot change the aspect ratio of ' + 'user-defined axes.') + self.ax._colorbar_info['aspect'] = aspect + if self.orientation == 'horizontal': + aspect = 1 / aspect + self.ax.set_box_aspect(aspect) + self.ax.set_aspect('auto') + + def get_aspect(self): + """ + Get the ratio of long to short axis for the colorbar. + """ + if hasattr(self.ax, '_colorbar_info'): + return self.ax._colorbar_info['aspect'] + else: + return self.ax.get_box_aspect() + + def remove(self): """ Remove this colorbar from the figure.