Opened 11 years ago

Last modified 11 years ago

#10503 new enhancement

Improve custom colormaps

Reported by: kcrisman Owned by: jason, was
Priority: minor Milestone:
Component: graphics Keywords: matplotlib, documentation
Cc: Merged in:
Authors: Reviewers:
Report Upstream: N/A Work issues:
Branch: Commit:
Dependencies: Stopgaps:

Status badges

Description (last modified by kcrisman)

At this question, there is some good discussion of making custom colormaps. 'DSM' gives the idea of the following, which 'Hypercube' suggests we make a standard method:

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

Also, apparently matrix_plot? isn't as helpful as it should be. In the same question, it wasn't obvious how to make a listed colormap - this is buried in the MatrixPlot documentation.

Change History (1)

comment:1 Changed 11 years ago by kcrisman

  • Description modified (diff)
  • Summary changed from Improve documentation for how to make custom colormaps to Improve custom colormaps
Note: See TracTickets for help on using tickets.