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

Skip to content

Commit 57dc5f7

Browse files
authored
Merge pull request #21872 from tacaswell/fix_nonorm
FIX: colorbars with NoNorm
2 parents 21ea3fb + 8266e76 commit 57dc5f7

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
@@ -824,6 +824,12 @@ def _get_ticker_locator_formatter(self):
824824
b = self.norm.boundaries
825825
if locator is None:
826826
locator = ticker.FixedLocator(b, nbins=10)
827+
elif isinstance(self.norm, colors.NoNorm):
828+
if locator is None:
829+
# put ticks on integers between the boundaries of NoNorm
830+
nv = len(self._values)
831+
base = 1 + int(nv / 10)
832+
locator = ticker.IndexLocator(base=base, offset=.5)
827833
elif self.boundaries is not None:
828834
b = self._boundaries[self._inside]
829835
if locator is None:
@@ -835,11 +841,6 @@ def _get_ticker_locator_formatter(self):
835841
locator = self._long_axis().get_major_locator()
836842
if minorlocator is None:
837843
minorlocator = self._long_axis().get_minor_locator()
838-
if isinstance(self.norm, colors.NoNorm):
839-
# default locator:
840-
nv = len(self._values)
841-
base = 1 + int(nv / 10)
842-
locator = ticker.IndexLocator(base=base, offset=0)
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)