10000 Fix colorbars for different norms by jklymak · Pull Request #16370 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

Fix colorbars for different norms #16370

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

Closed
wants to merge 4 commits into from
Closed
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
9 changes: 4 additions & 5 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -584,9 +584,8 @@ def _use_auto_colorbar_locator(self):
Return if we should use an adjustable tick locator or a fixed
one. (check is used twice so factored out here...)
"""
contouring = self.boundaries is not None and self.spacing == 'uniform'
return (type(self.norm) in [colors.Normalize, colors.LogNorm]
and not contouring)
# contouring = self.boundaries is not None and self.spacing == 'uniform'
return (type(self.norm) in [colors.Normalize, colors.LogNorm])

def _reset_locator_formatter_scale(self):
"""
Expand Down Expand Up @@ -1119,11 +1118,11 @@ def _mesh(self):
else:
y = self._proportional_y()
xmid = np.array([0.5])
try:
if self._use_auto_colorbar_locator():
y = norm.inverse(y)
x = norm.inverse(x)
xmid = norm.inverse(xmid)
except ValueError:
else:
# occurs for norms that don't have an inverse, in
# which case manually scale:
dv = self.vmax - self.vmin
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
16 changes: 15 additions & 1 deletion lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -373,7 +373,6 @@ def test_colorbar_get_ticks():
data = np.arange(1200).reshape(30, 40)
levels = [0, 200, 400, 600, 800, 1000, 1200]

plt.subplot()
plt.contourf(data, levels=levels)

# testing getter for user set ticks
Expand Down Expand Up @@ -581,3 +580,18 @@ def test_colorbar_int(clim):
im = ax.imshow([[*map(np.int16, clim)]])
fig.colorbar(im)
assert (im.norm.vmin, im.norm.vmax) == clim


@image_comparison(
baseline_images=['colorbar_norms'], extensions=['png'])
def test_cbar_norms():
# Test colormaps with different norms
norms = [mcolors.Normalize(vmin=-1, vmax=2),
mcolors.BoundaryNorm(boundaries=[-1, 0, 1], ncolors=4),
mcolors.LogNorm(vmin=1, vmax=1e4),
mcolors.PowerNorm(gamma=0.2, vmin=0.1, vmax=0.6),
mcolors.SymLogNorm(1, vmin=-10, vmax=10),
mcolors.TwoSlopeNorm(1, vmin=0, vmax=2)]
fig, axs = plt.subplots(ncols=len(norms), constrained_layout=True)
for ax, norm in zip(axs, norms):
fig.colorbar(cm.ScalarMappable(norm=norm, cmap='viridis'), cax=ax, extend='both')
0