How to animate a scatter plot?

后端 未结 4 1876
清酒与你
清酒与你 2020-11-22 17:18

I\'m trying to do an animation of a scatter plot where colors and size of the points changes at different stage of the animation. For data I have two numpy ndarray with an x

4条回答
  •  孤独总比滥情好
    2020-11-22 17:41

    Here is the thing. I used to a user of Qt and Matlab and I am not quite familiar with the animation system on the matplotlib.

    But I do have find a way that can make any kind of animation you want just like it is in matlab. It is really powerful. No need to check the module references and you are good to plot anything you want. So I hope it can help.

    The basic idea is to use the time event inside PyQt( I am sure other Gui system on the Python like wxPython and TraitUi has the same inner mechanism to make an event response. But I just don't know how). Every time a PyQt's Timer event is called I refresh the whole canvas and redraw the whole picture, I know the speed and performance may be slowly influenced but it is not that much.

    Here is a little example of it:

    import sys
    from PyQt4 import QtGui
    
    from matplotlib.figure import Figure
    from matplotlib.backends.backend_qt4agg import FigureCanvasQTAgg as FigureCanvas
    
    import numpy as np
    
    
    class Monitor(FigureCanvas):
        def __init__(self):
            self.fig = Figure()
            self.ax = self.fig.add_subplot(111)
    
            FigureCanvas.__init__(self, self.fig)
            self.x = np.linspace(0,5*np.pi,400)
            self.p = 0.0
            self.y = np.sin(self.x+self.p)
    
    
            self.line = self.ax.scatter(self.x,self.y)
    
            self.fig.canvas.draw()
    
            self.timer = self.startTimer(100)
    
    
        def timerEvent(self, evt):
            # update the height of the bars, one liner is easier
            self.p += 0.1
            self.y = np.sin(self.x+self.p)
            self.ax.cla()
            self.line = self.ax.scatter(self.x,self.y)
    
            self.fig.canvas.draw()
    
    
    
    if __name__ == "__main__":
        app = QtGui.QApplication(sys.argv)
        w = Monitor()
        w.setWindowTitle("Convergence")
        w.show()
        sys.exit(app.exec_())
    

    You can adjust the refresh speed in the

            self.timer = self.startTimer(100)
    

    I am just like you who want to use the Animated scatter plot to make a sorting animation. But I just cannot find a so called "set" function. So I refreshed the whole canva.

    Hope it helps..

提交回复
热议问题