在 Vispy 中创建 matplotlib 风格的 3d 散点图 z 轴

Create matplotlib-style 3d scattergraph z-axis in Vispy

我正在对极高密度激光雷达点云(100 万个点以上)进行博士研究,并且在 3D 散点图上绘制这些文件时遇到了真正的困难。 Matplotlib 并未针对如此大的数据集进行优化,因此我尝试使用 Vispy 来实现这一点。现在,我在尝试绘制散点图样式图和 运行 时遇到了真正的麻烦,因此我可以开始构建我的管道。

我正在寻找这样的东西: matplotlib 3D scattergraph

使用 Vispy,我发现很难应用 z 轴,而且我找不到文档的哪一部分可以帮助我解决这个问题。这是我的代码:

"""
This example demonstrates the use of the SurfacePlot visual.
"""

import sys
import numpy as np

from vispy import app, scene
from vispy.util.filter import gaussian_filter


canvas = scene.SceneCanvas(keys='interactive', bgcolor='w')
view = canvas.central_widget.add_view()
view.camera = scene.TurntableCamera(up='z', fov=60)

# Simple surface plot example
# x, y values are not specified, so assumed to be 0:50
z = np.random.normal(size=(250, 250), scale=200)
z[100, 100] += 50000
z = gaussian_filter(z, (10, 10))
print("This is z {0}".format(z))
p1 = scene.visuals.SurfacePlot(z=z, color=(0.3, 0.3, 1, 1))
p1.transform = scene.transforms.MatrixTransform()
p1.transform.scale([1/249., 1/249., 1/249.])
p1.transform.translate([-0.5, -0.5, 0])

view.add(p1)

# p1._update_data()  # cheating.
# cf = scene.filters.ZColormapFilter('fire', zrange=(z.max(), z.min()))
# p1.attach(cf)


xax = scene.Axis(pos=[[-0.5, -0.5], [0.5, -0.5]], tick_direction=(0, -1),
                 font_size=16, axis_color='k', tick_color='k', text_color='k',
                 parent=view.scene)
xax.transform = scene.STTransform(translate=(0, 0, -0.2))

yax = scene.Axis(pos=[[-0.5, -0.5], [-0.5, 0.5]], tick_direction=(-1, 0),
                 font_size=16, axis_color='k', tick_color='k', text_color='k',
                 parent=view.scene)
yax.transform = scene.STTransform(translate=(0, 0, -0.2))

zax = scene.Axis(pos=[[1.0, 1.0], [-1.0, 1.0]], tick_direction=(-1, 0),
                 font_size=16, axis_color='k', tick_color='k', text_color='k',
                 parent=view.scene)
zax.transform = scene.STTransform(translate=(0.0, 0.0, -0.2))


# Add a 3D axis to keep us oriented
axis = scene.visuals.XYZAxis(parent=view.scene)

if __name__ == '__main__':
    canvas.show()
    if sys.flags.interactive == 0:
        app.run()

它制作的图表如下所示:

vispy_example_plot

你可以在上图中看到我的问题。我想将第三个轴垂直于其他轴,并在绘图的墙壁上放置一个网格,以便数据更加明确。

不幸的是,子采样和其他使用 matplotlib 显示数据的技巧对我来说不是最佳选择,因为我必须显示原始数据,而不是显示子集。当然,如果我可以使用其他策略来显示我的数据,那么我会洗耳恭听。

在此先感谢,希望有人能提供帮助

我在研究相机深度图像和 cfar 雷达数据时遇到了同样的问题。由于 vispy 仅支持轴的 2d,因此您可以在创建后旋转 z 轴。在这里,您的代码稍作修改:

import sys
import numpy as np
from vispy import app, scene
from vispy.util.filter import gaussian_filter

canvas = scene.SceneCanvas(keys='interactive', bgcolor='w')
view = canvas.central_widget.add_view()
view.camera = scene.TurntableCamera(up='z', fov=60)

z = np.random.normal(size=(250, 250), scale=200)
z[100, 100] += 50000
z = gaussian_filter(z, (10, 10))

p1 = scene.visuals.SurfacePlot(z=z, color=(0.3, 0.3, 1, 1))
p1.transform = scene.transforms.MatrixTransform()
p1.transform.scale([1/249., 1/249., 1/249.])
view.add(p1)

xax = scene.Axis(pos=[[0, 0], [1, 0]], tick_direction=(0, -1), axis_color='r', tick_color='r', text_color='r', font_size=16, parent=view.scene)
yax = scene.Axis(pos=[[0, 0], [0, 1]], tick_direction=(-1, 0), axis_color='g', tick_color='g', text_color='g', font_size=16, parent=view.scene)

zax = scene.Axis(pos=[[0, 0], [-1, 0]], tick_direction=(0, -1), axis_color='b', tick_color='b', text_color='b', font_size=16, parent=view.scene)
zax.transform = scene.transforms.MatrixTransform()  # its acutally an inverted xaxis
zax.transform.rotate(90, (0, 1, 0))  # rotate cw around yaxis
zax.transform.rotate(-45, (0, 0, 1))  # tick direction towards (-1,-1)

if __name__ == '__main__':
    canvas.show()
    if sys.flags.interactive == 0:
        app.run()

此致