Ask Your Question
2

Specific cmap

asked 2010-12-20 00:18:36 +0100

Eviatar Bach gravatar image

updated 2015-01-14 14:50:21 +0100

FrédéricC gravatar image

Hello,

How would I create a specific cmap, which would have a separate color value for each matrix value? For example, could I make a cmap that specifies 0:'red', 1:'green', 2:'yellow', etc? The linear interpolation cmaps don't work as I want them to because they normalize to the given range of the matrix.

Thank you.

edit retag flag offensive close merge delete

3 Answers

Sort by » oldest newest most voted
2

answered 2010-12-20 09:32:26 +0100

DSM gravatar image

updated 2010-12-21 01:06:36 +0100

I seem to recall once having a slick workaround for this but for the life of me I can't find it now. Anyway, something like this should get the job done:

# preamble
import matplotlib

# choose our preferred colour scheme, where None describes the exceptions
cdict = {0: 'red', 1: 'green', 2:'yellow', 3:'orange', 43+2*I: 'brown', None: 'purple'}

# choose some fixed order -- doesn't matter what -- for iterating the keys
ckeys, cvals = zip(*cdict.items())

# build a mapping function to turn the matrix elements into integers
# (would be faster to use a dict for lots of colours)
cmap_fn = lambda x: Integer(ckeys.index(x if x in cdict else None))

# generate a colormap with exactly the colours we want, in the right order
my_cmap = matplotlib.colors.ListedColormap(cvals)

# generate a test matrix
m = Matrix(lambda i,j: (i+j) if (i,j) != (6,6) else 43+2*I, nrows=10)

# plot it: apply the mapping function to the matrix and feed matrix_plot our colour map
matrix_plot(m.apply_map(cmap_fn), cmap=my_cmap)
 

This should generate one very ugly graph: one red cell, two green cells, three yellow cells, four orange cells, and a brown cell at (6,6) in a purple sea. It's easy to bundle this into a function if you find yourself doing it frequently.

[update: modified to make it slightly more general: now you don't need any ordering relation defined on the matrix elements]

edit flag offensive delete link more

Comments

Will that work even with Sage #s, like $4.3+2*I$?

kcrisman gravatar imagekcrisman ( 2010-12-20 13:18:29 +0100 )edit

This is exactly what I wanted! Thank you! This should be built in to Sage.

Eviatar Bach gravatar imageEviatar Bach ( 2010-12-20 20:06:37 +0100 )edit

@kcrisman: yes, you just have to be careful to make sure that the matrix element still has the same type as [well, no, just 'still compares equal to'] the cdict key, else the cmap_fn will fail to find a match and fall back to the None colour.

DSM gravatar imageDSM ( 2010-12-20 21:15:49 +0100 )edit

I've updated the relevant ticket to put this as an enhancement. DSM may want to put his/her real name on there to get some credit if this ever makes it into Sage, which seems like a good idea.

kcrisman gravatar imagekcrisman ( 2010-12-21 09:04:18 +0100 )edit
2

answered 2010-12-20 09:20:27 +0100

kcrisman gravatar image

Well, after writing this, I finally found out that we have this capability, though there are no examples in the matrix_plot doc (though it's mentioned).

sage: matrix_plot(matrix([[0,1],[1,2]]),cmap=['red','green','blue'])

This relies on get_cmap, a very nice utility I had forgotten about! Read this:

sage: sage.plot.matrix_plot.get_cmap?

Still, I'll put some other interesting resources I found below, in case that is too easy, or you need more direction for how to create good colormaps..

Here is an example of how this might be done in pure Python, and here is a nice way to 'discretize' colormaps.

Now, these don't do exactly what you want, because the colormaps seem to mostly operate on the principle of the interval $(0,1)$. If we do

sage: import matplotlib
sage: matplotlib.colors?
A module for converting numbers or color arguments to *RGB* or *RGBA*

*RGB* and *RGBA* are sequences of, respectively, 3 or 4 floats in the
range 0-1.

This module includes functions and classes for color specification
conversions, and for mapping numbers to colors in a 1-D array of
colors called a colormap. Colormapping typically involves two steps: a
data array is first mapped onto the range 0-1 using an instance of
``Normalize`` or of a subclass; then this number in the 0-1 range is
mapped to a color using an instance of a subclass of ``Colormap``.
Two are provided here: ``LinearSegmentedColormap``, which is used to
generate all the built-in colormap instances, but is also useful for
making custom colormaps, and ``ListedColormap``, which is used for
generating a custom colormap from a list of color specifications.

So here is something that might work:

sage: import matplotlib
sage: M = matplotlib.colors.ListedColormap([(0,1,1),(1,0,1),(1,1,0)],'M')
sage: matrix_plot(matrix([[0,1],[1,2]]),cmap=M)
edit flag offensive delete link more

Comments

kcrisman gravatar imagekcrisman ( 2010-12-20 09:23:40 +0100 )edit

And I do not believe matplotlib allows one to associate specific numbers with colors, only the range. The type of cmap this provides is discrete, though, so hopefully it should be good enough if you know what values to expect ahead of time. If that is random, you may have to generate the cmap on the fly. The links provided are just some of some great posts and explanations I found on the matplotlib and Scipy websites, so hopefully you'll find what you need if this isn't it.

kcrisman gravatar imagekcrisman ( 2010-12-20 09:26:14 +0100 )edit

This fits my needs if normalized, but since user input will not necessarily be in order, the other solution works better.

Eviatar Bach gravatar imageEviatar Bach ( 2010-12-20 20:09:04 +0100 )edit
0

answered 2010-12-22 03:21:34 +0100

Jason Grout gravatar image

updated 2010-12-22 03:26:20 +0100

You might check out the norm option to matrix_plot (see the docs), which takes a value argument that does away with scaling before applying a colormap. You can also specify a function that does the colormapping. See also the matplotlib documentation for colormaps and the normalizing functions.

edit flag offensive delete link more

Your Answer

Please start posting anonymously - your entry will be published after you log in or create a new account.

Add Answer

Question Tools

Stats

Asked: 2010-12-20 00:18:36 +0100

Seen: 1,670 times

Last updated: Dec 22 '10