'matplotlib 3D scatter plot alpha varies when viewing different angles

When creating 3D scatter plots with matplotlib I noticed that when the alpha (transparency) of the points is varied it will draw them differently depending on how you rotate the view. The example images below are the same plot rotated slightly, which causes the alpha values to mysteriously reverse. Is anyone familiar with this behavior and how to address it? It looks like the 'zorder' (draw order) is a single value for the entire scatter plot call.

enter image description hereenter image description here

Simplified example code to recreate:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

X = [i for i in range(10)]
Y = [i for i in range(10)]
Z = [i for i in range(10)]
S = [(i+1)*400 for i in range(10)]
A = [i/10 for i in range(10)]

ax.scatter(xs=X, ys=Y, zs=Z, s=S, alpha=A)

plt.show()
  • Python 3.9.5
  • matplotlib 3.5.1


Solution 1:[1]

One possible workaround is to add each point with a for loop, for example:

import matplotlib.pyplot as plt

fig = plt.figure()
ax = fig.add_subplot(projection="3d")

X = [i for i in range(10)]
Y = [i for i in range(10)]
Z = [i for i in range(10)]
S = [(i+1)*400 for i in range(10)]
A = [i/10 for i in range(10)]

for x, y, z, s, a in zip(X, Y, Z, S, A):
    ax.scatter(xs=x, ys=y, zs=z, s=s, alpha=a, color="tab:blue")

plt.show()

Solution 2:[2]

I also posted this on the matplotlib github to see what the developers think.

It appears to be a bug with a very low priority, per their latest response: "We have limited core developer resources and mpl_toolkits/mplot3d is only a secondary priority for the core team. Likely this needs someone in the community to pick up and provide a fix."

I will try to do so, but am posting this for visibility in the meantime so that other people with more experience are made aware.

Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source
Solution 1 Davide_sd
Solution 2 Mandias