gpt4 book ai didi

python - matplotlib:通过迭代相关的灰度为线图着色

转载 作者:行者123 更新时间:2023-11-28 22:52:46 25 4
gpt4 key购买 nike

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

例如,

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

生产 enter image description here

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

感谢大家的慷慨帮助!

最佳答案

参见: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 为白色)。一个简单的例子:

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

enter image description here

关于python - matplotlib:通过迭代相关的灰度为线图着色,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/20118258/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com