Skip to content

Commit

Permalink
[ENH]: Use new style format strings for colorbar ticks
Browse files Browse the repository at this point in the history
Fixes #21378
  • Loading branch information
vfdev-5 committed Nov 4, 2021
1 parent f96eb63 commit c5d0054
Show file tree
Hide file tree
Showing 2 changed files with 13 additions and 12 deletions.
23 changes: 12 additions & 11 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -352,6 +352,7 @@ class Colorbar:
ticks : `~matplotlib.ticker.Locator` or array-like of float
format : str or `~matplotlib.ticker.Formatter`
If string, it supports only new-style format, e.g. ``"{x:.2e}"``.
drawedges : bool
Expand Down Expand Up @@ -471,7 +472,6 @@ def __init__(self, ax, mappable=None, *, cmap=None,

self.locator = None
self.minorlocator = None
self.formatter = None
self.__scale = None # linear, log10 for now. Hopefully more?

if ticklocation == 'auto':
Expand All @@ -486,10 +486,7 @@ def __init__(self, ax, mappable=None, *, cmap=None,
else:
self.locator = ticks # Handle default in _ticker()

if isinstance(format, str):
self.formatter = ticker.FormatStrFormatter(format)
else:
self.formatter = format # Assume it is a Formatter or None
self.formatter = format
self.draw_all()

if isinstance(mappable, contour.ContourSet) and not mappable.filled:
Expand All @@ -510,6 +507,15 @@ def __init__(self, ax, mappable=None, *, cmap=None,
# Set the cla function to the cbar's method to override it
self.ax.cla = self._cbar_cla

@property
def formatter(self):
return self._long_axis().get_major_formatter()

@formatter.setter
def formatter(self, fmt):
if fmt is not None:
self._long_axis().set_major_formatter(fmt)

def _cbar_cla(self):
"""Function to clear the interactive colorbar state."""
for x in self._interactive_funcs:
Expand Down Expand Up @@ -798,7 +804,6 @@ def update_ticks(self):
self._get_ticker_locator_formatter()
self._long_axis().set_major_locator(self.locator)
self._long_axis().set_minor_locator(self.minorlocator)
self._long_axis().set_major_formatter(self.formatter)

def _get_ticker_locator_formatter(self):
"""
Expand All @@ -811,7 +816,6 @@ def _get_ticker_locator_formatter(self):
Called by update_ticks...
"""
locator = self.locator
formatter = self.formatter
minorlocator = self.minorlocator
if isinstance(self.norm, colors.BoundaryNorm):
b = self.norm.boundaries
Expand All @@ -837,11 +841,7 @@ def _get_ticker_locator_formatter(self):
if minorlocator is None:
minorlocator = ticker.NullLocator()

if formatter is None:
formatter = self._long_axis().get_major_formatter()

self.locator = locator
self.formatter = formatter
self.minorlocator = minorlocator
_log.debug('locator: %r', locator)

Expand Down Expand Up @@ -1162,6 +1162,7 @@ def _reset_locator_formatter_scale(self):
self._process_values()
self.locator = None
self.minorlocator = None
# TODO: what is the impact of that ?
self.formatter = None
if (self.boundaries is not None or
isinstance(self.norm, colors.BoundaryNorm)):
Expand Down
2 changes: 1 addition & 1 deletion lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -550,7 +550,7 @@ def test_colorbar_format():

fig, ax = plt.subplots()
im = ax.imshow(z)
cbar = fig.colorbar(im, format='%4.2e')
cbar = fig.colorbar(im, format='{x:.2e}')
fig.canvas.draw()
assert cbar.ax.yaxis.get_ticklabels()[4].get_text() == '8.00e+04'

Expand Down

0 comments on commit c5d0054

Please sign in to comment.