matplotlib: coloring line plots by iteration-dependent gray scale

colors, interpolation, matplotlib, plot, python

Solution

See: http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.plot

E.g. you can set `plt.plot(x, yint(x), color=(0.5, 0.5, 0.5))` for a gray line. You can set the values up however you like (0.0 is black, 1.0 is white). A simple example:

import numpy as np
import matplotlib.pyplot as plt
from scipy.interpolate import interp1d

for t in np.arange(0.,2., 0.4):
    x = np.linspace(0.,4, 100)
    y = np.sin(x-2*t) + 0.01 * np.random.normal(size=x.shape)
    yint = interp1d(x, y)
    print t
    col = (t/2.0, t/2.0, t/2.0)
    plt.plot(x, yint(x), color=col)

plt.show()

Problem

Relative programming newbie here. I have trouble figuring out how to plot interpolated functions over a series of iterations, where as the iteration index increases, the plot would go from black to gradually lighter shades of grey. For example, ``` import numpy as np import matplotlib.pyplot as plt from scipy.interpolate import interp1d for t in np.arange(0.,2., 0.4): x = np.linspace(0.,4, 100) y = np.sin(x-2*t) + 0.01 * np.random.normal(size=x.shape) yint = interp1d(x, y) plt.plot(x, yint(x)) plt.show() ``` produces I would like the blue sinusoidal function to be black, and the rest becomes lighter and greyer as t increases (to the right). How would I do that? Thank you all for your generous help!

Original source