Matplotlib在python中三维数组值的动画

2024-10-01 13:37:04 发布

您现在位置:Python中文网/ 问答频道 /正文

我现在想从我的Walabot设备中可视化三维图形数据,并在用matplotlib FuncAnimation创建的三维动画中显示它。我已经在寻找答案了,但找不到任何有用的东西。 在我的例子中,我已经有了一个三维数组,其中每个索引都有一个特定的值,该值随着时间的推移而变化。我已经可以想出如何用不同的颜色和大小在三维图表中显示它,但现在我想更新自己。我找到了一些示例代码,这些代码给了我一个良好的开端,但是我的图表本身并没有更新。我必须关闭窗口,然后窗口再次弹出,其中包含来自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()

正如你所看到的

^{pr2}$

需要从顶部更新函数。此函数用于清除“我的绘图”并用不同的值重新创建新的绘图。但我总是需要先关闭绘图窗口,这是我想避免的。 情节如下: 3D array plot with matplotlib scatter 你们知道怎么解决这个问题吗?在

干杯


Tags: the代码signalplotmatplotlibdefaxfigure
2条回答

谢谢你的帮助!在试用了您的代码并将其调整为我的代码之后,它终于成功了。我在看文档(https://matplotlib.org/api/_as_gen/matplotlib.animation.FuncAnimation.html#matplotlib.animation.FuncAnimation),但我不知道如何正确地使用它,因为我不擅长编码。总之,这是我的新代码:

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()

我还是不明白你在用什么

^{pr2}$

在的FuncAnimation函数中?我只是没有得到这方面的文件。。 什么意思

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

以及

def gen_function() -> obj:

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

为什么update需要被忽略的参数输入?它在任何地方都不用。。在

非常感谢!在

你的代码并不是一个非常简单的工作示例,你不应该偷懒,在开始之前先阅读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()

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

相关问题 更多 >