Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Using Colormaps to set color of line in matplotlib

How does one set the color of a line in matplotlib with scalar values provided at run time using a colormap (say jet)? I tried a couple of different approaches here and I think I'm stumped. values[] is a storted array of scalars. curves are a set of 1-d arrays, and labels are an array of text strings. Each of the arrays have the same length.

fig = plt.figure() ax = fig.add_subplot(111) jet = colors.Colormap('jet') cNorm  = colors.Normalize(vmin=0, vmax=values[-1]) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet) lines = [] for idx in range(len(curves)):     line = curves[idx]     colorVal = scalarMap.to_rgba(values[idx])     retLine, = ax.plot(line, color=colorVal)     #retLine.set_color()     lines.append(retLine) ax.legend(lines, labels, loc='upper right') ax.grid() plt.show() 
like image 563
fodon Avatar asked Jan 19 '12 18:01

fodon


People also ask

How do I change the color of a line in Python?

You do not need to use format strings, which are just abbreviations. All of the line properties can be controlled by keyword arguments. For example, you can set the color, marker, linestyle, and markercolor with: plot(x, y, color='green', linestyle='dashed', marker='o', markerfacecolor='blue', markersize=12).

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)


1 Answers

The error you are receiving is due to how you define jet. You are creating the base class Colormap with the name 'jet', but this is very different from getting the default definition of the 'jet' colormap. This base class should never be created directly, and only the subclasses should be instantiated.

What you've found with your example is a buggy behavior in Matplotlib. There should be a clearer error message generated when this code is run.

This is an updated version of your example:

import matplotlib.pyplot as plt import matplotlib.colors as colors import matplotlib.cm as cmx import numpy as np  # define some random data that emulates your indeded code: NCURVES = 10 np.random.seed(101) curves = [np.random.random(20) for i in range(NCURVES)] values = range(NCURVES)  fig = plt.figure() ax = fig.add_subplot(111) # replace the next line  #jet = colors.Colormap('jet') # with jet = cm = plt.get_cmap('jet')  cNorm  = colors.Normalize(vmin=0, vmax=values[-1]) scalarMap = cmx.ScalarMappable(norm=cNorm, cmap=jet) print scalarMap.get_clim()  lines = [] for idx in range(len(curves)):     line = curves[idx]     colorVal = scalarMap.to_rgba(values[idx])     colorText = (         'color: (%4.2f,%4.2f,%4.2f)'%(colorVal[0],colorVal[1],colorVal[2])         )     retLine, = ax.plot(line,                        color=colorVal,                        label=colorText)     lines.append(retLine) #added this to get the legend to work handles,labels = ax.get_legend_handles_labels() ax.legend(handles, labels, loc='upper right') ax.grid() plt.show() 

Resulting in:

enter image description here

Using a ScalarMappable is an improvement over the approach presented in my related answer: creating over 20 unique legend colors using matplotlib

like image 111
Yann Avatar answered Oct 09 '22 01:10

Yann