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
Solution 1:
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()
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
Solution 2:
ax.scatter(x, y, z, depthshade=0)
Solution 3:
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()
Post a Comment for "Controlling Alpha Value On 3d Scatter Plot Using Python And Matplotlib"