使用Python和Matplotlib控制3D散点图上的alpha值 [英] Controlling alpha value on 3D scatter plot using Python and matplotlib

查看:296
本文介绍了使用Python和Matplotlib控制3D散点图上的alpha值的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在使用scatter和mplot3d函数绘制3D散点图.我为绘图中的所有点都选择了一种颜色,但是当使用matplotlib进行绘制时,这些点的透明度是相对于距相机的距离而设置的.有什么办法可以禁用此功能?

I'm plotting a 3D scatter plot using the function scatter and 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. Is there any way to disable this feature?

我尝试将alpha kwarg设置为None/1,并且还将vmin/vmax设置为1(以试图将颜色缩放比例调整为纯色),但是没有运气.在分散文档中,我没有看到与此设置相关的其他可能选项.

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. I didn't see any other likely options related to this setting in the scatter documentation.

谢谢!

推荐答案

对于Matplotlib 1.4 +,@ fraxel在下面提供的答案是最好的解决方案:用参数depthshade=False调用ax.scatter.

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

没有参数可以控制它.这是一些破解方法.

There is no arguments that can control this. Here is some hack method.

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

Disable set_edgecolors and set_facecolors method, so that mplot3d can't update the alpha part of the colors:

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()

如果要稍后调用set_edgecolorsset_facecolors方法,则可以在禁用这两个方法之前先备份这两个方法:

If you want call set_edgecolors and set_facecolors methods later, you can backup these two methods before disable them:

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

这篇关于使用Python和Matplotlib控制3D散点图上的alpha值的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆