Reverse Brewer colour map...

354 views
Skip to first unread message

Nick Dunstone

unread,
Mar 23, 2015, 11:37:31 AM3/23/15
to scitoo...@googlegroups.com
Hi,

I've been going around in circles on this and banging my head against too many brick walls!

What I want to do is very simple, I'd like to reverse the colours in one of the standard Iris Brewer color maps. In particular I'd like to reverse this:
brewer_cmap = mpl_cm.get_cmap('brewer_RdBu_11')

so that blue and red are swapped.

There must be a simple way of doing this but I've gone down too many wrong turns already!

Thanks in advance,

Nick

Nick Dunstone

unread,
Mar 23, 2015, 1:22:03 PM3/23/15
to scitoo...@googlegroups.com
Think I've finally managed to answer my own question. This seems to work to reverse this colour bar:


>>> brewer_cmap = mpl_cm.get_cmap('brewer_RdBu_11')
>>> my_cmap = brewer_cmap([10,9,8,7,6,5,4,3,2,1,0])
>>> new_cmap = colors.ListedColormap(my_cmap,"my_colormap")

Seems to do the trick! Hope it helps someone else in future and saves them a few hours...

Nick

Benjamin Root

unread,
Mar 23, 2015, 11:31:00 PM3/23/15
to scitoo...@googlegroups.com
Or just put an '_r'at the end of any built-in matplotlib colormap name. Reverses it automatically for you.

Andrew Dawson

unread,
Mar 24, 2015, 5:15:15 AM3/24/15
to scitoo...@googlegroups.com
Thanks Ben, but the problem here is that this is not a matplotlib builtin colormap and the '_r' version is not defined (you'll just see a ValueError if you try and get 'brewer_RdBu_11_r').

Benjamin Root

unread,
Mar 24, 2015, 9:30:23 AM3/24/15
to Andrew Dawson, scitoo...@googlegroups.com
Ah, indeed. I saw the "RdBu" in the name, which is a built-in colormap. That is a bit silly that matplotlib doesn't provide an easy way to reverse the colormap. There are some internal functions that works for the built-ins, but nothing general. I think I'll open up an issue for this on the tracker.

--
You received this message because you are subscribed to a topic in the Google Groups "Iris" group.
To unsubscribe from this topic, visit https://groups.google.com/d/topic/scitools-iris/biD6F6OG9Og/unsubscribe.
To unsubscribe from this group and all its topics, send an email to scitools-iri...@googlegroups.com.
For more options, visit https://groups.google.com/d/optout.

Andreas Hilboll

unread,
Mar 24, 2015, 9:39:01 AM3/24/15
to scitoo...@googlegroups.com
Hi,

I have this lying around:

def reverse_linearsegmentedcolormap(cmap):
"""Reverse a LinearSegmentedColormap

Parameters
----------
cmap : matplotlib.colors.Colormap
The colormap to be reversed.

Returns
-------
cmap_reversed : matplotlib.colors.Colormap
The colormap to be reversed.

"""
cdict = {}
for k, v in cmap._segmentdata.iteritems():
newlist = []
for c in v:
newlist.append((1.0 - c[0], c[1], c[2]))
v = newlist[::-1]
cdict[k] = v
cmap_reversed = deepcopy(cmap)
cmap_reversed._segmentdata = cdict
return cmap_reversed


Not sure at the moment why I'm writing LinearSegmentedColormap here;
this might be unnecessary.

Maybe it's helpful to someone ...

Cheers,
Andreas.


On 24.03.2015 14:30, Benjamin Root wrote:> Ah, indeed. I saw the "RdBu"
> <mailto:scitools-iri...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.
>
>
> --
> You received this message because you are subscribed to the Google
> Groups "Iris" group.
> To unsubscribe from this group and stop receiving emails from it, send
> an email to scitools-iri...@googlegroups.com
> <mailto:scitools-iri...@googlegroups.com>.
> For more options, visit https://groups.google.com/d/optout.


--
-- Andreas.

Benjamin Root

unread,
Mar 24, 2015, 10:00:17 AM3/24/15
to Andreas Hilboll, scitoo...@googlegroups.com
It is because _segmentdata is only for certain kinds of colormaps, IIRC. It isn't fully general. Useful, but not completely there. Probably the best trick might be to reverse the underlying Norm on-the-fly.

To unsubscribe from this group and all its topics, send an email to scitools-iri...@googlegroups.com.

Andrew Dawson

unread,
Mar 24, 2015, 10:03:43 AM3/24/15
to scitoo...@googlegroups.com, ajd...@gmail.com, ben....@ou.edu
 I think I'll open up an issue for this on the tracker.

Cheers Ben, it'd be great if matplotlib could support this. 

Benjamin Root

unread,
Mar 24, 2015, 11:05:28 AM3/24/15
to scitoo...@googlegroups.com, ajd...@gmail.com
I created the following ticket on github: https://github.com/matplotlib/matplotlib/issues/4271
Reply all
Reply to author
Forward
0 new messages