Animating Monte Carlo Method Pi color dots diferently

Deadly 提交于 2021-01-29 06:00:49

问题


I want to have an animation of my plot of my results of the mc method calculating pi; but having the inner circle dots colored differently than the others. How can I do that? This is my code so far:

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

As you can see, I have tried to change the colors by using the commented color if statement; but it then says
IndexError: arrays used as indices must be of integer (or boolean) type

I then thought I could do an if statement in the animated-function, to determine where the dot is. But when using graph.set_color it changes all dots color.

I'd be really happy if someone can help me.
Thanks in advance!


回答1:


All the markers in a plot have the same color, so that cannot work. If you want different colors for different points, you need to use 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)


来源:https://stackoverflow.com/questions/61012851/animating-monte-carlo-method-pi-color-dots-diferently

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