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

Skip to content

Commit f20a66a

Browse files
committed
Set units on colorbar axes
1 parent b786a55 commit f20a66a

File tree

2 files changed

+27
-3
lines changed

2 files changed

+27
-3
lines changed

lib/matplotlib/colorbar.py

Lines changed: 26 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -291,7 +291,7 @@ def __init__(self, ax, mappable=None, *, cmap=None,
291291
drawedges=False,
292292
extendfrac=None,
293293
extendrect=False,
294-
label='',
294+
label=None,
295295
location=None,
296296
):
297297

@@ -370,6 +370,7 @@ def __init__(self, ax, mappable=None, *, cmap=None,
370370
self.solids_patches = []
371371
self.lines = []
372372

373+
373374
for spine in self.ax.spines.values():
374375
spine.set_visible(False)
375376
self.outline = self.ax.spines['outline'] = _ColorbarSpine(self.ax)
@@ -391,8 +392,10 @@ def __init__(self, ax, mappable=None, *, cmap=None,
391392
orientation) if location is None else location
392393
self.ticklocation = ticklocation
393394

394-
self.set_label(label)
395+
if label is not None:
396+
self.set_label(label)
395397
self._reset_locator_formatter_scale()
398+
self._set_units_from_mappable()
396399

397400
if np.iterable(ticks):
398401
self._locator = ticker.FixedLocator(ticks, nbins=len(ticks))
@@ -1331,6 +1334,27 @@ def drag_pan(self, button, key, x, y):
13311334
elif self.orientation == 'vertical':
13321335
self.norm.vmin, self.norm.vmax = points[:, 1]
13331336

1337+
def _set_units_from_mappable(self):
1338+
"""
1339+
Set the colorbar locator and formatter if the mappable has units.
1340+
"""
1341+
self._units = self.mappable._units
1342+
self._converter = self.mappable._converter
1343+
if self._converter is not None:
1344+
1345+
axis = self._long_axis()
1346+
info = self._converter.axisinfo(self._units, axis)
1347+
1348+
if info is not None:
1349+
if info.majloc is not None:
1350+
self.locator = info.majloc
1351+
if info.minloc is not None:
1352+
self.minorlocator = info.minloc
1353+
if info.majfmt is not None:
1354+
self.formatter = info.majfmt
1355+
if info.minfmt is not None:
1356+
self.minorformatter = info.minfmt
1357+
13341358

13351359
ColorbarBase = Colorbar # Backcompat API
13361360

lib/matplotlib/colorbar.pyi

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -59,7 +59,7 @@ class Colorbar:
5959
drawedges: bool = ...,
6060
extendfrac: Literal["auto"] | float | Sequence[float] | None = ...,
6161
extendrect: bool = ...,
62-
label: str = ...,
62+
label: str | None = ...,
6363
location: Literal["left", "right", "top", "bottom"] | None = ...
6464
) -> None: ...
6565
@property

0 commit comments

Comments
 (0)