Anders Madsen
Anders Madsen

Reputation: 61

How to make a custom colormap using PyPlot (not matplotlib proper)

Working in IJulia. Desperately trying to make a custom colormap. Tried the line:

matplotlib.colors.ListedColormap([(1,0,0),(0,1,0),(0,0,1)],"A") 

which resulted in the following error

type PyObject has no field colors while loading In[16], in expression starting on line 1

which apparently means that I cannot use matplotlib directly, but only the functions which are in PyPlot.

I cannot involve matplotlib with an import (as this is invalid in IJulia). I have noted that others have had help on similar problems, but that doesn't solve mine.

Upvotes: 6

Views: 2497

Answers (2)

SGJ
SGJ

Reputation: 1058

You don't need to use PyCall to call Python directly (although this is, of course, an option). You can also just use the PyPlot constructors for ColorMap to construct a colormap from (r,g,b) arrays or an array of colors as defined in the Julia Color package. See the PyPlot ColorMap documentation. For example:

using PyPlot, Color
ColorMap("A", [RGB(1,0,0),RGB(0,1,0),RGB(0,0,1)])

Upvotes: 0

Daniel Høegh
Daniel Høegh

Reputation: 716

By using the PyCall package which PyPlot is using to wrap matplotlib you can obtain a colormap like this:

using PyCall
@pyimport matplotlib.colors as matcolors
cmap = matcolors.ListedColormap([(1,0,0),(0,1,0),(0,0,1)],"A")

In order to access fields in a PyObject you need to index the object with a symbol like:

cmap[:set_over]((0,0,0))

This is equivalent to: cmap.set_over((0,0,0)) in python. For other good examples of how to plot different kinds of plots using PyPlot, see these examples: https://gist.github.com/gizmaa/7214002

Upvotes: 4

Related Questions