matplotlib:通过依赖于迭代的灰度为线图着色 [英] matplotlib: coloring line plots by iteration-dependent gray scale

查看:125
本文介绍了matplotlib:通过依赖于迭代的灰度为线图着色的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

相对编程新手。我很难弄清楚如何在一系列迭代中绘制插值函数,其中随着迭代索引的增加,该图将从黑色变为逐渐变浅的灰色阴影。

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.

例如,

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

产生

我希望蓝色正弦函数为黑色,其余的随着t的增加而变得更浅和更灰(向右)。我该怎么办?

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!

推荐答案

请参阅: http://matplotlib.org/api/axes_api.html#matplotlib.axes.Axes.plot

例如您可以将 plt.plot(x,yint(x),color =(0.5,0.5,0.5))设置为灰线。您可以随意设置值(0.0是黑色,1.0是白色)。一个简单的例子:

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

这篇关于matplotlib:通过依赖于迭代的灰度为线图着色的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆