Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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.

like image 528
Anders Madsen Avatar asked Apr 04 '15 06:04

Anders Madsen


People also ask

What is the default colormap in Python?

The new default colormap used by matplotlib. cm. ScalarMappable instances is 'viridis' (aka option D).

How do you create a custom color in python?

if you have RGB colors, go to http://www.rgbtohex.net/ and convert the rgb colors to hex numbers. In seaborn, we have a function called color_pallet and we can pass our list of colors to it and your colors will be applied to your graphs. Now, just simply plot your graph with your own custom colors. Happy plotting!


1 Answers

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

like image 127
Daniel Høegh Avatar answered Oct 13 '22 22:10

Daniel Høegh