python animation函数_FuncAnimation不显示在函数之外

关于这个问题,我给出了一个答案,讨论了matplotlib上的衰落点。我对重要的Beingernest的回答很好奇。所以我试着玩弄他的代码。在

首先,这是我的代码。在import numpy as np

import matplotlib.pyplot as plt

import matplotlib.animation

from matplotlib.colors import LinearSegmentedColormap

def get_new_vals():

x = 0

y = 0

while True:

if x >= .9:

x = 0

y = 0

x += .1

y += .1

yield x, y

def update(t, x_vals, y_vals, intensity, scatter, gen):

# Get intermediate points

new_xvals, new_yvals = gen.next()

x_vals.extend([new_xvals])

y_vals.extend([new_yvals])

# Put new values in your plot

scatter.set_offsets(np.c_[x_vals, y_vals])

# Calculate new color values

for index in range(len(intensity)):

if intensity[index] < .1:

intensity[index] = 0

intensity[index] *= .6

intensity.extend(1 for _ in xrange(len([new_xvals])))

intens_dup = np.array(intensity)

"""

intensity = np.concatenate((np.array(intensity) * .6, np.ones(len(new_xvals))))

"""

scatter.set_array(intens_dup)

# Set title

axis.set_title('Time: %0.3f' % t)

def anim_random_points(fig, axis):

x_vals = []

y_vals = []

intensity = []

iterations = 100

colors = [ [0, 0, 1, 0], [0, 0, 1, 0.5], [0, 0.2, 0.4, 1] ]

cmap = LinearSegmentedColormap.from_list("", colors)

scatter = axis.scatter(x_vals, y_vals, c=[], cmap=cmap, vmin=0, vmax=1)

gen_values = get_new_vals()

ani = matplotlib.animation.FuncAnimation(fig, update, frames=iterations,

interval=50, fargs=(x_vals, y_vals, intensity, scatter, gen_values),

repeat=False)

# Position 1 for plt.show()

plt.show()

if __name__ == '__main__':

fig, axis = plt.subplots()

axis.set_xlabel('X Axis', size = 12)

axis.set_ylabel('Y Axis', size = 12)

axis.axis([0,1,0,1])

anim_random_points(fig, axis)

# Position 2 for plt.show()

# plt.show()

我注意到一件奇怪的事。至少对我来说。注意Position 1和{}(在代码末尾)。位置1放在animation函数之后,另一个放在代码之后,因为函数在位置1之后结束,因此转到位置2。在

由于FuncAnimation需要figure来运行动画,我想知道为什么plt.show()在位置1工作,而在位置2不工作。在

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值