Matplotlib/MulticoloredLine - SciPy wiki dump (original) (raw)
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.
Here is the script:
1
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:]))
13
14
15
16 cmap = ListedColormap(['r', 'g', 'b'])
17 norm = BoundaryNorm([-1, -0.5, 0.5, 1], cmap.N)
18
19
20
21
22
23 points = np.array([x, y]).T.reshape(-1, 1, 2)
24 segments = np.concatenate([points[:-1], points[1:]], axis=1)
25
26
27
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.
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 10 11 12 13 points = np.array([x, y]).T.reshape(-1, 1, 2) 14 segments = np.concatenate([points[:-1], points[1:]], axis=1) 15 16 17 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()
SciPy: Cookbook/Matplotlib/MulticoloredLine (last edited 2015-10-24 17:48:23 by anonymous)