Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Controlling alpha value on 3D scatter plot using Python and 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?

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.

Thanks!

like image 527
coderunner Avatar asked Mar 20 '13 19:03

coderunner


People also ask

What is alpha value in matplotlib?

Matplotlib allows you to regulate the transparency of a graph plot using the alpha attribute. By default, alpha=1. If you would like to form the graph plot more transparent, then you'll make alpha but 1, such as 0.5 or 0.25.

Can matplotlib be used for 3D plotting?

3D plotting in Matplotlib starts by enabling the utility toolkit. We can enable this toolkit by importing the mplot3d library, which comes with your standard Matplotlib installation via pip. Just be sure that your Matplotlib version is over 1.0. Now that our axes are created we can start plotting in 3D.


2 Answers

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.

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

enter image description here

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
like image 180
HYRY Avatar answered Oct 14 '22 21:10

HYRY


ax.scatter(x, y, z, depthshade=0)
like image 24
fraxel Avatar answered Oct 14 '22 20:10

fraxel