【问题标题】:Matplotlib animation of the values of a 3D array in pythonpython中3D数组值的Matplotlib动画
【发布时间】:2018-03-08 11:17:47
【问题描述】:

我目前想从我的 Walabot 设备中可视化 3D 原始数据,并将其显示在使用 matplotlib FuncAnimation 创建的 3D 动画中。我已经搜索了答案,但找不到任何有用的东西。 就我而言,我已经有一个 3 维数组,其中每个索引都有一个特定的值,该值会随着时间而变化。我已经可以弄清楚如何在具有不同颜色和大小的 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 你们知道如何解决这个问题吗?

干杯

【问题讨论】:

    标签: python arrays matplotlib 3d refresh


    【解决方案1】:

    您的代码实际上并不是一个最小的工作示例,您不应该偷懒,在开始 SO 之前实际阅读 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()
    

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

    【讨论】:

    • 批评缺失的mcve却仍然提供答案似乎有点矛盾,而答案本身并没有任何解释。我想提醒提问者问题的清晰度应该是评论;另一方面,如果您能够回答这个问题,为什么还要担心缺少 mcve?或者反过来:如果问题不清楚,为什么要提供答案?无论如何,输入一段代码然后说“如果你想理解它,请阅读文档”同样糟糕。
    • @ImportanceOfBeingErnest 我让他首先阅读文档,然后问任何剩余的问题。我会争辩说,我实际所说的和你的描述是有区别的。
    猜你喜欢
    • 2021-10-21
    • 1970-01-01
    • 1970-01-01
    • 2012-07-07
    • 2016-11-02
    • 1970-01-01
    • 2021-04-02
    • 2014-05-21
    相关资源
    最近更新 更多