Ask Your Question

Revision history [back]

click to hide/show revision 1
initial version

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', 4.3+2j: 'brown', None: 'purple'}

# build a mapping function to turn the matrix elements into integers
# N.B. the 0 is okay because the None should be first in the sorted array of keys
cmap_fn = lambda x: sorted(cdict).index(x) if x in cdict else 0

# generate a colormap with exactly the colours we want, in the right order
my_cmap = matplotlib.colors.ListedColormap(list(cdict[k] for k in sorted(cdict)))

# generate a test matrix
m = Matrix(lambda i,j: i+j if (i,j) != (6,6) else 4.3+2j, nrows=10, ring=CDF)

# 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.

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', 4.3+2j: 43+2*I: 'brown', None: 'purple'}

# choose some fixed order -- doesn't matter what -- for iterating the keys
cdict_keys = list(cdict)

# build a mapping function to turn the matrix elements into integers
# N.B. the 0 is okay because the None should (would be first in the sorted array faster to use a dict for lots of keys
colours)
cmap_fn = lambda x: sorted(cdict).index(x) Integer(cdict_keys.index(x if x in cdict else 0
None))

# generate a colormap with exactly the colours we want, in the right order
my_cmap = matplotlib.colors.ListedColormap(list(cdict[k] for k in sorted(cdict)))
cdict_keys))

# generate a test matrix
m = Matrix(lambda i,j: i+j (i+j) if (i,j) != (6,6) else 4.3+2j, nrows=10, ring=CDF)
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]

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
cdict_keys = list(cdict)
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(cdict_keys.index(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(list(cdict[k] for k in cdict_keys))
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)
nrows=10)# ring=ComplexField)

# 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]

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)# ring=ComplexField)
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]