FIX: fix colorbars with no scales by jklymak · Pull Request #20327 · matplotlib/matplotlib · GitHub
Skip to content
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
36 changes: 23 additions & 13 deletions lib/matplotlib/colorbar.py
12 changes: 11 additions & 1 deletion lib/matplotlib/colors.py
Original file line number Diff line number Diff line change
Expand Up @@ -1152,7 +1152,7 @@ def __init__(self, vmin=None, vmax=None, clip=False):
self.vmin = _sanitize_extrema(vmin)
self.vmax = _sanitize_extrema(vmax)
self.clip = clip
self._scale = scale.LinearScale(axis=None)
self._scale = None # will default to LinearScale for colorbar

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Note downstream libraries will likely derive from Normalize, so scale should be None so their norm gets used...

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

@dstansby can you see if this version works?

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

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

Yep, seems to work well with this version.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Any chance of a formal review?


@staticmethod
def process_value(value):
Expand Down Expand Up @@ -1334,6 +1334,16 @@ def __call__(self, value, clip=None):
result = np.atleast_1d(result)[0]
return result

def inverse(self, value):
if not self.scaled():
raise ValueError("Not invertible until both vmin and vmax are set")
(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])
return result


class CenteredNorm(Normalize):
def __init__(self, vcenter=0, halfrange=None, clip=False):
Expand Down
Loading
Sorry, something went wrong. Reload?
Sorry, we cannot display this file.
Sorry, this file is invalid so it cannot be displayed.
14 changes: 14 additions & 0 deletions lib/matplotlib/tests/test_colorbar.py
Original file line number Diff line number Diff line change
Expand Up @@ -771,3 +771,17 @@ def test_inset_colorbar_layout():
np.testing.assert_allclose(cb.ax.get_position().bounds,
[0.87, 0.342, 0.0237, 0.315], atol=0.01)
assert cb.ax.outer_ax in ax.child_axes


@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

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

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

Adding the ticklabels to the image would be helpful here.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

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

Yeah, I agree, but we really try to avoid labels, because they cause image comparison problems when the font library changes...

# of the colorbar (white)
fig, ax = plt.subplots()

norm = mcolors.TwoSlopeNorm(20, 0, 100)
pc = ax.pcolormesh(np.arange(1, 11), np.arange(1, 11),
np.arange(100).reshape(10, 10),
norm=norm, cmap='RdBu_r')
fig.colorbar(pc)
3 changes: 1 addition & 2 deletions lib/matplotlib/tests/test_colors.py