This is one of the 100 recipes of the IPython Cookbook, the definitive guide to high-performance scientific computing and data science in Python.
import numpy as np
import matplotlib as mpl
import matplotlib.pyplot as plt
%matplotlib inline
n = 5000
x = np.cumsum(np.random.randn(n))
y = np.cumsum(np.random.randn(n))
plot(x, y)
. However, the result would be monochromatic and a bit boring. We would like to use a gradient of color to illustrate the progression of the motion in time. Matplotlib forces us to use a small hack based on scatter
. This function allows us to assign a different color to each point at the expense of dropping out line segments between points. To work around this issue, we interpolate linearly the process to give the illusion of a continuous line.k = 10 # We add 10 intermediary points between two
# successive points.
# We interpolate x and y.
x2 = np.interp(np.arange(n*k), np.arange(n)*k, x)
y2 = np.interp(np.arange(n*k), np.arange(n)*k, y)
# Now, we draw our points with a gradient of colors.
plt.scatter(x2, y2, c=range(n*k), linewidths=0,
marker='o', s=3, cmap=plt.cm.jet,)
plt.axis('equal');
plt.xticks([]); plt.yticks([]);
You'll find all the explanations, figures, references, and much more in the book (to be released later this summer).
IPython Cookbook, by Cyrille Rossant, Packt Publishing, 2014 (500 pages).