动画 Monte Carlo 方法 Pi 颜色点不同
Animating Monte Carlo Method Pi color dots diferently
我想用 mc 方法计算 pi 的结果绘制动画;但内圈点的颜色与其他点不同。我怎样才能做到这一点?
到目前为止,这是我的代码:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
def punkt_im_quadrat(a,N): #a is length of square, N number of dots
"""generates random point in [0,a)x[0,a)"""
x = a * np.random.random_sample((N,2))
return x #[x,y]
def kreis(radius):
return np.sqrt(radius - x**2)
x = np.linspace(0,1,100)
#create array of N=10 dots
punkte = punkt_im_quadrat(1,10) #a=1, so radius of circle is one
treffer = [i for i in punkte if i[1] <= np.sqrt(1 - i[0]**2)] #dots in circle
treffer = np.array(treffer)
außerhalb = [i for i in punkte if i not in treffer] #dots not in circle
außerhalb = np.array(außerhalb)
pi = 4 * len(treffer) / np.shape(punkte)[0]
fig = plt.figure()
fig, ax = plt.subplots()
ax.plot(x,kreis(1))
ax.set_xlim(0,1)
ax.set_ylim(0,1)
ax.set(title=r"MC Sampling for $\pi$",
ylabel="y-axis",
xlabel="x-axis")
#colors = ["r" if [punkte[i][0],punkte[i][1]] in treffer else "b"]
graph, = ax.plot([],[], "ro")
def animate(i):
graph.set_data((punkte[:i,0],), (punkte[:i,1],))
return graph,
animation = FuncAnimation(fig, func=animate, frames = range(np.shape(punkte)[0]), interval=20, repeat = False)
plt.show()
如您所见,我尝试使用注释颜色 if 语句来更改颜色;但它接着说
IndexError: 用作索引的数组必须是整数(或布尔)类型
然后我想我可以在动画函数中做一个 if 语句,以确定点在哪里。但是当使用 graph.set_color
时,它会改变所有点的颜色。
如果有人能帮助我,我会很高兴。
提前致谢!
plot
中的所有标记都具有相同的颜色,因此无法工作。如果你想要不同的点有不同的颜色,你需要使用scatter()
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
def punkt_im_quadrat(a,N): #a is length of square, N number of dots
"""generates random point in [0,a)x[0,a)"""
x = a * np.random.random_sample((N,2))
return x #[x,y]
def kreis(radius):
return np.sqrt(radius - x**2)
x = np.linspace(0,1,100)
#create array of N=10 dots
punkte = punkt_im_quadrat(1,10) #a=1, so radius of circle is one
treffer = [i for i in punkte if i[1] <= np.sqrt(1 - i[0]**2)] #dots in circle
treffer = np.array(treffer)
außerhalb = [i for i in punkte if i not in treffer] #dots not in circle
außerhalb = np.array(außerhalb)
colors = np.array(["r" if i[1] <= np.sqrt(1 - i[0]**2) else "b" for i in punkte])
pi = 4 * len(treffer) / np.shape(punkte)[0]
fig, ax = plt.subplots()
ax.plot(x,kreis(1))
ax.set_xlim(0,1)
ax.set_ylim(0,1)
ax.set(title=r"MC Sampling for $\pi$",
ylabel="y-axis",
xlabel="x-axis")
graph = ax.scatter([],[], marker='o', s=30)
def animate(i):
graph.set_offsets(punkte[:i,:])
graph.set_facecolor(colors[:i])
return graph,
animation = FuncAnimation(fig, func=animate, frames = range(np.shape(punkte)[0]), interval=20, repeat = False)
我想用 mc 方法计算 pi 的结果绘制动画;但内圈点的颜色与其他点不同。我怎样才能做到这一点? 到目前为止,这是我的代码:
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
def punkt_im_quadrat(a,N): #a is length of square, N number of dots
"""generates random point in [0,a)x[0,a)"""
x = a * np.random.random_sample((N,2))
return x #[x,y]
def kreis(radius):
return np.sqrt(radius - x**2)
x = np.linspace(0,1,100)
#create array of N=10 dots
punkte = punkt_im_quadrat(1,10) #a=1, so radius of circle is one
treffer = [i for i in punkte if i[1] <= np.sqrt(1 - i[0]**2)] #dots in circle
treffer = np.array(treffer)
außerhalb = [i for i in punkte if i not in treffer] #dots not in circle
außerhalb = np.array(außerhalb)
pi = 4 * len(treffer) / np.shape(punkte)[0]
fig = plt.figure()
fig, ax = plt.subplots()
ax.plot(x,kreis(1))
ax.set_xlim(0,1)
ax.set_ylim(0,1)
ax.set(title=r"MC Sampling for $\pi$",
ylabel="y-axis",
xlabel="x-axis")
#colors = ["r" if [punkte[i][0],punkte[i][1]] in treffer else "b"]
graph, = ax.plot([],[], "ro")
def animate(i):
graph.set_data((punkte[:i,0],), (punkte[:i,1],))
return graph,
animation = FuncAnimation(fig, func=animate, frames = range(np.shape(punkte)[0]), interval=20, repeat = False)
plt.show()
如您所见,我尝试使用注释颜色 if 语句来更改颜色;但它接着说
IndexError: 用作索引的数组必须是整数(或布尔)类型
然后我想我可以在动画函数中做一个 if 语句,以确定点在哪里。但是当使用 graph.set_color
时,它会改变所有点的颜色。
如果有人能帮助我,我会很高兴。
提前致谢!
plot
中的所有标记都具有相同的颜色,因此无法工作。如果你想要不同的点有不同的颜色,你需要使用scatter()
import matplotlib.pyplot as plt
import numpy as np
from matplotlib.animation import FuncAnimation
def punkt_im_quadrat(a,N): #a is length of square, N number of dots
"""generates random point in [0,a)x[0,a)"""
x = a * np.random.random_sample((N,2))
return x #[x,y]
def kreis(radius):
return np.sqrt(radius - x**2)
x = np.linspace(0,1,100)
#create array of N=10 dots
punkte = punkt_im_quadrat(1,10) #a=1, so radius of circle is one
treffer = [i for i in punkte if i[1] <= np.sqrt(1 - i[0]**2)] #dots in circle
treffer = np.array(treffer)
außerhalb = [i for i in punkte if i not in treffer] #dots not in circle
außerhalb = np.array(außerhalb)
colors = np.array(["r" if i[1] <= np.sqrt(1 - i[0]**2) else "b" for i in punkte])
pi = 4 * len(treffer) / np.shape(punkte)[0]
fig, ax = plt.subplots()
ax.plot(x,kreis(1))
ax.set_xlim(0,1)
ax.set_ylim(0,1)
ax.set(title=r"MC Sampling for $\pi$",
ylabel="y-axis",
xlabel="x-axis")
graph = ax.scatter([],[], marker='o', s=30)
def animate(i):
graph.set_offsets(punkte[:i,:])
graph.set_facecolor(colors[:i])
return graph,
animation = FuncAnimation(fig, func=animate, frames = range(np.shape(punkte)[0]), interval=20, repeat = False)