Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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)