python中3D数组值的Matplotlib动画

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

我目前想将Walabot设备中的3D rawdata可视化,并将其显示在使用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)
需要顶部的更新功能。此函数用于清除我的绘图,并使用不同的值重新创建新的绘图。但我总是需要先关闭绘图窗口,我希望避免这种情况。 这是情节的样子: 你们有办法解决这个问题吗


干杯

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

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

谢谢你的帮助!在试用了你的代码并将其调整到我的代码之后,它终于成功了。我在看文档(),但我不知道如何正确使用它,因为我不太擅长编码。无论如何,这是我的新代码:

def calc_index(signal):
    for i in range(0, signal[1], 1):
            for j in range(0, signal[2], 1):
                    for k in range(0, signal[3], 1):
                            #Location of Index
                            x.append(i)
                            y.append(j)
                            z.append(k)

def display(walabot_instance):
    # 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')
    signal = walabot_instance.get_RawImage_values()
    path_collection = ax.scatter(x, y, z, zdir='z', s=signal[0], c=signal[0])

    #Plot labeling
    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')
    cbar = plt.colorbar(path_collection)
    cbar.set_label('Density')

    def update(ignored, walabot_instance):
            ax.clear()
            signal_update = walabot_instance.get_RawImage_values()
            path_collection = ax.scatter(x, y, z, zdir='z', s=signal_update[0], c=signal_update[0])
            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()

    calc_index(wlbt.get_RawImage_values())
    plt.ion()
    animation = display(wlbt)

    raw_input("Press any key when done watching Walabot...")
    wlbt.stop()

if __name__ == '__main__':
    main()
我仍然不明白的是你在用什么

fargs=[walabot_instance]
在的动画函数中?我只是没有得到这方面的文件。。 这意味着什么

def func(fr: object, *fargs) -> iterable_of_artists:

在func和frames参数的FuncAnimation文档中?只是为了更好地理解这个过程

为什么更新需要被忽略的参数输入?它不在任何地方使用


非常感谢

批评缺失的mcve并仍然提供答案似乎有点矛盾,而答案本身并没有任何解释。我想提醒发问者问题缺少清晰性应该是一个评论;另一方面,如果你能回答这个问题,为什么还要为mcve丢失而烦恼呢?或者反过来说:如果问题不清楚,为什么要提供答案?在任何情况下,输入一段代码,然后说“如果你想理解它,请阅读文档”同样糟糕。@ImportanceOfBeingErnest我让他先阅读文档,然后再问剩余的问题。我想说的是,我实际说的话和你描述它的方式是有区别的。
FuncAnimation
不是为你的用例设计的。预期的用例是,您正在描述一个动态系统的演化,这是确定性的,因为您已经拥有所有数据,或者您知道将系统状态描述为时间函数的函数<代码>更新因此需要一个浮点数或一个表示时间的整数,可用于计算系统的新状态或新状态本身(以生成器函数或数据本身的形式)。在您的情况下,系统的新状态在运行时确定。你也许可以把你的实例包装成一个看起来像发电机的东西(它不会),但对我来说这似乎太复杂了。因此,
frame
参数是多余的,我忽略了它。
fargs
用于向更新函数传递附加参数。查看代码,我刚刚意识到您可能甚至不需要传入它(最初,我没有将
update
函数设计为闭包,在这种情况下,walabot实例不在范围内,因此您不需要传入它)。
def gen_function() -> obj: