Skip to content
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

boundarynorm fix #7553

Merged
merged 19 commits into from
Mar 28, 2023
Merged

boundarynorm fix #7553

merged 19 commits into from
Mar 28, 2023

Conversation

veenstrajelmer
Copy link
Contributor

@veenstrajelmer veenstrajelmer commented Feb 23, 2023

  • Closes #4061
  • Closes #49
  • Closes #7014
  • Tests added
  • User visible changes (including notable bug fixes) are documented in whats-new.rst
  • New functions/methods are listed in api.rst >> not applicable

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....

@veenstrajelmer
Copy link
Contributor Author

Thanks @jklymak, that makes sense I guess. I have committed the changes again. Could you check if it can be merged?

Copy link
Collaborator

@headtr1ck headtr1ck left a comment

Choose a reason for hiding this comment

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

You can add an entry in whats-new.

xarray/tests/test_plot.py Outdated Show resolved Hide resolved
xarray/tests/test_plot.py Show resolved Hide resolved
@veenstrajelmer
Copy link
Contributor Author

@headtr1ck the proposed changes were pushed agian. I think most should be fixed now.

Copy link
Collaborator

@headtr1ck headtr1ck left a comment

Choose a reason for hiding this comment

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

LGTM

@headtr1ck headtr1ck added the plan to merge Final call for comments label Mar 16, 2023
doc/whats-new.rst Outdated Show resolved Hide resolved
@dcherian dcherian removed the plan to merge Final call for comments label Mar 25, 2023
Co-authored-by: Deepak Cherian <dcherian@users.noreply.github.com>
doc/whats-new.rst Outdated Show resolved Hide resolved
@headtr1ck headtr1ck enabled auto-merge (squash) March 27, 2023 10:04
@headtr1ck
Copy link
Collaborator

Thanks for this PR!

@headtr1ck headtr1ck merged commit e79eaf5 into pydata:main Mar 28, 2023
dcherian added a commit to kmsquire/xarray that referenced this pull request Mar 29, 2023
* upstream/main:
  Save groupby codes after factorizing, pass to flox (pydata#7206)
  [skip-ci] Add compute to groupby benchmarks (pydata#7690)
  Delete built-in cfgrib backend (pydata#7670)
  Added a pronunciation guide to the word Xarray in the README.MD fil… (pydata#7677)
  boundarynorm fix (pydata#7553)
  Fix lazy negative slice rewriting. (pydata#7586)
  [pre-commit.ci] pre-commit autoupdate (pydata#7687)
  Adjust sidebar font colors (pydata#7674)
  Bump pypa/gh-action-pypi-publish from 1.8.1 to 1.8.3 (pydata#7682)
  Raise PermissionError when insufficient permissions (pydata#7629)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging this pull request may close these issues.

4 participants