Python Matplotlib blitting多个艺术家
我正在尝试创建一个使用blitting的annimated Matplotlib图表。我想在同一个子图中包括散点图、线图和注释。然而,我发现的所有例子,例如,似乎只返回一个艺术家,例如,一个线条图。(我认为我使用的艺术家术语是正确的,但可能不是。) 我曾尝试将多个艺术家组合在一起,但这似乎不起作用。例如,在下面的示例中,打印线不会更新,如果blit设置为True,我会得到一个AttributeError:“Artisters”对象没有属性“set_animated”Python Matplotlib blitting多个艺术家,python,python-3.x,matplotlib,Python,Python 3.x,Matplotlib,我正在尝试创建一个使用blitting的annimated Matplotlib图表。我想在同一个子图中包括散点图、线图和注释。然而,我发现的所有例子,例如,似乎只返回一个艺术家,例如,一个线条图。(我认为我使用的艺术家术语是正确的,但可能不是。) 我曾尝试将多个艺术家组合在一起,但这似乎不起作用。例如,在下面的示例中,打印线不会更新,如果blit设置为True,我会得到一个AttributeError:“Artisters”对象没有属性“set_animated” from collectio
from collections import namedtuple
import numpy as np
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
Artists = namedtuple('Artists', ('scatter', 'plot'))
artists = Artists(
ax.scatter([], []),
ax.plot([], [], animated=True)[0],
)
def init():
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
return artists,
def update(frame):
artists.scatter.set_offsets([[0, 0]])
artists.plot.set_data([0, 1], [0, 1])
return artists,
ani = FuncAnimation(
fig=fig,
func=update,
init_func=init,
blit=True)
plt.show()
与多个艺术家进行blitting的正确方式是什么?文档中说
func
:可调用
在每帧调用的函数。第一个参数将是帧中的下一个值。任何附加的位置参数都可以通过fargs参数提供
所需签名为:
def func(frame, *fargs) -> iterable_of_artists:
因此,返回类型应该是列表、元组或通常是Artists
的iterable
使用返回艺术家时,
返回艺术家的可编辑列表
所以你可以去掉逗号
return artists
更一般地说,命名的元组似乎比它在这里帮助更多,所以为什么不简单地返回一个元组呢
import matplotlib.pyplot as plt
from matplotlib.animation import FuncAnimation
fig, ax = plt.subplots()
scatter = ax.scatter([], [])
plot = ax.plot([], [], animated=True)[0]
def init():
ax.set_xlim(-1, 1)
ax.set_ylim(-1, 1)
return scatter, plot
def update(frame):
scatter.set_offsets([[0, 0]])
plot.set_data([0, 1], [0, 1])
return scatter, plot
ani = FuncAnimation(
fig=fig,
func=update,
init_func=init,
blit=True)
plt.show()
我想把这件事弄得太复杂了。非常感谢。