Skip to content

boundarynorm fix #7553

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 19 commits into from
Mar 28, 2023
Merged
Show file tree
Hide file tree
Changes from 4 commits
Commits
Show all changes
19 commits
Select commit Hold shift + click to select a range
0a288a2
Update utils.py
veenstrajelmer Feb 23, 2023
3d84b2c
Update test_plot.py
veenstrajelmer Feb 23, 2023
c9e4a41
Merge pull request #1 from veenstrajelmer/patch-1
veenstrajelmer Feb 23, 2023
4effc03
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Feb 23, 2023
c2ce3c1
fix as suggested by jklymak
veenstrajelmer Mar 10, 2023
9bbc690
Merge branch 'main' into patch-2
veenstrajelmer Mar 10, 2023
1c764c2
updated testcase
veenstrajelmer Mar 10, 2023
3a60f1d
enabled type checking of test_discrete_colormap_provided_boundary_nor…
veenstrajelmer Mar 16, 2023
317cab8
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 16, 2023
5b5335d
updated whats-new.rst
veenstrajelmer Mar 16, 2023
35ed8d1
Merge branch 'patch-2' of https://github.com/veenstrajelmer/xarray in…
veenstrajelmer Mar 16, 2023
b6d6cb1
enabled type checking of test_discrete_colormap_provided_boundary_nor…
veenstrajelmer Mar 16, 2023
a1c0391
Merge branch 'main' into patch-2
veenstrajelmer Mar 16, 2023
45eaf42
Merge branch 'patch-2' of https://github.com/veenstrajelmer/xarray in…
veenstrajelmer Mar 16, 2023
e7306a0
[pre-commit.ci] auto fixes from pre-commit.com hooks
pre-commit-ci[bot] Mar 16, 2023
4dfb3ac
Update doc/whats-new.rst
veenstrajelmer Mar 27, 2023
0936a66
Merge branch 'main' into patch-2
veenstrajelmer Mar 27, 2023
b3a7c4a
moved whatsnew for pull 7553 to 2023.04 section
veenstrajelmer Mar 27, 2023
bdaf380
Merge branch 'main' into patch-2
veenstrajelmer Mar 28, 2023
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
4 changes: 3 additions & 1 deletion xarray/plot/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -304,9 +304,11 @@ def _determine_cmap_params(
if extend is None:
extend = _determine_extend(calc_data, vmin, vmax)

if levels is not None or isinstance(norm, mpl.colors.BoundaryNorm):
if levels is not None:
cmap, newnorm = _build_discrete_cmap(cmap, levels, extend, filled)
norm = newnorm if norm is None else norm
if isinstance(norm, mpl.colors.BoundaryNorm):
Copy link
Contributor

Choose a reason for hiding this comment

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

I think there is some incompatibility between "levels" and "BoundaryNorm" - you really shouldn't use both and there should be no need to discretize the colormap if using a BoundaryNorm; conversely, there is no need to use a BoundaryNorm or discretize the colormap if using "levels" in a contour plot.

I'm not fully familiar with the xarray pcolormesh API, but I would raise if the user passes both BoundaryNorm and "levels", as having incompatible or duplicated specifications of the color boundaries, and I wouldn't do anything if a BoundaryNorm is passed in. If you want discrete colormaps with levels, but a non-linear norm, I think _build_discrete_cmap is fine.

Copy link
Contributor Author

@veenstrajelmer veenstrajelmer Feb 27, 2023

Choose a reason for hiding this comment

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

Thanks for your valuable feedback. I see your point and I agree, since the "levels" can be derived from BoundaryNorm.vmin, BoundaryNorm.vmax and BoundaryNorm.N. I also considered your alternative suggestion and issues #4061 and Deltares/xugrid#49 are indeed fixed by supplying levels instead of norm to ds.plot().

However, if it is not beneficial to supply norm=BoundaryNorm to ds.plot(), I would say it should raise an error instead of being incorrectly catched in an if-statement. Would you agree?

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jklymak: could you let me know if you agree. If so, I will make a separate issue and close this PR.

Copy link
Contributor

Choose a reason for hiding this comment

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

Assuming we are talking about pcolormesh, Matplotlib has no levels kwarg, so it's a bit strange for xarray to have grown one. I would not prohibit the Matplotlib way of doing this (BoundaryNorm). If you want to keep the convenience of levels, then I would just error if both levels and BoundaryNorm are passed.

In either case, I'm not clear why folks are using levels at all for a pcolormesh, but that is just me ;-)

Copy link
Contributor Author

@veenstrajelmer veenstrajelmer Mar 6, 2023

Choose a reason for hiding this comment

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

@jklymak, the issue here is that levels is constructed by xarray, it is not passed by the user. The user passes the norm argument only (#4061 by @rjp23). The result is a mismatch in length of Ncolors in the cmap and the amount of levels in the BoundaryNorm object.

Copy link
Contributor

Choose a reason for hiding this comment

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

If there is a BoundaryNorm already, I'm not following why you are wanting to build a discrete cmap - the norm will discretize the underlying cmap for you...

Copy link
Contributor Author

Choose a reason for hiding this comment

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

@jklymak You can ignore the PR I did, this is a workaround for the issue in #4061, but as I have learned from you it is not an improvement for xarray. The issue I am referring to provides the norm argument upon plotting, but it behaves different than the matplotlib. I do not understand the xarray plotting code good enough to provide a proper fix for this issue since it is a bit complex. As far as I understand, this PR can be closed without merging, but the issue is not fixed yet. Maybe you can suggest a proper fix for the code in the issue? You seem to understand how the plotting methods should behave.

Copy link
Contributor

Choose a reason for hiding this comment

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

I think this PR is very close, just you went the opposite way with it. I think it should actually be

if (levels is not None) and (not isinstance(norm, mpl.colors.BoundaryNorm)):

and not create a colormap if BoundaryNorm is being used....

cmap, norm = _build_discrete_cmap(cmap, levels, extend, filled)

# vmin & vmax needs to be None if norm is passed
# TODO: always return a norm with vmin and vmax
Expand Down
5 changes: 5 additions & 0 deletions xarray/tests/test_plot.py
Original file line number Diff line number Diff line change
Expand Up @@ -1194,6 +1194,11 @@ def test_discrete_colormap_provided_boundary_norm(self) -> None:
primitive = self.darray.plot.contourf(norm=norm)
np.testing.assert_allclose(primitive.levels, norm.boundaries)

def test_discrete_colormap_provided_boundary_norm_matching_cmap_levels(self):
norm = mpl.colors.BoundaryNorm([0, 5, 10, 15], 4)
primitive = self.darray.plot.contourf(norm=norm)
assert primitive.colorbar.norm.Ncmap + 1 == primitive.colorbar.norm.N


class Common2dMixin:
"""
Expand Down