代码之家  ›  专栏  ›  技术社区  ›  porzoo

python中3D数组值的Matplotlib动画

  •  -1
  • porzoo  · 技术社区  · 8 年前

    我目前想从我的Walabot设备中可视化3D原始数据,并将其显示在使用matplotlib FuncAnimation创建的3D动画中。我已经找到了答案,但没有找到任何有用的。 在我的例子中,我已经有了一个三维数组,其中每个索引都有一个特定的值,该值随时间而变化。我已经知道如何在不同颜色和大小的3D图表中显示它,但现在我想自己进行更新。我发现一些示例代码给了我一个良好的开端,但我的图表本身并没有更新。我必须关闭窗口,然后窗口再次弹出,带有3D数组中的不同值。你们知道怎么解决这个问题吗?

    def update(plot, signal, figure):
        plot.clear()
        scatterplot = plot.scatter(x, y, z, zdir='z', s=signal[0], c=signal[0])
        figure.show()
        return figure
    
    def calc_RasterImage(signal):
        # 3D index is represnted is the following schema {i,j,k}
        #  sizeX - signal[1] represents the i dimension length
        #  sizeY - signal[2] represents the j dimension length
        #  sizeZ - signal[3] represents the k dimension length
        #  signal[0][i][j][k] - represents the walabot 3D scanned image (internal data)
    
        #Initialize 3Dplot with matplotlib                      
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        ax.set_xlim([xMin-1,xMax-1])
        ax.set_ylim([yMin-1,yMax-1])
        ax.set_zlim([zMin-1,zMax-1])
        ax.set_xlabel('X AXIS')
        ax.set_ylabel('Y AXIS')
        ax.set_zlabel('Z AXIS')
        scatterplot = ax.scatter(x, y, z, zdir='z', s=signal[0], c= signal[0])
        cbar = plt.colorbar(scatterplot)
        cbar.set_label('Density')
        #def update(signal):
        #        ax.clear()
        #       scatterplot = ax.scatter(x, y, z, zdir='z', s=signal[0], c=signal[0])
        ani = anim.FuncAnimation(fig, update(ax, signal, plt), frames=10 , blit=True, repeat = True)
    
    def main():
        wlbt = Walabot()
        wlbt.connect()
        if not wlbt.isConnected:
                print("Not Connected")
        else:
                print("Connected")
        wlbt.start()
        calc_index(wlbt.get_RawImage_values())
        while True:
                #print_RawImage_values(wlbt.get_RawImage_values())
                calc_RasterImage(wlbt.get_RawImage_values())
        wlbt.stop()
    
    if __name__ == '__main__':
        main()
    

    正如你所看到的那样

    ani = anim.FuncAnimation(fig, update(ax, signal, plt), frames=10 , blit=True, repeat = True)
    

    需要从顶部更新功能。此函数用于清除我的绘图,并用不同的值重新创建新的绘图。但我总是需要先关闭绘图窗口,我希望避免这种情况。 情节是这样的: 3D array plot with matplotlib scatter

    1 回复  |  直到 8 年前
        1
  •  -1
  •   Paul Brodersen    8 年前

    您的代码并不是一个真正的最小工作示例,在开始之前,您不应该懒惰并实际阅读FuncAnimation的文档。也就是说,这样的事情应该奏效:

    #!/usr/bin/env python
    # -*- coding: utf-8 -*-
    
    """
    Display walabot output.
    """
    
    import matplotlib.pyplot as plt
    from matplotlib.animation import FuncAnimation
    
    def display(walabot_instance):
    
        # set x, y, z
    
        fig = plt.figure()
        ax = fig.add_subplot(111, projection='3d')
        path_collection = ax.scatter(x, y, z, zdir='z')
    
        # do your labelling, layout etc
    
        def update(ignored, walabot_instance):
            signal = walabot_instance.get_RawImage_values()
            path_collection.set_sizes(signal[0])
            path_collection.set_color(signal[1])
            return path_collection,
    
        return FuncAnimation(fig, update, fargs=[walabot_instance])
    
    def main():
        wlbt = Walabot()
        wlbt.connect()
        if not wlbt.isConnected:
            print("Not Connected")
        else:
            print("Connected")
        wlbt.start()
    
        plt.ion()
        animation = display(wlbt)
        raw_input("Press any key when done watching Walabot...")
    
    
    if __name__ == "__main__":
        main()
    

    如果您有任何问题(在阅读文档之后!),删除评论。