8000 FIX: colorbars with NoNorm · matplotlib/matplotlib@8d3d03a · GitHub
[go: up one dir, main page]

Skip to content

Commit 8d3d03a

Browse files
tacaswelljklymak
andcommitted
FIX: colorbars with NoNorm
Closes #21870 This adds another special-case path for NoNorm and tweaks the contents of another. These special-cases are required because NoNorm (like BoundaryNorm) has a different return value than ever other Norm (it directly returns integers to index into the LUT rather than [0, 1] that is later mapped into the LUT. Co-authored-by: Jody Klymak <jklymak@gmail.com>
1 parent 2ccc67f commit 8d3d03a

File tree

3 files changed

+174
-3
lines changed

3 files changed

+174
-3
lines changed

lib/matplotlib/colorbar.py

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -839,7 +839,8 @@ def _get_ticker_locator_formatter(self):
839839
# default locator:
840840
nv = len(self._values)
841841
base = 1 + int(nv / 10)
842-
locator = ticker.IndexLocator(base=base, offset=0)
842+
# put ticks on integers between the boundaries of NoNorm...
843+
locator = ticker.IndexLocator(base=base, offset=.5)
843844

844845
if minorlocator is None:
845846
minorlocator = ticker.NullLocator()
@@ -1097,6 +1098,9 @@ def _process_values(self):
10971098
# otherwise values are set from the boundaries
10981099
if isinstance(self.norm, colors.BoundaryNorm):
10991100
b = self.norm.boundaries
1101+
elif isinstance(self.norm, colors.NoNorm):
1102+
# NoNorm has N blocks, so N+1 boundaries, centered on integers:
1103+
b = np.arange(self.cmap.N + 1) - .5
11001104
else:
11011105
# otherwise make the boundaries from the size of the cmap:
11021106
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

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

0 commit comments

Comments
 (0)
0