matplotlib: coloring line plots by iteration-dependent gray scale

安稳与你 提交于 2019-12-08 02:47:39

问题


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!


回答1:


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()



来源:https://stackoverflow.com/questions/20118258/matplotlib-coloring-line-plots-by-iteration-dependent-gray-scale

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!