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

Skip to content

Fix twoslopenorm colorbar #20582

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
Jul 13, 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
8 changes: 5 additions & 3 deletions lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1300,9 +1300,11 @@ def __call__(self, value, clip=None):

if not self.vmin <= self.vcenter <= self.vmax:
raise ValueError("vmin, vcenter, vmax must increase monotonically")
# note that we must extrapolate for tick locators:
result = np.ma.masked_array(
np.interp(result, [self.vmin, self.vcenter, self.vmax],
[0, 0.5, 1.]), mask=np.ma.getmask(result))
[0, 0.5, 1], left=-np.inf, right=np.inf),
mask=np.ma.getmask(result))
if is_scalar:
result = np.atleast_1d(result)[0]
return result
Expand All @@ -1313,8 +1315,8 @@ def inverse(self, value):
(vmin,), _ = self.process_value(self.vmin)
(vmax,), _ = self.process_value(self.vmax)
(vcenter,), _ = self.process_value(self.vcenter)

result = np.interp(value, [0, 0.5, 1.], [vmin, vcenter, vmax])
result = np.interp(value, [0, 0.5, 1], [vmin, vcenter, vmax],
left=-np.inf, right=np.inf)
return result


Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
5 changes: 3 additions & 2 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -776,11 +776,12 @@ def test_inset_colorbar_layout():
@image_comparison(['colorbar_twoslope.png'], remove_text=True,
style='mpl20')
def test_twoslope_colorbar():
# Note that the first tick = 20, and should be in the middle
# Note that the second tick = 20, and should be in the middle
# of the colorbar (white)
# There should be no tick right at the bottom, nor at the top.
fig, ax = plt.subplots()

norm = mcolors.TwoSlopeNorm(20, 0, 100)
norm = mcolors.TwoSlopeNorm(20, 5, 95)
pc = ax.pcolormesh(np.arange(1, 11), np.arange(1, 11),
np.arange(100).reshape(10, 10),
norm=norm, cmap='RdBu_r')
Expand Down
20 changes: 15 additions & 5 deletions tutorials/colors/colormapnorms.py
Original file line number Diff line number Diff line change
Expand Up @@ -277,7 +277,8 @@
# longitude depends on latitude.
ax.set_aspect(1 / np.cos(np.deg2rad(49)))
ax.set_title('TwoSlopeNorm(x)')
fig.colorbar(pcm, shrink=0.6)
cb = fig.colorbar(pcm, shrink=0.6)
cb.set_ticks([-500, 0, 1000, 2000, 3000, 4000])
plt.show()


Expand Down Expand Up @@ -312,7 +313,8 @@ def _inverse(x):
# ----------------------------------------------------------
#
# The `.TwoSlopeNorm` described above makes a useful example for
# defining your own norm.
# defining your own norm. Note for the colorbar to work, you must
# define an inverse for your norm:
Comment on lines +316 to +317
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I wonder if it would make sense to create an abstract base class for Norms that raises NotImplemented for inverse() so that it doesn't accidentally work with a colorbar? (I think right now it is inheriting the inverse from the standard "Linear" Normalize class. We could catch the NotImplemented in the Colorbar code and warn appropriately there that the inverse method needs to be implemented.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I think that would be sensible.

Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

#7320 seems related to that.



class MidpointNormalize(colors.Normalize):
Expand All @@ -323,8 +325,14 @@ def __init__(self, vmin=None, vmax=None, vcenter=None, clip=False):
def __call__(self, value, clip=None):
# I'm ignoring masked values and all kinds of edge cases to make a
# simple example...
x, y = [self.vmin, self.vcenter, self.vmax], [0, 0.5, 1]
return np.ma.masked_array(np.interp(value, x, y))
# Note also that we must extrapolate beyond vmin/vmax
x, y = [self.vmin, self.vcenter, self.vmax], [0, 0.5, 1.]
return np.ma.masked_array(np.interp(value, x, y,
left=-np.inf, right=np.inf))

def inverse(self, value):
y, x = [self.vmin, self.vcenter, self.vmax], [0, 0.5, 1]
return np.interp(value, x, y, left=-np.inf, right=np.inf)


fig, ax = plt.subplots()
Expand All @@ -334,5 +342,7 @@ def __call__(self, value, clip=None):
cmap=terrain_map, shading='auto')
ax.set_aspect(1 / np.cos(np.deg2rad(49)))
ax.set_title('Custom norm')
fig.colorbar(pcm, shrink=0.6, extend='both')
cb = fig.colorbar(pcm, shrink=0.6, extend='both')
cb.set_ticks([-500, 0, 1000, 2000, 3000, 4000])

plt.show()