Skip to content

Commit d74f0a7

Browse files
dstansbymeeseeksmachine
authored andcommitted
Backport PR #21872: FIX: colorbars with NoNorm
1 parent 514fbb1 commit d74f0a7

File tree

3 files changed

+178
-7
lines changed

3 files changed

+178
-7
lines changed

lib/matplotlib/colorbar.py

Lines changed: 9 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -817,6 +817,12 @@ def _get_ticker_locator_formatter(self):
817817
b = self.norm.boundaries
818818
if locator is None:
819819
locator = ticker.FixedLocator(b, nbins=10)
820+
elif isinstance(self.norm, colors.NoNorm):
821+
if locator is None:
822+
# put ticks on integers between the boundaries of NoNorm
823+
nv = len(self._values)
824+
base = 1 + int(nv / 10)
825+
locator = ticker.IndexLocator(base=base, offset=.5)
820826
elif self.boundaries is not None:
821827
b = self._boundaries[self._inside]
822828
if locator is None:
@@ -828,11 +834,6 @@ def _get_ticker_locator_formatter(self):
828834
locator = self._long_axis().get_major_locator()
829835
if minorlocator is None:
830836
minorlocator = self._long_axis().get_minor_locator()
831-
if isinstance(self.norm, colors.NoNorm):
832-
# default locator:
833-
nv = len(self._values)
834-
base = 1 + int(nv / 10)
835-
locator = ticker.IndexLocator(base=base, offset=0)
836837

837838
if minorlocator is None:
838839
minorlocator = ticker.NullLocator()
@@ -1090,6 +1091,9 @@ def _process_values(self):
10901091
# otherwise values are set from the boundaries
10911092
if isinstance(self.norm, colors.BoundaryNorm):
10921093
b = self.norm.boundaries
1094+
elif isinstance(self.norm, colors.NoNorm):
1095+
# NoNorm has N blocks, so N+1 boundaries, centered on integers:
1096+
b = np.arange(self.cmap.N + 1) - .5
10931097
else:
10941098
# otherwise make the boundaries from the size of the cmap:
10951099
N = self.cmap.N + 1
Lines changed: 151 additions & 0 deletions
Loading

lib/matplotlib/tests/test_colorbar.py

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -7,10 +7,11 @@
77
from matplotlib import rc_context
88
from matplotlib.testing.decorators import image_comparison
99
import matplotlib.pyplot as plt
10-
from matplotlib.colors import BoundaryNorm, LogNorm, PowerNorm, Normalize
10+
from matplotlib.colors import (
11+
BoundaryNorm, LogNorm, PowerNorm, Normalize, NoNorm
12+
)
1113
from matplotlib.colorbar import Colorbar
1214
from matplotlib.ticker import FixedLocator
13-
1415
from matplotlib.testing.decorators import check_figures_equal
1516

1617

@@ -911,3 +912,18 @@ def test_negative_boundarynorm():
911912
cb = fig.colorbar(cm.ScalarMappable(cmap=cmap, norm=norm), cax=ax)
912913
np.testing.assert_allclose(cb.ax.get_ylim(), [clevs[0], clevs[-1]])
913914
np.testing.assert_allclose(cb.ax.get_yticks(), clevs)
915+
916+
917+
@image_comparison(['nonorm_colorbars.svg'], remove_text=False,
918+
style='mpl20')
919+
def test_nonorm():
920+
plt.rcParams['svg.fonttype'] = 'none'
921+
data = [1, 2, 3, 4, 5]
922+
923+
fig, ax = plt.subplots(figsize=(6, 1))
924+
fig.subplots_adjust(bottom=0.5)
925+
926+
norm = NoNorm(vmin=min(data), vmax=max(data))
927+
cmap = cm.get_cmap("viridis", len(data))
928+
mappable = cm.ScalarMappable(norm=norm, cmap=cmap)
929+
cbar = fig.colorbar(mappable, cax=ax, orientation="horizontal")

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy