问题描述
所以基本上我想知道,在我的代码的底部,当我绘制我的试验的图形,有没有办法运行一个颜色生成器通过那里?或者更明确地说,我可以列出温暖的颜色,并把它放入我的绘图函数,其中它遍历列表中的每个颜色,当循环通过,因此我的情节只由温暖的颜色?
来自numpy import *
来自pylab import show,plot
来自scipy.special import erfinv
n = 366#天数
ntrials = 5000
u = random.rand(ntrials)
v = sqrt(2。)* erfinv(2. * u-1。)
mu = 0
sigma = .05
investment = 1000.
data = empty((ntrials,n))
data [:,0] = investment
for t在范围(n-1)中:
u = random.rand(ntrials)
v = sqrt(2。)* erfinv(2. * u-1。)
epsilon = v
data [:,t + 1] =(1. + mu + sigma * epsilon)* data [:,t]
data2 = data.sum(axis = 0)
= data2 [-1] / ntrials
data3 = data2 [-1]
x = linspace(0,n,n)
for t in range(n):
plot解决方案
div> 听起来你只是想要这样的东西?
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
#生成数据...
nx,nsteps = 100,20
x = np.linspace(0,1,nx )
data = np.random.random((nx,nsteps)) - 0.5
data = data.cumsum(axis = 0)
data = data.cumsum(axis = 1)
#plot
cmap = mpl.cm.autumn
for i,y in enumerate(data.T):
plt.plot(x,y,color = cmap(i / float(nsteps)))
plt.show()
键调用具有0和1之间的值的matplotlib颜色图实例将返回一种颜色(其中0是颜色图中最低的颜色,1是最高颜色)。
可用色彩图表列表,。您可以使用 name_r
(例如, mpl.cm.autumn
的反转版本 mpl.cm.autumn_r
)。
So basically what i'm wondering, is at the bottom of my code when i plot the graph of my trials, is there a way to run a color generator through there? Or more explicitly put, could i make a list of warm colors, and put that into my plot function, where it runs through each color in a list as the loop runs through, and therefore my plot would only consist of warm colors?
from numpy import *
from pylab import show,plot
from scipy.special import erfinv
n = 366 #number of days
ntrials = 5000
u = random.rand(ntrials)
v = sqrt(2.)*erfinv(2.*u-1.)
mu = 0
sigma = .05
investment = 1000.
data = empty((ntrials,n))
data[:,0] = investment
for t in range(n-1):
u = random.rand(ntrials)
v = sqrt(2.)*erfinv(2.*u-1.)
epsilon = v
data[:,t+1] = (1. + mu +sigma*epsilon)*data[:,t]
data2 = data.sum(axis=0)
woo = data2[-1]/ntrials
data3 = data2[-1]
x = linspace(0,n,n)
for t in range(n):
plot(x,data[t,:])
show()
解决方案 It sounds like you just want something like this?
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
# Generate data...
nx, nsteps = 100, 20
x = np.linspace(0, 1, nx)
data = np.random.random((nx, nsteps)) - 0.5
data = data.cumsum(axis=0)
data = data.cumsum(axis=1)
# Plot
cmap = mpl.cm.autumn
for i, y in enumerate(data.T):
plt.plot(x, y, color=cmap(i / float(nsteps)))
plt.show()
The key is that calling a matplotlib colormap instance with a value between 0 and 1 will return a color (where 0 is the lowest color in the colormap and 1 is the highest).
For a list of available colormaps, see here. You can access the reversed version of any of these with name_r
(e.g. the reversed version of mpl.cm.autumn
is mpl.cm.autumn_r
).
这篇关于我可以把换色器放在一个循环吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!