python 中 3D 数组值的 Matplotlib 动画
Matplotlib animation of the values of a 3D array in python
我目前想要从我的 Walabot 设备中可视化 3D 原始数据,并将其显示在使用 matplotlib FuncAnimation 创建的 3D 动画中。我已经搜索了答案,但找不到任何有用的东西。
在我的例子中,我已经有了一个 3 维数组,其中每个索引都有一个特定的值,该值会随时间变化。我已经知道如何在具有不同颜色和大小的 3D 图表中显示它,但现在我想自己进行更新。我找到了一些示例代码,这给了我一个良好的开端,但我的图表不会自行更新。我必须关闭 window 然后 window 再次弹出,其中包含来自 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)
需要高层的更新功能。此函数清除我的绘图并重新创建具有不同值的新绘图。但我总是需要先关闭情节 window,我想避免这种情况。
这是情节的样子:
3D array plot with matplotlib scatter
你们知道如何解决这个问题吗?
干杯
您的代码实际上并不是一个最小的工作示例,您不应该偷懒,在使用 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()
如果您有任何问题(阅读文档后!),请发表评论。
我目前想要从我的 Walabot 设备中可视化 3D 原始数据,并将其显示在使用 matplotlib FuncAnimation 创建的 3D 动画中。我已经搜索了答案,但找不到任何有用的东西。 在我的例子中,我已经有了一个 3 维数组,其中每个索引都有一个特定的值,该值会随时间变化。我已经知道如何在具有不同颜色和大小的 3D 图表中显示它,但现在我想自己进行更新。我找到了一些示例代码,这给了我一个良好的开端,但我的图表不会自行更新。我必须关闭 window 然后 window 再次弹出,其中包含来自 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)
需要高层的更新功能。此函数清除我的绘图并重新创建具有不同值的新绘图。但我总是需要先关闭情节 window,我想避免这种情况。 这是情节的样子: 3D array plot with matplotlib scatter 你们知道如何解决这个问题吗?
干杯
您的代码实际上并不是一个最小的工作示例,您不应该偷懒,在使用 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()
如果您有任何问题(阅读文档后!),请发表评论。