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

Skip to content

FIX: colorbars with NoNorm #21872

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 2 commits into from
Dec 8, 2021
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 9 additions & 5 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -824,6 +824,12 @@ def _get_ticker_locator_formatter(self):
b = self.norm.boundaries
if locator is None:
locator = ticker.FixedLocator(b, nbins=10)
elif isinstance(self.norm, colors.NoNorm):
if locator is None:
# put ticks on integers between the boundaries of NoNorm
nv = len(self._values)
base = 1 + int(nv / 10)
locator = ticker.IndexLocator(base=base, offset=.5)
elif self.boundaries is not None:
b = self._boundaries[self._inside]
if locator is None:
Expand All @@ -835,11 +841,6 @@ def _get_ticker_locator_formatter(self):
locator = self._long_axis().get_major_locator()
if minorlocator is None:
minorlocator = self._long_axis().get_minor_locator()
if isinstance(self.norm, colors.NoNorm):
# default locator:
nv = len(self._values)
base = 1 + int(nv / 10)
locator = ticker.IndexLocator(base=base, offset=0)

if minorlocator is None:
minorlocator = ticker.NullLocator()
Expand Down Expand Up @@ -1097,6 +1098,9 @@ def _process_values(self):
# otherwise values are set from the boundaries
if isinstance(self.norm, colors.BoundaryNorm):
b = self.norm.boundaries
elif isinstance(self.norm, colors.NoNorm):
# NoNorm has N blocks, so N+1 boundaries, centered on integers:
b = np.arange(self.cmap.N + 1) - .5
else:
# otherwise make the boundaries from the size of the cmap:
N = self.cmap.N + 1
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
20 changes: 18 additions & 2 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -7,10 +7,11 @@
from matplotlib import rc_context
from matplotlib.testing.decorators import image_comparison
import matplotlib.pyplot as plt
from matplotlib.colors import BoundaryNorm, LogNorm, PowerNorm, Normalize
from matplotlib.colors import (
BoundaryNorm, LogNorm, PowerNorm, Normalize, NoNorm
)
from matplotlib.colorbar import Colorbar
from matplotlib.ticker import FixedLocator

from matplotlib.testing.decorators import check_figures_equal


Expand Down Expand Up @@ -913,3 +914,18 @@ def test_negative_boundarynorm():
cb = fig.colorbar(cm.ScalarMappable(cmap=cmap, norm=norm), cax=ax)
np.testing.assert_allclose(cb.ax.get_ylim(), [clevs[0], clevs[-1]])
np.testing.assert_allclose(cb.ax.get_yticks(), clevs)


@image_comparison(['nonorm_colorbars.svg'], remove_text=False,
style='mpl20')
def test_nonorm():
plt.rcParams['svg.fonttype'] = 'none'
data = [1, 2, 3, 4, 5]

fig, ax = plt.subplots(figsize=(6, 1))
fig.subplots_adjust(bottom=0.5)

norm = NoNorm(vmin=min(data), vmax=max(data))
cmap = cm.get_cmap("viridis", len(data))
mappable = cm.ScalarMappable(norm=norm, cmap=cmap)
cbar = fig.colorbar(mappable, cax=ax, orientation="horizontal")