1

Following the answers of both topics Matplotlib: Plotting numerous disconnected line segments with different colors and matplotlib: how to change data points color based on some variable, I am trying to plot a set of segments given by a list, for instance:

data = [(-118, -118), (34.07, 34.16),
        (-117.99, -118.15), (34.07, 34.16),
        (-118, -117.98), (34.16, 34.07)]

and I would like to plot each segments with a color based on a second list for instance:

color_param = [9, 2, 21]

with a colormap. So far I am using this line to display the segments:

plt.plot(*data)

I was expecting that something like

plt.plot(*data, c=color_param, cmap='hot')

would work but it doesn't. Can anybody help me solving this problem? I would rather work with matplotlib if possible.

Thank you in advance!

Community
  • 1
  • 1
Antoine
  • 23
  • 1
  • 4

2 Answers2

4

You can use LineCollection, here is an example:

import pylab as pl
import numpy as np
from matplotlib.collections import LineCollection
x = np.linspace(0, 2*np.pi, 100)
y = np.sin(x)
c = np.cos(x)
lines = np.c_[x[:-1], y[:-1], x[1:], y[1:]]
lc = LineCollection(lines.reshape(-1, 2, 2), array=c, linewidths=3)
fig, ax = pl.subplots()
ax.add_collection(lc)
ax.set_xlim(x.min(), x.max())
ax.set_ylim(y.min(), y.max())
fig.show()

Here is the result:

HYRY
  • 94,853
  • 25
  • 187
  • 187
1

You can consider the following:

import numpy as np 
import pylab as pl 

# normalize this
color_param = np.array([9.0, 2.0, 21.0])
color_param = (color_param - color_param.min())/(color_param.max() - color_param.min())

data = [(-118, -118), (34.07, 34.16),
        (-117.99, -118.15), (34.07, 34.16),
        (-118, -117.98), (34.16, 34.07)]

startD = data[::2]
stopD  = data[1::2]



for start, stop, col in zip( startD, stopD,  color_param):
    pl.plot( start, stop, color = pl.cm.jet(col) )

pl.show()

Rememebr that the colormaps pl.cm.hot(0.7) will return a color value when presented a number between 0 and 1. This comes in very handy sometimes, like in your case

Edit:

For a red-to-green colormap:

import pylab as pl 
import matplotlib.colors as col
import numpy as np 

cdict = {'red':   [(0.0,  1.0, 1.0),
                   (1.0,  0.0, 0.0)],
         'green':  [(0.0,  0.0, 0.0),
                   (1.0,  1.0, 1.0)],
         'blue':   [(0.0,  0.0, 0.0),
                    (1.0,  0.0, 0.0)]}

my_cmap = col.LinearSegmentedColormap('my_colormap',cdict,256)


for theta in np.linspace(0, np.pi*2, 30):
    pl.plot([0,np.cos(theta)], [0,np.sin(theta)], color=my_cmap(theta/(2*np.pi)) )

pl.show()
ssm
  • 5,277
  • 1
  • 24
  • 42
  • This works perfectly thanks, is there any way to use a personal color scale instead of pl.cm? For instance, if I want red for 0 and green for 1, like setting up my own colormap. Thank you. – Antoine Nov 04 '14 at 21:34
  • You can use the `LinearSegmentedColormap`. There are a couple of detailed tutorials here http://matplotlib.org/api/colors_api.html#matplotlib.colors.LinearSegmentedColormap and here http://wiki.scipy.org/Cookbook/Matplotlib/Show_colormaps for doing that. – ssm Nov 05 '14 at 00:53
  • I have shown an implementation for your simple scheme. Also, if this works for you, you might consider accepting the answer. :) – ssm Nov 05 '14 at 01:07