首页 文章

使用Python和matplotlib控制3D散点图上的alpha值

提问于
浏览
13

我正在使用函数scatter和mplot3d绘制三维散点图 . 我正在为绘图中的所有点选择单一颜色,但是当使用matplotlib绘制时,点的透明度相对于距相机的距离设置 . 有没有办法禁用此功能?

我已经尝试将alpha kwarg设置为None / 1并且还将vmin / vmax设置为1(试图强制颜色缩放为纯色单色)而没有运气 . 我没有在散点文档中看到与此设置相关的任何其他可能选项 .

谢谢!

3 回答

  • 6

    For Matplotlib 1.4+, the answer provided below by @fraxel is the best solution: call ax.scatter with the argument depthshade=False.

    没有可以控制这一点的论据 . 这是一些黑客方法 .

    禁用 set_edgecolorsset_facecolors 方法,以便mplot3d无法更新颜色的alpha部分:

    from mpl_toolkits.mplot3d import Axes3D
    import numpy as np
    import matplotlib.pyplot as plt
    
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    
    x = np.random.sample(20)
    y = np.random.sample(20)
    z = np.random.sample(20)
    s = ax.scatter(x, y, z, c="r")
    s.set_edgecolors = s.set_facecolors = lambda *args:None
    
    ax.legend()
    ax.set_xlim3d(0, 1)
    ax.set_ylim3d(0, 1)
    ax.set_zlim3d(0, 1)
    
    plt.show()
    

    enter image description here

    如果您希望稍后调用 set_edgecolorsset_facecolors 方法,则可以在禁用它们之前备份这两种方法:

    s._set_facecolors, s._set_edgecolors = s.set_facecolors, s.set_edgecolors
    
  • 12

    如果您只想禁用alpha调整,则可以覆盖zalpha函数 . 这将允许您在交互式绘图的情况下更新颜色,并仍然删除深度雾 .

    from mpl_toolkits.mplot3d import *
    import numpy as np
    import matplotlib.pyplot as plt
    plt.ion()
    
    art3d.zalpha = lambda *args:args[0]
    
    fig = plt.figure()
    ax = fig.gca(projection='3d')
    
    x = np.random.sample(20)
    y = np.random.sample(20)
    z = np.random.sample(20)
    s = ax.scatter(x, y, z, c="r")
    
    ax.legend()
    ax.set_xlim3d(0, 1)
    ax.set_ylim3d(0, 1)
    ax.set_zlim3d(0, 1)
    
    plt.show()
    
  • 6
    ax.scatter(x, y, z, depthshade=0)
    

相关问题