Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to map number to color using matplotlib's colormap?

Consider a variable x containing a floating point number. I want to use matplotlib's colormaps to map this number to a color, but not plot anything. Basically, I want to be able to choose the colormap with mpl.cm.autumn for example, use mpl.colors.Normalize(vmin = -20, vmax = 10) to set the range, and then map x to the corresponding color. But I really don't get the documentation of mpl.cm, so if anyone could give me a hint.

like image 889
Psirus Avatar asked Feb 28 '13 16:02

Psirus


People also ask

What is CMAP =' viridis?

( cmaps.viridis is a matplotlib.colors.ListedColormap ) import matplotlib.pyplot as plt import matplotlib.image as mpimg import numpy as np import colormaps as cmaps img=mpimg.imread('stinkbug.png') lum_img = np.flipud(img[:,:,0]) imgplot = plt.pcolormesh(lum_img, cmap=cmaps.viridis)

How do you plot a gradient color in python?

MatPlotLib with PythonCreate x, y and c data points, using numpy. Create scatter points over the axes (closely so as to get a line), using the scatter() method with c and marker='_'. To display the figure, use the show() method.


1 Answers

It's as simple as cm.hot(0.3):

import matplotlib.cm as cm      print(cm.hot(0.3)) 
(0.8240081481370484, 0.0, 0.0, 1.0) 

If you also want to have the normalizer, use

import matplotlib as mpl import matplotlib.cm as cm     norm = mpl.colors.Normalize(vmin=-20, vmax=10) cmap = cm.hot x = 0.3  m = cm.ScalarMappable(norm=norm, cmap=cmap) print(m.to_rgba(x)) 
(1.0, 0.8225486412996345, 0.0, 1.0) 
like image 128
David Zwicker Avatar answered Sep 24 '22 01:09

David Zwicker