Skip to content

DOC: Change colormaps for examples (showcase different types, discourage 'jet') #921

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
Jun 24, 2012

Conversation

endolith
Copy link
Contributor

@endolith endolith commented Jun 5, 2012

Here's a bunch of colormap-related changes to the examples for Issue #875, intended to

  1. discourage the use of the 'jet' colormap
  2. encourage using diverging colormaps for bipolar data, sequential colormaps for unipolar data, cyclic colormaps for cyclic data
  3. showcase a variety of different colormaps (hopefully I chose good-looking ones)

There are more that could be changed (like this), but I imagine even this set of changes needs discussion first.

(Also some typo and spelling fixes, which I probably should have done in a different branch, but I'm still learning how this git thing works)

@mdboom
Copy link
Member

mdboom commented Jun 6, 2012

I think this is a really beneficial change. I'd like to get an opinion from someone a little more invested in colormaps than I before merging.

@efiring
Copy link
Member

efiring commented Jun 7, 2012

Running backend_bases.py to generate all the examples reveals several places where an import of cm is missing, so the example fails.

@endolith
Copy link
Contributor Author

endolith commented Jun 8, 2012

Ok, should I add from matplotlib import cm to each, or change them to plt.cm.rainbow?

@efiring
Copy link
Member

efiring commented Jun 8, 2012

On 06/07/2012 03:56 PM, endolith wrote:

Ok, should I add from matplotlib import cm to each, or change them to plt.cm.rainbow?

If the rest of the example is using the "plt.contourf" etc. style, then
you can do the latter to stay consistent with it. But if the example is
using no more than the most minimal pyplot functionality (plt.figure(),
plt.show()), then the former style is probably more consistent.

Eric

@@ -35,14 +35,14 @@
# pcolor plot.
plt.figure()
plt.gca().set_aspect('equal')
plt.tripcolor(triang, z, shading='flat')
plt.tripcolor(triang, z, shading='flat', cmap = plt.cm.rainbow)
plt.colorbar()
Copy link
Collaborator

Choose a reason for hiding this comment

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

You need to import cm in this example

Copy link
Contributor Author

Choose a reason for hiding this comment

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

I don't understand. matplotlib.pyplot does a from matplotlib import cm, so plt.cm.rainbow should be ok?

Copy link
Collaborator

Choose a reason for hiding this comment

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

It's OK now, but it wasn't when I added my line note. It was fixed in endolith@e1b17ec#diff-7. I do think it better to explicitly import cm rather than use it from pyplot though, because it will educate users where they should go to look at the module docs and code

@jdh2358
Copy link
Collaborator

jdh2358 commented Jun 9, 2012

I'm all for it. We inherited the default jet colormap from matlab. Most psychophysicists discourage it, so I'm happy to see someone who cares enough about it to make some intelligent suggestions. It will give some nice visual diversity to our gallery, encourage better habits in users who are exploring through the examples, and give a lot more exposure to many users to the incredible diversity of colormaps we ship. Thanks also for the docstring and comment cleanups in the examples.

@endolith
Copy link
Contributor Author

Please do double-check that I made good choices, though. I'm no psychophysicist. For instance, in tricontour_vs_griddata.py, I initially chose a diverging colormap with a white middle, but then realized the white middle hid the difference between the two plots in the lower left corner, which is probably supposed to be visible in this example.

@pelson
Copy link
Member

pelson commented Jun 13, 2012

Interested to hear others opinions on this, but maybe we could just accept strings as an argument to cmap? The following example seems simple and intuitive:

plt.contourf(my_data, cmap='rainbow')

@efiring
Copy link
Member

efiring commented Jun 13, 2012

On 06/13/2012 04:31 AM, Phil Elson wrote:

Interested to hear others opinions on this, but maybe we could just accept strings as an argument to cmap? The following example seems simple and intuitive:

plt.contourf(my_data, cmap='rainbow')

This idea has also occurred to me, but I never did anything about it.
Given that we have cmaps registered by name, I don't see any advantage
in requiring users give the kwarg as cmap=cm.get_cmap("rainbow"); I
agree with you that the cmap kwarg handling should be able to accept the
name directly.

Eric

@endolith
Copy link
Contributor Author

Doesn't it already accept the name directly?

A = rand(15,15)
imshow(A, cmap='rainbow')

Works for me

@efiring
Copy link
Member

efiring commented Jun 13, 2012

On 06/13/2012 08:51 AM, endolith wrote:

Doesn't it already accept the name directly?

 A = rand(15,15)
 imshow(A, cmap='rainbow')

Works for me

But not for contour, which not only does not support this, but is using
the bad practice of abusing "assert" for input validation.

Eric

@mdboom
Copy link
Member

mdboom commented Jun 14, 2012

I think it would be great to support strings everywhere, including contour -- and we can fix those asserts while we're at it.

@endolith: Did you realise what a mess you were opening up when you started this? :)

@endolith
Copy link
Contributor Author

So each plotting function would import cm internally? Would they recognize strings for colormaps created by register_cmap?

Well the vmid centered norm and the string cmaps should be separate issues, no?

@efiring
Copy link
Member

efiring commented Jun 15, 2012

On 06/15/2012 11:31 AM, endolith wrote:

So each plotting function would import cm internally? Would they
recognize strings for colormaps created by register_cmap?

Yes.

Well the vmid centered norm and the string cmaps should be separate
issues, no?

Yes, definitely separate from your pull request. Is your request
waiting on anything, or do you think it is ready to go now? It doesn't
have to be perfect.

Eric

@@ -39,14 +39,14 @@
gridsize=30

plt.subplot(211)
plt.hexbin(x,y, C=z, gridsize=gridsize, marginals=True)
plt.hexbin(x,y, C=z, gridsize=gridsize, marginals=True, cmap=plt.cm.RdBu, vmax=abs(z).max(), vmin=-abs(z).max())
Copy link
Member

Choose a reason for hiding this comment

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

Ideally the line should be wrapped to 80 characters.

@pelson
Copy link
Member

pelson commented Jun 16, 2012

I think this is a really beneficial change and I appreciate you taking the time to do it. I think my preference, seen as we are changing so many of the examples, would be to use strings to specify the colormap as this would seem to be a preferable solution to using the matplotlib.cm module attributes (currently, only mpl supplied cmaps exist at that level, even if user cmaps have been registered. Hence the user would need to know about two different ways of getting named cmaps).

I have opened a PR (#951) to get this functionality, which, if @efiring agrees, I think should be a dependency on this PR.

@efiring: thoughts?

cset = ax.contour(X, Y, Z, zdir='z', offset=-100)
cset = ax.contour(X, Y, Z, zdir='x', offset=-40)
cset = ax.contour(X, Y, Z, zdir='y', offset=40)
cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap = cm.coolwarm)
Copy link
Member

Choose a reason for hiding this comment

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

Stylistically, throughout this PR it would be preferable to not put spaces between function/method keyword argument definitions. e.g.

cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap = cm.coolwarm)

becomes

cset = ax.contour(X, Y, Z, zdir='z', offset=-100, cmap=cm.coolwarm)

efiring added a commit that referenced this pull request Jun 24, 2012
Change colormaps for examples
@efiring efiring merged commit 5970bde into matplotlib:master Jun 24, 2012
@efiring
Copy link
Member

efiring commented Jun 24, 2012

I think this set of changes has gone through enough review that it makes sense to commit it (as I did just now), and leave any changes related to #951 to another pull request. There is no need to make all examples use the string form of colormap specification, though certainly this should be illustrated.

@endolith
Copy link
Contributor Author

Ok, should anything be done for other examples like http://docs.scipy.org/doc/scipy/reference/tutorial/interpolate.html#id1 ?

@efiring
Copy link
Member

efiring commented Jun 25, 2012

I don't understand. What would you do with that example? It looks OK to me, given that it is illustrating scipy functionality. Are you referring to the use of cm.jet in the example at the bottom of that page?

@endolith
Copy link
Contributor Author

Yes, there are other examples outside of the gallery that are using cm.jet, probably just because of its ubiquity. Should I go around changing those, too?

@efiring
Copy link
Member

efiring commented Jun 25, 2012

You are welcome to propose such changes in the matplotlib docs. Scipy might also be receptive, but that involves different mailing lists, repos, etc.

@pelson pelson mentioned this pull request Jul 2, 2012
@endolith endolith deleted the patch-6 branch February 25, 2013 15:19
@endolith endolith changed the title Change colormaps for examples DOC: Change colormaps for examples (discourage 'jet') May 28, 2022
@endolith endolith changed the title DOC: Change colormaps for examples (discourage 'jet') DOC: Change colormaps for examples (showcase different types, discourage 'jet') May 28, 2022
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

Successfully merging this pull request may close these issues.

5 participants