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

FIX: colorbar with boundary norm, proportional, extend #20987

Merged
merged 1 commit into from Sep 5, 2021

Conversation

jklymak
Copy link
Member

@jklymak jklymak commented Sep 3, 2021

PR Summary

Colorbars with a Boundary norm and extend != 'none' were not working because the normalization included the extend boundaries, which are very very big. Normalizing with just the inside boundaries is the right thing to do.

Added an image test because I don't really know how to test this without, and this functionality is not well-tested elsewhere.

Closes #20963

PR Checklist

  • Has pytest style unit tests (and pytest passes).
  • Is Flake 8 compliant (run flake8 on changed files to check).
  • New features are documented, with examples if plot related.
  • Documentation is sphinx and numpydoc compliant (the docs should build without error).
  • Conforms to Matplotlib style conventions (install flake8-docstrings and run flake8 --docstring-convention=all).
  • New features have an entry in doc/users/next_whats_new/ (follow instructions in README.rst there).
  • API changes documented in doc/api/next_api_changes/ (follow instructions in README.rst there).

Copy link
Contributor

@greglucas greglucas left a comment

Choose a reason for hiding this comment

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

Looks good to me, and I think the image test makes sense.

Copy link
Member

@timhoffm timhoffm left a comment

Choose a reason for hiding this comment

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

Optional comments.

Comment on lines 844 to 846
cmap = mcolors.ListedColormap(['r', 'g', 'white', 'g', 'r'])
cmap.set_under('yellow')
cmap.set_over('cyan')
Copy link
Member

Choose a reason for hiding this comment

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

Optional, but the test is stonger if you break the symmetry of the colors. With the symmetry, you cannot check the sign.

Suggested change
cmap = mcolors.ListedColormap(['r', 'g', 'white', 'g', 'r'])
cmap.set_under('yellow')
cmap.set_over('cyan')
cmap = mcolors.ListedColormap(
['0.3', '0.5', 'white', 'lightblue', 'steelblue'])
cmap.set_under('darkred')
cmap.set_over('crimson')

grafik

Also, I've chosen colors that hurt the eye a little less 😄

Copy link
Member Author

Choose a reason for hiding this comment

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

sure

style='mpl20')
def test_proportional_colorbars():

x = y = np.arange(-3.0, 3.01, 0.025)
Copy link
Member

Choose a reason for hiding this comment

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

Suggested change
x = y = np.arange(-3.0, 3.01, 0.025)
x = y = np.linspace(-3, 3, 241)

np.arange with non-integer step is generally not recommended

https://numpy.org/doc/stable/reference/generated/numpy.arange.html:

When using a non-integer step, such as 0.1, the results will often not be consistent. It is better to use numpy.linspace for these cases.

While it doesn't matter for the test, let's set a good example.

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 guess I don't understand this advice. Very often one wants to specify dx instead of N. Sure you can compute one from the other, but I don't see why one would be more consistent than the other.

Copy link
Member

@timhoffm timhoffm Sep 4, 2021

Choose a reason for hiding this comment

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

Since you have limited numeric precision, the results are only approximately the same. For arange you often don't get expected "exact" numbers. e.g. the middle value is 0 with linspace but 1.06e-14 with arange. Additionally, the "endpoint not included" feature makes sense for indices, but is a somewhat awkard API for (float)numeric sequences. And in conjunction with the numeric jitter, this can have surprising effects; e.g. https://stackoverflow.com/questions/10011302/python-numpy-arange-unexpected-results.

Copy link
Member Author

Choose a reason for hiding this comment

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

Sure, that's just floating point arithmetic. I'm not convinced that makes arange bad style or "inconsistent"

Copy link
Member

@timhoffm timhoffm Sep 5, 2021

Choose a reason for hiding this comment

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

Well, "inconsistent" is numpy's description. That may not be the best term.

However, that the endpoint and number of points depend on small numeric effects is highly problematic. Practically, you don't know what you will get if endpoint is start + N times interval.

Anyway, I'm not going to argue more about this within a test.

@timhoffm timhoffm merged commit 8ce14fb into matplotlib:master Sep 5, 2021
meeseeksmachine pushed a commit to meeseeksmachine/matplotlib that referenced this pull request Sep 5, 2021
@lumberbot-app
Copy link

lumberbot-app bot commented Sep 5, 2021

Owee, I'm MrMeeseeks, Look at me.

There seem to be a conflict, please backport manually. Here are approximate instructions:

  1. Checkout backport branch and update it.
$ git checkout v3.4.x
$ git pull
  1. Cherry pick the first parent branch of the this PR on top of the older branch:
$ git cherry-pick -m1 8ce14fb60d9441ce3fb92e6e59b4aa3146aca6c1
  1. You will likely have some merge/cherry-pick conflict here, fix them and commit:
$ git commit -am 'Backport PR #20987: FIX: colorbar with boundary norm, proportional, extend'
  1. Push to a named branch :
git push YOURFORK v3.4.x:auto-backport-of-pr-20987-on-v3.4.x
  1. Create a PR against branch v3.4.x, I would have named this PR:

"Backport PR #20987 on branch v3.4.x (FIX: colorbar with boundary norm, proportional, extend)"

And apply the correct labels and milestones.

Congratulation you did some good work! Hopefully your backport PR will be tested by the continuous integration and merged soon!

Remember to remove Still Needs Manual Backport label once the PR gets merged.

If these instruction are inaccurate, feel free to suggest an improvement.

dstansby added a commit that referenced this pull request Sep 5, 2021
…987-on-v3.5.x

Backport PR #20987 on branch v3.5.x (FIX: colorbar with boundary norm, proportional, extend)
@QuLogic QuLogic modified the milestones: v3.4.4, v3.5.0 Sep 7, 2021
@QuLogic
Copy link
Member

QuLogic commented Sep 7, 2021

It never worked, so I don't think we should go through the trouble of figuring out the backport.

@jklymak
Copy link
Member Author

jklymak commented Sep 8, 2021

Agreed. The back port would be really hard given the changes to colorbar in 3.5

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.

[Bug]: broken 'proportional' colorbar when using contourf+cmap+norm+extend
4 participants