【发布时间】:2020-08-25 11:43:17
【问题描述】:
我正在使用this tutorial of the Barnsley fern 中的代码,我希望能够缓慢地绘制蕨类植物(而不是一次)。 Python 还不是很舒服,我看到函数 plt.pause() 可能会在其他一些答案中起到作用;但是,我不知道如何将 plt.pause() 与 plt.scatter() 结合起来以获得动画 gif 效果。例如,我是否必须将 plt.scatter 合并到 for 循环中?
# importing necessary modules
import matplotlib.pyplot as plt
from random import randint
# initializing the list
x = []
y = []
# setting first element to 0
x.append(0)
y.append(0)
current = 0
for i in range(1, 50000):
# generates a random integer between 1 and 100
z = randint(1, 100)
# the x and y coordinates of the equations
# are appended in the lists respectively.
# for the probability 0.01
if z == 1:
a = 0; b = 0; c = 0; d = 0.16; e = 0; f = 0
x.append(0)
y.append(d*(y[current]))
# for the probability 0.85
if z>= 2 and z<= 86:
a = 0.85; b = 0.04; c = -0.04; d = 0.85; e = 0; f = 1.6
x.append(a*(x[current]) + b*(y[current]))
y.append(c*(x[current]) + d*(y[current])+f)
# for the probability 0.07
if z>= 87 and z<= 93:
a = 0.2; b = -0.26; c = 0.23; d = 0.22; e = 0; f = 1.6
x.append(a*(x[current]) + b*(y[current]))
y.append(c*(x[current]) + d*(y[current])+f)
# for the probability 0.07
if z>= 94 and z<= 100:
a = -0.15; b = 0.28; c = 0.26; d = 0.24; e = 0; f = 0.44
x.append(a*(x[current]) + b*(y[current]))
y.append(c*(x[current]) + d*(y[current])+f)
current = current + 1
plt.figure(figsize=(10,10))
plt.scatter(x, y, s = 0.2, edgecolor ='green')
这是想要的效果:
【问题讨论】:
标签: python matplotlib plot