Skip to content

Setting an alpha value to a Poly3DCollection #10237

Closed

Description

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]

Activity

  1. ImportanceOfBeingErnest commented on Mar 11, 2018

    @ImportanceOfBeingErnest
    MemberAuthor

    Just to get this straight: Are there now two PRs

    both trying to fix this simultaneously?

  2. added this to the v3.0.3 milestone on Dec 9, 2018
  3. modified the milestones: v3.0.3, v3.1.0 on Feb 4, 2019
  4. added a commit that references this issue on Mar 3, 2019
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions