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

Skip to content

Commit 9fd22ac

Browse files
committed
Add support for units in vmin/vmax
1 parent a3312d7 commit 9fd22ac

File tree

2 files changed

+17
-11
lines changed

2 files changed

+17
-11
lines changed

lib/matplotlib/cm.py

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -484,10 +484,14 @@ def set_clim(self, vmin=None, vmax=None):
484484
vmin, vmax = vmin
485485
except (TypeError, ValueError):
486486
pass
487-
if vmin is not None:
488-
self.norm.vmin = colors._sanitize_extrema(vmin)
489-
if vmax is not None:
490-
self.norm.vmax = colors._sanitize_extrema(vmax)
487+
488+
def _process_lim(lim):
489+
if self._converter is not None:
490+
lim = self._converter.convert(lim, self._units, axis=None)
491+
return colors._sanitize_extrema(lim)
492+
493+
self.norm.vmin = _process_lim(vmin)
494+
self.norm.vmax = _process_lim(vmax)
491495

492496
def get_alpha(self):
493497
"""

lib/matplotlib/tests/test_units.py

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -305,22 +305,24 @@ def test_mappable_units(quantity_converter):
305305
munits.registry[Quantity] = quantity_converter
306306
x, y = np.meshgrid([0, 1], [0, 1])
307307
data = Quantity(np.arange(4).reshape(2, 2), 'hours')
308+
vmin = Quantity(1, "hours") # Test a limit different from min of the data
309+
vmax = Quantity(3 * 60, "minutes") # Test a different unit to the data
308310

309-
fig, axs = plt.subplots(nrows=2, ncols=2)
311+
fig, axs = plt.subplots(nrows=2, ncols=2, constrained_layout=True)
310312

311313
# imshow
312314
ax = axs[0, 0]
313-
mappable = ax.imshow(data, origin='lower')
314-
cbar = fig.colorbar(mappable, ax=ax)
315+
mappable = ax.imshow(data, origin='lower', vmin=vmin, vmax=vmax)
316+
cbar = fig.colorbar(mappable, ax=ax, extend="min")
315317

316318
# pcolor
317319
ax = axs[0, 1]
318-
mappable = ax.pcolor(x, y, data)
319-
fig.colorbar(mappable, ax=ax)
320+
mappable = ax.pcolor(x, y, data, vmin=vmin, vmax=vmax)
321+
fig.colorbar(mappable, ax=ax, extend="min")
320322

321323
# pcolormesh + horizontal colorbar
322324
ax = axs[1, 0]
323-
mappable = ax.pcolormesh(x, y, data)
324-
fig.colorbar(mappable, ax=ax, orientation="horizontal")
325+
mappable = ax.pcolormesh(x, y, data, vmin=vmin, vmax=vmax)
326+
fig.colorbar(mappable, ax=ax, orientation="horizontal", extend="min")
325327

326328
axs[1, 1].axis("off")

0 commit comments

Comments
 (0)