有人可以帮我为这个 python 代码写一个动画函数吗?
Can someone help me write an animation function for this python code?
我正在尝试学习如何为这个使用蒙特卡洛方法计算函数积分的图形制作动画,但没有成功。我对python了解不多,这是我几年前除了学习一些语言基础之外的第一个代码。这是我到目前为止写的。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
N = 100000
print("N=", N)
x_list = []
y_list = []
x_list.append(np.random.uniform(low=-5, high=5, size=[N, 1]))
y_list.append(np.random.uniform(low=0, high=2, size=[N, 1]))
x = np.array(x_list)
y = np.array(y_list)
ins = y - np.exp((-x**2) / 2) < 0
ap_pi = 20 * np.sum(ins) / N
print('pi: {}, approximation: {}'.format(np.pi, ap_pi))
print(ap_pi)
x_in = x[ins]
y_in = y[ins]
fig = plt.figure(figsize=[10, 10])
plt.text(1, 2.145, "Value of the integral:", fontsize=14)
plt.text(4, 2.15, ap_pi, bbox=dict(facecolor='red', alpha=0.5))
plt.scatter(x_list, y_list, s=1)
plt.scatter(x_in, y_in, color='r', s=1)
def animation(i):
anim = FuncAnimation(fig, animation, frames=100, interval=20)
plt.pause(0.01)
plt.show()
我确实尝试将 plt.scatter 移动到动画功能,但这只会导致以某种方式对颜色进行动画处理。我还尝试了多种方法,但最终以打开图表本身的循环告终。我不知道如何进行。有帮助吗?
这应该是一个很好的起点:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
N = 100000
print("N=", N)
x = np.random.uniform(-5, 5, N)
y = np.random.uniform(0, 2, N)
ins = y - np.exp((-x**2) / 2) < 0
ap_pi = 20 * np.sum(ins) / N
print('pi: {}, approximation: {}'.format(np.pi, ap_pi))
print(ap_pi)
x_in = x[ins]
y_in = y[ins]
def animation(i):
line1.set_data(x[:i], y[:i])
if i < len(x_in):
# remember that len(x_in) < N
line2.set_data(x_in[:i], y_in[:i])
fig = plt.figure(figsize=[10, 10])
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim(-5, 5)
ax.set_ylim(0, 2)
ax.text(1, 2.145, "Value of the integral:", fontsize=14)
ax.text(4, 2.15, ap_pi, bbox=dict(facecolor='red', alpha=0.5))
# initialize empty scatter plots. They will receive updated data below.
# NOTE: I'm using `ax.plot` because I know what method I have to call
# when updating data. If I use `ax.scatter`, who knows... :|
line1, = ax.plot([], [], 'o')
line2, = ax.plot([], [], 'o', color='r')
# The length of the animation is given by N
anim = FuncAnimation(fig, animation, frames=N, interval=20)
plt.show()
我正在尝试学习如何为这个使用蒙特卡洛方法计算函数积分的图形制作动画,但没有成功。我对python了解不多,这是我几年前除了学习一些语言基础之外的第一个代码。这是我到目前为止写的。
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
N = 100000
print("N=", N)
x_list = []
y_list = []
x_list.append(np.random.uniform(low=-5, high=5, size=[N, 1]))
y_list.append(np.random.uniform(low=0, high=2, size=[N, 1]))
x = np.array(x_list)
y = np.array(y_list)
ins = y - np.exp((-x**2) / 2) < 0
ap_pi = 20 * np.sum(ins) / N
print('pi: {}, approximation: {}'.format(np.pi, ap_pi))
print(ap_pi)
x_in = x[ins]
y_in = y[ins]
fig = plt.figure(figsize=[10, 10])
plt.text(1, 2.145, "Value of the integral:", fontsize=14)
plt.text(4, 2.15, ap_pi, bbox=dict(facecolor='red', alpha=0.5))
plt.scatter(x_list, y_list, s=1)
plt.scatter(x_in, y_in, color='r', s=1)
def animation(i):
anim = FuncAnimation(fig, animation, frames=100, interval=20)
plt.pause(0.01)
plt.show()
我确实尝试将 plt.scatter 移动到动画功能,但这只会导致以某种方式对颜色进行动画处理。我还尝试了多种方法,但最终以打开图表本身的循环告终。我不知道如何进行。有帮助吗?
这应该是一个很好的起点:
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
N = 100000
print("N=", N)
x = np.random.uniform(-5, 5, N)
y = np.random.uniform(0, 2, N)
ins = y - np.exp((-x**2) / 2) < 0
ap_pi = 20 * np.sum(ins) / N
print('pi: {}, approximation: {}'.format(np.pi, ap_pi))
print(ap_pi)
x_in = x[ins]
y_in = y[ins]
def animation(i):
line1.set_data(x[:i], y[:i])
if i < len(x_in):
# remember that len(x_in) < N
line2.set_data(x_in[:i], y_in[:i])
fig = plt.figure(figsize=[10, 10])
ax = fig.add_subplot(1, 1, 1)
ax.set_xlim(-5, 5)
ax.set_ylim(0, 2)
ax.text(1, 2.145, "Value of the integral:", fontsize=14)
ax.text(4, 2.15, ap_pi, bbox=dict(facecolor='red', alpha=0.5))
# initialize empty scatter plots. They will receive updated data below.
# NOTE: I'm using `ax.plot` because I know what method I have to call
# when updating data. If I use `ax.scatter`, who knows... :|
line1, = ax.plot([], [], 'o')
line2, = ax.plot([], [], 'o', color='r')
# The length of the animation is given by N
anim = FuncAnimation(fig, animation, frames=N, interval=20)
plt.show()