Thanks to visit codestin.com
Credit goes to github.com

Skip to content

ENH: add colorbar.set_aspect #22103

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 2 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
30 changes: 30 additions & 0 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down