使用Matplotlib绘制数据的动画散点图

时间:2012-10-15 09:33:58

标签: python matplotlib animated scatter

我尝试以这种方式调整动画散点图示例,因此它实时显示了我开发的基于代理的模型的结果。但是,图表中显示的结果并不是我除了它们之外的结果。 更新值时出错,并且代理倾向于在对角线中聚集的地方出现奇怪的模式。 我添加了一些简单的代码来说明这个问题。有没有人知道出了什么问题?

import matplotlib.pyplot as plt
import matplotlib.animation as animation
import numpy as np
import os

n = 25 ## nr of agents
x,y = 10, 10 ## matrix of x by y dimension
dataX, dataY, binaryRaster = [],[],[]

class AnimatedScatter(object):
    """An animated scatter plot using matplotlib.animations.FuncAnimation."""
    def __init__(self):
        global n
        self.numpoints = n
        self.stream = self.data_stream()
        self.fig, self.ax = plt.subplots()
        self.ax.set_title("My first Agent Based Model (ABM)",fontsize=14)
        self.ax.grid(True,linestyle='-',color='0.75')
        self.ani = animation.FuncAnimation(self.fig, self.update, interval=100, 
                                           init_func=self.setup_plot, blit=True,
                                           repeat=False)

    def setup_plot(self):
        """Initial drawing of the scatter plot."""
        global x,y
        dataX,dataY = next(self.stream)
        self.scat = self.ax.scatter(dataY, dataX, c="tomato", s=20, animated=True)
        self.ax.axis([0, y, x, 0])
        return self.scat,

    def data_stream(self):
        """Generate a random walk (brownian motion). Data is scaled to produce
        a soft "flickering" effect."""
        global x,y, n

        dataX,dataY = self.createRandomData()

        #printing results to ascii for validation
        lines = []
        binaryData = np.zeros((x,y), dtype=np.int)
        for i in range(n):
            binaryData[dataX,dataY] =1
        for i in range(x):
            line = ""
            for j in range(y):
                line += str(binaryData[i,j])+ ","
            line= line[:-1]+ "\n"
            lines.append(line)
        lines.append("\n")

        yx = np.array([dataY,dataX])
        cnt = 0
        while cnt < 10:
            dataX,dataY = self.createRandomData()
            yx = np.array([dataY,dataX])

            #printing results to ascii for validation
            binaryData = np.zeros((x,y), dtype=np.int)
            for i in range(n):
                binaryData[dataX,dataY] =1
            for i in range(x):
                line = ""
                for j in range(y):
                    line += str(binaryData[i,j])+ ","
                line= line[:-1]+ "\n"
                lines.append(line)
            lines.append("\n")

            cnt+=1
            yield yx

        #printing results to ascii for validation

        outNm = os.getcwd()+"\\ScatterValidation.txt"
        outfile = open(outNm, "w")
        outfile.writelines(lines)
        outfile.close()
        return

    def update(self, i):
        """Update the scatter plot."""
        data = next(self.stream)
        self.scat.set_offsets(data[:2, :])
        return self.scat,

    def show(self):
        plt.show()

    def createRandomData(self):
        """Positions n agents randomly on a raster of x by y cells.
        Each cell can only hold a single agent."""

        global x,y,n
        binaryData = np.zeros((x,y), dtype=np.int)
        newAgents = 0
        dataX,dataY = [],[]
        while newAgents < n:
            row = np.random.randint(0,x,1)[0]
            col = np.random.randint(0,y,1)[0]
            if binaryData[row][col] != 1:
                binaryData[row][col] = 1
                newAgents+=1

        for row in range(x):
            for col in range(y):
                if binaryData[row][col] == 1:
                    dataX.append(row)
                    dataY.append(col)
        return dataX, dataY

def main():
    global n, x, y, dataX, dataY, binaryRaster
    a = AnimatedScatter()
    a.show()
    return

if __name__ ==  "__main__":
    main()

1 个答案:

答案 0 :(得分:5)

您可以通过两种方式修复脚本,两者都涉及更改update功能:

  1. 在更新功能中使用分散调用,我认为更清楚
  2. 在更新
  3. 中调用set_offsets之前转置数据数组

    使用分散调用是最明确的修复,您可以在运行期间增加代理:

    def update(self, i):
        """Update the scatter plot."""
        dataX, dataY = next(self.stream)
        self.scat = self.ax.scatter(dataX, dataY, c="tomato", s=20, animated=True)
        return self.scat,
    

    转置偏移数组也可以:

    def update(self, i):
        """Update the scatter plot."""
        data = next(self.stream)
        self.scat.set_offsets(data.transpose())
        return self.scat,
    

    偏移量作为N个元组给出,每个元组2个项目,而数据数组给出为2个元组,每个元素包含N个项目,转置数据数组将解决您的问题。

    注意:如果不更改全局变量,则不需要使用全局语句指定全局变量,因此在setup_plot__init__等中,您可以删除{{1}行。 我会将global n,x,ynx作为您班级的实例变量,而且不需要ydataXdataY在脚本的顶部定义。