Skip to content

Inconsistent plot with and without colorbar #15566

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

Open
martinfleis opened this issue Oct 29, 2019 · 15 comments · May be fixed by #26307
Open

Inconsistent plot with and without colorbar #15566

martinfleis opened this issue Oct 29, 2019 · 15 comments · May be fixed by #26307

Comments

@martinfleis
Copy link

Bug report

Bug summary

Plotting without colorbar gives different colors than plotting with one if all plotted values are equal. While this might be expected behaviour, I find it a bit strange. Plotting colorbar alongside the plot should only show the legend to what is plotted, not change the plot itself, I would say. We ran into this recently with geopandas geopandas/geopandas#1163 . Is this the intended behaviour of matplotlib or is it something which should be changed?

Code for reproduction

Without:
sc = plt.scatter(range(3), range(3), c=[1, 1, 1], cmap='viridis')

With:

sc = plt.scatter(range(3), range(3), c=[1, 1, 1], cmap='viridis')
plt.colorbar(sc)

Actual outcome

67638048-9a7cb800-f8d8-11e9-9584-c15ba7e78c20
67638210-f3008500-f8d9-11e9-9164-da640b7180fc

Expected outcome

I would expect colors to be the same in both plots above.

Matplotlib version

  • Operating system: macOS
  • Matplotlib version: 3.1.1
  • Matplotlib backend (print(matplotlib.get_backend())): module://ipykernel.pylab.backend_inline
  • Python version: 3.7.3
  • Jupyter version (if applicable): 1.0.0
  • Other libraries:
@anntzer
Copy link
Contributor

anntzer commented Oct 29, 2019

looks like a proper bug to me.
goes back at least as far as 2.0.0.

@jklymak
Copy link
Member

jklymak commented Oct 29, 2019

Guess this is an issue w scatter. What happens with imagesc or pcolormesh?

@martinfleis
Copy link
Author

@jklymak It is not. pcolormesh does exactly the same thing.

sc = plt.pcolormesh([[2, 2], [2, 2]], cmap='viridis')
plt.colorbar(sc)

Unknown-1

sc = plt.pcolormesh([[2, 2], [2, 2]], cmap='viridis')

Unknown

@jorisvandenbossche
Copy link

Although it is inconsistent/surprising behaviour, it's a consequence of how the colorbar deals with constant values:

self.norm.vmin, self.norm.vmax = mtransforms.nonsingular(
self.norm.vmin,
self.norm.vmax,
expander=0.1)

So here +/- 10% is added to the range, to have an actual colorbar to show. So given that this is also useful, not sure there is a clear fix / better option. You could only add 10% (and not subtract), so the actual value is not in the middle of the colorbar range but at the beginning. That would ensure the color does not change, but on the other hand also gives an odd look I think (although it's a corner case anyway ..)

@jklymak
Copy link
Member

jklymak commented Oct 29, 2019

Ok so the question is whether we should do that non singular expansion regardless of the presence of a colorbar.

@jorisvandenbossche
Copy link

Ah, yes, that's also a way to get it consistent.

@jklymak jklymak added the status: needs comment/discussion needs consensus on next step label Oct 29, 2019
@jklymak
Copy link
Member

jklymak commented Oct 29, 2019

Anything we do will not be backwards compatible so this needs discussion.

  1. leave as is
  2. apply non-singular expansion before a norm is used and either:
    a. keep centered as it is now
    b. offset so its just x and x*1.1.

@anntzer
Copy link
Contributor

anntzer commented Oct 29, 2019

As always, it would be nice to reuse the nonsingular expansion of scales here.

@jklymak
Copy link
Member

jklymak commented Oct 29, 2019

You mean a norm/scale refactor?

@anntzer
Copy link
Contributor

anntzer commented Oct 29, 2019

Yes.

@github-actions
Copy link

This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

@github-actions github-actions bot added the status: inactive Marked by the “Stale” Github Action label Jun 28, 2023
@tacaswell
Copy link
Member

We let #5467 which is the same issue close due to inactivity recently.

Repeating the discussion a bit:

  • if you want a colorbar, the norm must be non-singular (or the transform to make the colorbar don't work as it must be finite size)
  • in every other case there is no reason that a norm has to be non-singular and we map all singular values to 0 (acd19a1 picked up a comment that maybe degenerate norms should give 0.5, but chasing this all the way back, the behavior of giving 0 goes back to e34a333 which is the first reliable commit we have for most of the library from 2004)
  • the norm of the mappable and the colorbar is shared (so they stay in sync!)

Taken together this means if you create a colorbar with a singular norm then we have the options of:

  1. expand the norm as we do now
  2. raise

I do not think any of the 3 things above are really changeable and given the options of expanding or raising, I think expanding is the better choice.

Even if we were to push the non-singular expansion down to the auto-scaling (like suggested #5467 (comment) ) I don't think it would actually solve this problem. The exact case of the OP would be solved, but users could still manually set the limits to be singular and would have even more reason to report a bug ("I asked for a singular norm and the colorbar 'fixed' it!").

I am also going to close this as "can't-fix" as we are in a jam. If in early 2000's the choice had been made to map singular norms to 0.5 we would not have this problem, but I do not think the up-side of fixing this issue is worth the cost of changing the singular behavior.

@tacaswell tacaswell closed this as not planned Won't fix, can't repro, duplicate, stale Jun 28, 2023
@tacaswell tacaswell added status: won't or can't fix and removed status: confirmed bug status: needs comment/discussion needs consensus on next step status: inactive Marked by the “Stale” Github Action labels Jun 28, 2023
@anntzer
Copy link
Contributor

anntzer commented Jun 28, 2023

I did not think much about this recently but is it clear that mapping singular norms to 0.5 (or perhaps even more generically, using the already existing scale expansion behavior for scale-derived norms) is really too disruptive a change? (Seems like something for which we can have a normal deprecation period.)

@greglucas
Copy link
Contributor

I did not think much about this recently but is it clear that mapping singular norms to 0.5 (or perhaps even more generically, using the already existing scale expansion behavior for scale-derived norms) is really too disruptive a change? (Seems like something for which we can have a normal deprecation period.)

I have also wondered the same thing, so seems like it might be worth considering. More to the point, I'm actually wondering if we could go even one step further and deprecate singular norms altogether. We could even add a PendingDeprecation to emit a warning if vmin == vmax during calls and see if we get many complaints. I'm not sure how many users want vmin==vmax, versus it just being due to singular data they passed in autoscaling that way. If it is simply due to the data they passed in it seems like we could auto-expand the limits within the norm and the user wouldn't care either way as long as the plot is a single color (now the midpoint rather than the low end of colors).

@timhoffm
Copy link
Member

The associated PR #26307 has been re-milestoned. Applying the same here.

@timhoffm timhoffm modified the milestones: v3.10.0, future releases Oct 28, 2024
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

8 participants