【问题标题】:scatter update with animation用动画散布更新
【发布时间】:2017-02-01 13:09:47
【问题描述】:

我正在尝试使用 matplotlib 的动画模块制作实时散点图,但我是个新手。我的目标是在收到要绘制的数据时更新绘图,以便在收到数据时,以前的点消失并绘制新的点。

如果我用无限循环和随机生成的数据代替接收数据,我的程序可以这样写:

fig = plt.figure()
skyplot = fig.add_subplot(111, projection='polar')
skyplot.set_ylim(90)  # sets radius of the circle to maximum elevation
skyplot.set_theta_zero_location("N")  # sets 0(deg) to North
skyplot.set_theta_direction(-1)  # sets plot clockwise
skyplot.set_yticks(range(0, 90, 30))  # sets 3 concentric circles
skyplot.set_yticklabels(map(str, range(90, 0, -30)))  # reverse labels
plt.ion()

while(1):

    azimuths = random.sample(range(360), 8)
    elevations = random.sample(range(90), 8)
    colors = numpy.random.rand(3,1)

    sat_plot = satellite()
    ani= animation.FuncAnimation(fig, sat_plot.update, azimuths, elevations, colors)

class satellite:

    def __init__(self):
        self.azimuths = []
        self.elevations = []
        self.colors = []
        self.scatter = plt.scatter(self.azimuths, self.elevations, self.colors)

    def update(self, azimuth, elevation, colors):
        self.azimuths = azimuth
        self.elevations = elevation
        return self.scatter

现在,我收到以下错误:

> Traceback (most recent call last):
  File "./skyplot.py", line 138, in <module>
    ani= animation.FuncAnimation(fig, sat_plot.update, azimuths, elevations, colors)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 442, in __init__
    TimedAnimation.__init__(self, fig, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 304, in __init__
    Animation.__init__(self, fig, event_source=event_source, *args, **kwargs)
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 53, in __init__
    self._init_draw()
  File "/usr/lib/pymodules/python2.7/matplotlib/animation.py", line 469, in _init_draw
    self._drawn_artists = self._init_func()
TypeError: 'list' object is not callable

谁能告诉我我做错了什么以及我该怎么做?

提前致谢

【问题讨论】:

  • 我不确定您是否正确使用了FuncAnimation,是吗?不应该叫FuncAnimation(fig,sat_plot.update,fargs=(azimuths,elevation,colors))

标签: python animation matplotlib scatter-plot


【解决方案1】:

我认为你不需要动画。您需要一个简单的无限循环(例如while),并在线程中进行绘图更新。我可以提出这样的建议:

import threading,time
import matplotlib.pyplot as plt
import numpy as np

fig = plt.figure()
data = np.random.uniform(0, 1, (5, 3))
plt.scatter(data[:, 0], data[:,1],data[:, 2]*50)

def getDataAndUpdate():
    while True:
        """update data and redraw function"""
        new_data = np.random.uniform(0, 1, (5, 3))
        time.sleep(1)
        plt.clf()
        plt.scatter(new_data[:, 0], new_data[:, 1], new_data[:, 2] * 50)
        plt.draw()

t = threading.Thread(target=getDataAndUpdate)
t.start()
plt.show()

结果是带有散点图的类似动画的图形。

【讨论】:

  • 感谢您的回答,但我想避免使用画布。你知道我该怎么做吗?
  • 虽然我不明白你为什么不想使用画布,但是有'plt.clf()'和'plt.draw()'的解决方案。请查看新代码。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2012-01-12
  • 1970-01-01
  • 2017-08-21
  • 1970-01-01
  • 1970-01-01
  • 2013-02-04
  • 2019-03-07
相关资源
最近更新 更多