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

Contourf Colorbar #5055

Closed
lkilcher opened this issue Sep 13, 2015 · 9 comments
Closed

Contourf Colorbar #5055

lkilcher opened this issue Sep 13, 2015 · 9 comments

Comments

@lkilcher
Copy link
Contributor

The contourf colorbar doesn't work as expected.

The following code:

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as mplc

x, y = np.mgrid[0:1:0.01, 0:1:0.01]
r = np.sqrt(x ** 2 + y ** 2)
z = np.sin(6 * np.pi * r)

norm = mplc.Normalize(0, 0.5)
fig0, ax0 = plt.subplots(1, 1, )
cf0 = ax0.contourf(x, y, z, np.arange(-1., 1.001, .01), norm=norm)
cbar0 = plt.colorbar(cf0,)

Produces the following figure:

conf

Compare this with the colorbar of, e.g. a pcolormesh:

fig2, ax2 = plt.subplots(1, 1)
pc2 = ax2.pcolormesh(x, y, z, norm=norm)
cbar2 = plt.colorbar(pc2)

pcol

That's a nice colorbar, and it's what I expect the default behavior to be for contourf also. Is there a reason the contourf colorbar behaves this way? I've looked at using the vmin,vmax args, and the result is the same. Also, the result is the same for tricontourf colorbars as well.

@lkilcher
Copy link
Contributor Author

I've already worked a little bit on creating a fix for this here. The issue seems to be related to trying to handle creation of colorbar contour lines (not contourf), but I have no idea if this fix causes other issues.

This fix above gives the colorbar I want:

conf

@tacaswell
Copy link
Member

That color bar does look "correct". You asked countourf to make contours from [-1, 1] so the color bar covers that range. Outside of [0, .5] the color maps to the limits of the colormap as you asked it to.

attn @efiring

@tacaswell
Copy link
Member

If you do

import matplotlib.pyplot as plt
import numpy as np
import matplotlib.colors as mplc

x, y = np.mgrid[0:1:0.01, 0:1:0.01]
r = np.sqrt(x ** 2 + y ** 2)
z = np.sin(6 * np.pi * r)

norm = mplc.Normalize(0, 0.5)
fig0, ax0 = plt.subplots(1, 1, )
cf0 = ax0.contourf(x, y, z, np.arange(0, .5, .01), norm=norm,
                   extend='both', cmap='viridis')
cbar0 = plt.colorbar(cf0,)

you get
so

which is what I think your intent was.

@lkilcher
Copy link
Contributor Author

Got it. I was unaware of the extend kwarg. Thanks and sorry. I was starting to think there is a need for a cbar.set_lim method, but I suppose this kind of functionality removes any need for it (i.e. one should always control the color limits via the mappable, rather than the cbar itself). Is that correct?

@tacaswell
Copy link
Member

I could have sworn there was a cbar.set_lim method as I remember using it in the past, but I could not find it (which either means my memory is bad or we have that functionality someplace really strange). I suspect that what you say is true (that you should control the color bar limits through the mappable object), but defer that question to @efiring .

This is something where there are multiple places where it might make sense to be able to control it, we may not always be consistent on where it should be controlled, there is a high degree of 'do the right thing' required, and we can't break back compatibility.

I am only aware of the extend kwarg because there was another bug report related to it last week 😉 . I have a patch on the way that will make the extend on the color bar a bit more controllable.

Where would you have looked in the docs to try and find out this sort of stuff?

@lkilcher
Copy link
Contributor Author

Looking more closely I see that it's in the contourf_demo.py, but it wasn't highlighted prominently enough for me to find it. Is it worth creating a Q+A on SO about how to do this (I don't see one that uses extend), or is this issue good enough?

@tacaswell
Copy link
Member

I am indifferent to creating on SO post on this.

@WeatherGod
Copy link
Member

WeatherGod commented Dec 26, 2016 via email

@efiring
Copy link
Member

efiring commented Dec 26, 2016

The comment above refers to what has now been opened as #7686.

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

4 participants