【问题标题】:Combining scatter plot with surface plot结合散点图和曲面图
【发布时间】:2013-02-20 05:07:57
【问题描述】:

如何将 3D 散点图与 3D 曲面图结合起来,同时保持曲面图透明,以便我仍然可以看到所有点?

【问题讨论】:

    标签: python matplotlib scatter geometry-surface


    【解决方案1】:

    要在同一张图中组合各种类型的图,您应该使用该函数

    plt.hold(真)。

    以下代码用 3D 曲面图绘制 3D 散点图:

    from mpl_toolkits.mplot3d import *
    import matplotlib.pyplot as plt
    import numpy as np
    from random import random, seed
    from matplotlib import cm
    
    
    fig = plt.figure()
    ax = fig.gca(projection='3d')               # to work in 3d
    plt.hold(True)
    
    x_surf=np.arange(0, 1, 0.01)                # generate a mesh
    y_surf=np.arange(0, 1, 0.01)
    x_surf, y_surf = np.meshgrid(x_surf, y_surf)
    z_surf = np.sqrt(x_surf+y_surf)             # ex. function, which depends on x and y
    ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot);    # plot a 3d surface plot
    
    n = 100
    seed(0)                                     # seed let us to have a reproducible set of random numbers
    x=[random() for i in range(n)]              # generate n random points
    y=[random() for i in range(n)]
    z=[random() for i in range(n)]
    ax.scatter(x, y, z);                        # plot a 3d scatter plot
    
    ax.set_xlabel('x label')
    ax.set_ylabel('y label')
    ax.set_zlabel('z label')
    
    plt.show()
    

    结果:

    您可以在此处查看其他带有 3d 绘图的示例:
    http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html

    我已将曲面图的颜色从默认更改为颜色图“热”,以便区分两个图的颜色 - 现在,可以看到曲面图独立地覆盖了散点图顺序...

    编辑:要解决这个问题,应该在曲面图的颜色图中使用透明度;将代码添加到: Transparent colormap 并换行:

    ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot);    # plot a 3d surface plot
    

    ax.plot_surface(x_surf, y_surf, z_surf, cmap=theCM);
    

    我们得到:

    【讨论】:

    • 解决此问题的方法是(手动)将散点图中的数据分成两半(平面上方的数据和下方的数据),然后以正确的顺序绘制这三件事.见stackoverflow.com/questions/14824893/…
    • 如果您需要“真正的”3D 渲染,请从 enthought 中查看 mayavi。 code.enthought.com/projects/mayavi
    • @tcaswell 我注意到即使改变绘图的顺序,表面总是出现在顶部,这很奇怪......(zorder 也不起作用)这就是为什么我选择使用透明胶片。
    • hold 已被弃用,而 hold(True) 是 matplotlib 2.2 中的默认行为 matplotlib.org/api/api_changes.html 鉴于此不知道如何更新答案..
    • 看来zorder 参数仍然适用于 3d 线。这有点混乱,但我通过迭代点并将它们单独绘制为从点到自身的线,在曲面图的顶部绘制了散点图。
    【解决方案2】:

    使用siluaty的例子;您可以调整 alpha 值,而不是通过 cmap=theCM 命令使用透明度。这可能会得到你想要的?

    ax.plot_surface(x_surf, y_surf, z_surf, cmap=cm.hot, alpha=0.2)
    

    【讨论】:

    • 我想知道是否有类似 alpha 的东西,你的评论很明显!
    • 有谁知道如何在不设置低 alpha 的情况下使散点变得明亮可见?
    • post 由 matplotlib 开发人员编写:这是 mplot3d 的已知限制。它不是一个真正的 3d 渲染工具包,它受到 matplotlib 分层引擎的约束。每个艺术家都有一个 zorder 值,它决定了艺术家和收藏品的绘制顺序。因此,在一般情况下,混合 3d 场景的渲染是不可能的,因为表面艺术家或散射艺术家必须在另一个之前绘制。
    猜你喜欢
    • 2016-07-03
    • 1970-01-01
    • 2020-07-15
    • 1970-01-01
    • 2018-02-08
    • 1970-01-01
    • 2022-01-27
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多