Skip to content

Setting an alpha value to a Poly3DCollection #10237

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Closed
ImportanceOfBeingErnest opened this issue Jan 12, 2018 · 1 comment · Fixed by #13573
Closed

Setting an alpha value to a Poly3DCollection #10237

ImportanceOfBeingErnest opened this issue Jan 12, 2018 · 1 comment · Fixed by #13573
Milestone

Comments

@ImportanceOfBeingErnest
Copy link
Member

Trying to create a semitransparent Poly3DCollection fails as shown in the following.

from mpl_toolkits.mplot3d import Axes3D
from mpl_toolkits.mplot3d.art3d import Poly3DCollection
import matplotlib.pyplot as plt

fig = plt.figure()
ax = Axes3D(fig)
x = [0, 1, 1, 0]
y = [0, 0, 1, 1]
z = [0, 1, 0, 1]
verts = [list(zip(x, y, z))]

alpha=0.5
fc = "C0"

# This line fails to give a semitransparent artist
pc = Poly3DCollection(verts, alpha = alpha, facecolors=fc, linewidths=1)

ax.add_collection3d(pc)
plt.show()

It gives the following output, which still has an alpha of 1.

image

Non-Working options

Now one may try several things. The following is not working:

  • Setting facecolor and alpha individually, facecolor first,

      pc = Poly3DCollection(verts, linewidths=1)
      pc.set_facecolor(fc) 
      pc.set_alpha(alpha)
    
  • Setting facecolors and alpha at instantiation

      pc = Poly3DCollection(verts, alpha = alpha, facecolors=fc, linewidths=1)
    

Working options

However, each of the following is correctly giving a semitransparent artist. This would be the expected outcome for any of the above as well.

  • Setting facecolor and alpha individually, alpha first,

      pc = Poly3DCollection(verts, linewidths=1)
      pc.set_alpha(alpha)   # Order reversed 
      pc.set_facecolor(fc)
    
  • Setting alpha at instantiation, facecolor afterwards

      pc = Poly3DCollection(verts, alpha = alpha, linewidths=1)
      pc.set_facecolor(fc)
    
  • Setting facecolor and alpha at instantiation

      pc = Poly3DCollection(verts, alpha = alpha, facecolor=fc, linewidths=1)
    

image

It seems there had been a similar bug in previous versions, https://stackoverflow.com/questions/18897786/transparency-for-poly3dcollection-plot-in-matplotlib which was reported to be fixed in the meantime but might have been reintroduced in a newer version.

In the 2D case the following works as expected and produces a semtransparent artist.

from matplotlib.collections import PolyCollection
import matplotlib.pyplot as plt

fig, ax = plt.subplots()
x = [0, 1, 1, 0]
y = [0, 0, 1, 1]

verts = [list(zip(x, y))]

alpha=0.5
fc = "C0"

pc = PolyCollection(verts, alpha = alpha, facecolors=fc, linewidths=1)

ax.add_collection(pc)
ax.autoscale()
plt.show()

[All of the code in this issue has been tested with matplotlib 2.1]

DanielMatu added a commit to DanielMatu/matplotlib that referenced this issue Feb 16, 2018
in Poly3DCollection's set_alpha method, facecolor was getting updated when facecolors3d should have been updated instead.
JZCJackson added a commit to JZCJackson/matplotlib that referenced this issue Mar 10, 2018
@ImportanceOfBeingErnest
Copy link
Member Author

Just to get this straight: Are there now two PRs

both trying to fix this simultaneously?

@ImportanceOfBeingErnest ImportanceOfBeingErnest added this to the v3.0.3 milestone Dec 9, 2018
@tacaswell tacaswell modified the milestones: v3.0.3, v3.1.0 Feb 4, 2019
jklymak pushed a commit to jklymak/matplotlib that referenced this issue Mar 3, 2019
in Poly3DCollection's set_alpha method, facecolor was getting updated when facecolors3d should have been updated instead.
@jklymak jklymak mentioned this issue Mar 3, 2019
6 tasks
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Projects
None yet
Development

Successfully merging a pull request may close this issue.

3 participants