This is an archival dump of old wiki content --- see scipy.org for current material.
Please see http://scipy-cookbook.readthedocs.org/

Defining colors manually

colored_line.py is a simple illustration of how to make the color of each segment of a line depend on some property of the data being plotted.

An up to date version of the script can be found here.

colored_line.png

Here is the script:

   1 #!/usr/bin/env python
   2 '''
   3 Color parts of a line based on its properties, e.g., slope.
   4 '''
   5 import numpy as np
   6 import matplotlib.pyplot as plt
   7 from matplotlib.collections import LineCollection
   8 from matplotlib.colors import ListedColormap, BoundaryNorm
   9 
  10 x = np.linspace(0, 3 * np.pi, 500)
  11 y = np.sin(x)
  12 z = np.cos(0.5 * (x[:-1] + x[1:]))  # first derivative
  13 
  14 # Create a colormap for red, green and blue and a norm to color
  15 # f' < -0.5 red, f' > 0.5 blue, and the rest green
  16 cmap = ListedColormap(['r', 'g', 'b'])
  17 norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)
  18 
  19 # Create a set of line segments so that we can color them individually
  20 # This creates the points as a N x 1 x 2 array so that we can stack points
  21 # together easily to get the segments. The segments array for line collection
  22 # needs to be numlines x points per line x 2 (x and y)
  23 points = np.array([x, y]).T.reshape(-1, 1, 2)
  24 segments = np.concatenate([points[:-1], points[1:]], axis=1)
  25 
  26 # Create the line collection object, setting the colormapping parameters.
  27 # Have to set the actual values used for colormapping separately.
  28 lc = LineCollection(segments, cmap=cmap, norm=norm)
  29 lc.set_array(z)
  30 lc.set_linewidth(3)
  31 plt.gca().add_collection(lc)
  32 
  33 plt.xlim(x.min(), x.max())
  34 plt.ylim(-1.1, 1.1)
  35 plt.show()

Note that the number of segments is one less than the number of points.

An alternative strategy would be to generate one segment for each contiguous region of a given color.

Using a smooth, builtin colormap

If you have a parametric curve to display, and want to represent the parameter using color.

colored_line2.png

   1 import numpy as np
   2 import matplotlib.pyplot as plt
   3 from matplotlib.collections import LineCollection
   4 
   5 t = np.linspace(0, 10, 200)
   6 x = np.cos(np.pi * t)
   7 y = np.sin(t)
   8 
   9 # Create a set of line segments so that we can color them individually
  10 # This creates the points as a N x 1 x 2 array so that we can stack points
  11 # together easily to get the segments. The segments array for line collection
  12 # needs to be numlines x points per line x 2 (x and y)
  13 points = np.array([x, y]).T.reshape(-1, 1, 2)
  14 segments = np.concatenate([points[:-1], points[1:]], axis=1)
  15 
  16 # Create the line collection object, setting the colormapping parameters.
  17 # Have to set the actual values used for colormapping separately.
  18 lc = LineCollection(segments, cmap=plt.get_cmap('copper'),
  19     norm=plt.Normalize(0, 10))
  20 lc.set_array(t)
  21 lc.set_linewidth(3)
  22 
  23 plt.gca().add_collection(lc)
  24 plt.xlim(-1, 1)
  25 plt.ylim(-1, 1)
  26 plt.show()


CategoryCookbookMatplotlib

SciPy: Cookbook/Matplotlib/MulticoloredLine (last edited 2015-10-24 17:48:23 by anonymous)