coderunner
coderunner

Reputation: 599

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!

Upvotes: 20

Views: 12206

Answers (3)

HYRY
HYRY

Reputation: 97331

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

Upvotes: 17

fraxel
fraxel

Reputation: 35319

ax.scatter(x, y, z, depthshade=0)

Upvotes: 13

dkissick
dkissick

Reputation: 113

If you only want to disable the alpha adjustment, you can overwrite the zalpha function. 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()

Upvotes: 6

Related Questions