简体   繁体   English

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

[英]Controlling alpha value on 3D scatter plot using Python and matplotlib

I'm plotting a 3D scatter plot using the function scatter and mplot3d. 我正在使用函数scatter和mplot3d绘制三维散点图。 I'm choosing a single color for all points in the plot, but when drawn by matplotlib the transparency of the points is set relative to the distance from the camera. 我正在为绘图中的所有点选择单一颜色,但是当使用matplotlib绘制时,点的透明度相对于距相机的距离设置。 Is there any way to disable this feature? 有没有办法禁用此功能?

I've tried setting the alpha kwarg to None/1 and also set vmin/vmax to 1 (in an attempt to force the color scaling to be a solid single color) with no luck. 我已经尝试将alpha kwarg设置为None / 1并且还将vmin / vmax设置为1(试图强制颜色缩放为纯色单色)而没有运气。 I didn't see any other likely options related to this setting in the scatter documentation. 我没有在散点文档中看到与此设置相关的任何其他可能选项。

Thanks! 谢谢!

For Matplotlib 1.4+, the answer provided below by @fraxel is the best solution: call ax.scatter with the argument depthshade=False . 对于Matplotlib 1.4+,下面由@fraxel提供的答案是最佳解决方案:使用参数depthshade=False调用ax.scatter

There is no arguments that can control this. 没有可以控制这一点的论据。 Here is some hack method. 这是一些黑客方法。

Disable set_edgecolors and set_facecolors method, so that mplot3d can't update the alpha part of the colors: 禁用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()

在此输入图像描述

If you want call set_edgecolors and set_facecolors methods later, you can backup these two methods before disable them: 如果您希望稍后调用set_edgecolorsset_facecolors方法,则可以在禁用它们之前备份这两种方法:

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

If you only want to disable the alpha adjustment, you can overwrite the zalpha function. 如果您只想禁用alpha调整,则可以覆盖zalpha函数。 This will allow you to update the colors in the case of an interactive plot and still remove the depth fog. 这将允许您在交互式绘图的情况下更新颜色,并仍然删除深度雾。

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()
ax.scatter(x, y, z, depthshade=0)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM