我正在嘗試創建數字 pi 的 Monte-Carlo 估計影片,對于每次迭代,我希望數值估計出現在繪圖上的文本中,但之前的文本不會被洗掉并且使值不可讀。我試過Artist.remove(frame)沒有成功。情節是用木星筆記本完成的。
#Enable interactive plot
%matplotlib notebook
import math
from matplotlib.path import Path
from matplotlib.animation import FuncAnimation
from matplotlib.path import Path
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
from matplotlib.artist import Artist
N = 10000
#create necessary arrays
x = np.arange(0,N)
y = np.zeros(N)
#set initial points to zero
inHull = 0
def inCircle(point):
#the function is given a point in R^n
#returns a boolean stating if the norm of the point is smaller than 1.
if np.sum(np.square(point)) <= 1:
return True
else:
return False
#iterate over each point
for i in range(N):
random_point = np.random.rand(2)*2 - 1
#determine if the point is inside the hull
if inCircle(random_point):
inHull = 1
#we store areas in array y.
y[i] = (inHull*4)/(i 1)
fig = plt.figure()
ax = plt.subplot(1, 1, 1)
data_skip = 20
def init_func():
ax.clear()
plt.xlabel('n points')
plt.ylabel('Estimated area')
plt.xlim((x[0], x[-1]))
plt.ylim((min(y)- 1, max(y) 0.5))
def update_plot(i):
ax.plot(x[i:i data_skip], y[i:i data_skip], color='k')
ax.scatter(x[i], y[i], color='none')
Artist.remove(ax.text(N*0.6, max(y) 0.25, "Estimation: " str(round(y[i],5))))
ax.text(N*0.6, max(y) 0.25, "Estimation: " str(round(y[i],5)))
anim = FuncAnimation(fig,
update_plot,
frames=np.arange(0, len(x), data_skip),
init_func=init_func,
interval=20)
plt.show()
謝謝你。
uj5u.com熱心網友回復:
正如您已經在 中所做的那樣 init_func,您應該在每次迭代中使用 清除繪圖ax.clear()。然后有必要稍微編輯繪圖功能:
ax.plot(x[i:i data_skip], y[i:i data_skip], color='k')
最后,您必須在每次迭代中使用ax.set_xlim(0, N).
完整代碼
#Enable interactive plot
%matplotlib notebook
import math
from matplotlib.path import Path
from matplotlib.animation import FuncAnimation
from matplotlib.path import Path
import numpy as np
import matplotlib.pyplot as plt
from scipy.spatial import ConvexHull
from matplotlib.artist import Artist
N = 10000
# create necessary arrays
x = np.arange(0, N)
y = np.zeros(N)
# set initial points to zero
inHull = 0
def inCircle(point):
# the function is given a point in R^n
# returns a boolean stating if the norm of the point is smaller than 1.
if np.sum(np.square(point)) <= 1:
return True
else:
return False
# iterate over each point
for i in range(N):
random_point = np.random.rand(2)*2 - 1
# determine if the point is inside the hull
if inCircle(random_point):
inHull = 1
# we store areas in array y.
y[i] = (inHull*4)/(i 1)
fig = plt.figure()
ax = plt.subplot(1, 1, 1)
data_skip = 20
txt = ax.text(N*0.6, max(y) 0.25, "")
def init_func():
ax.clear()
plt.xlabel('n points')
plt.ylabel('Estimated area')
plt.xlim((x[0], x[-1]))
plt.ylim((min(y) - 1, max(y) 0.5))
def update_plot(i):
ax.clear()
ax.plot(x[:i data_skip], y[:i data_skip], color = 'k')
ax.scatter(x[i], y[i], color = 'none')
ax.text(N*0.6, max(y) 0.25, "Estimation: " str(round(y[i], 5)))
ax.set_xlim(0, N)
anim = FuncAnimation(fig,
update_plot,
frames = np.arange(0, len(x), data_skip),
init_func = init_func,
interval = 20)
plt.show()
影片

轉載請註明出處,本文鏈接:https://www.uj5u.com/caozuo/483270.html
標籤:Python 麻木的 matplotlib 动画 数学
上一篇:計算百分比Python
