8000 FIX: inverted colorbar ticks by jklymak · Pull Request #13563 · matplotlib/matplotlib · GitHub
[go: up one dir, main page]

Skip to content

FIX: inverted colorbar ticks #13563

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 1 commit into from
Mar 2, 2019
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
9 changes: 7 additions & 2 deletions lib/matplotlib/colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -238,6 +238,9 @@ def __init__(self, colorbar):
super().__init__(nbins=nbins, steps=steps)

def tick_values(self, vmin, vmax):
# flip if needed:
if vmin > vmax:
vmin, vmax = vmax, vmin
vmin = max(vmin, self._colorbar.norm.vmin)
vmax = min(vmax, self._colorbar.norm.vmax)
ticks = super().tick_values(vmin, vmax)
Expand Down Expand Up @@ -295,8 +298,10 @@ def __init__(self, colorbar, *args, **kwargs):
super().__init__(*args, **kwargs)

def tick_values(self, vmin, vmax):
vmin = self._colorbar.norm.vmin
vmax = self._colorbar.norm.vmax
if vmin > vmax:
vmin, vmax = vmax, vmin
vmin = max(vmin, self._colorbar.norm.vmin)
vmax = min(vmax, self._colorbar.norm.vmax)
ticks = super().tick_values(vmin, vmax)
rtol = (np.log10(vmax) - np.log10(vmin)) * 1e-10
ticks = ticks[(np.log10(ticks) >= np.log10(vmin) - rtol) &
Expand Down
22 changes: 21 additions & 1 deletion lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -514,8 +514,28 @@ def test_colorbar_scale_reset():
def test_colorbar_get_ticks():
with rc_context({'_internal.classic_mode': False}):

fig, ax = plt. subplots()
fig, ax = plt.subplots()
np.random.seed(19680801)
pc = ax.pcolormesh(np.random.rand(30, 30))
cb = fig.colorbar(pc)
np.testing.assert_allclose(cb.get_ticks(), [0.2, 0.4, 0.6, 0.8])


def test_colorbar_inverted_ticks():
fig, axs = plt.subplots(2)
ax = axs[0]
pc = ax.pcolormesh(10**np.arange(1, 5).reshape(2, 2), norm=LogNorm())
cbar = fig.colorbar(pc, ax=ax, extend='both')
ticks = cbar.get_ticks()
cbar.ax.invert_yaxis()
np.testing.assert_allclose(ticks, cbar.get_ticks())

ax = axs[1]
pc = ax.pcolormesh(np.arange(1, 5).reshape(2, 2))
cbar = fig.colorbar(pc, ax=ax, extend='both')
cbar.minorticks_on()
ticks = cbar.get_ticks()
minorticks = cbar.get_ticks(minor=True)
cbar.ax.invert_yaxis()
np.testing.assert_allclose(ticks, cbar.get_ticks())
np.testing.assert_allclose(minorticks, cbar.get_ticks(minor=True))
0