Skip to content
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

[Bug]: PathCollection using alpha ignores 'none' facecolors #27839

Closed
subsurfaceiodev opened this issue Feb 29, 2024 · 1 comment · Fixed by #27845
Closed

[Bug]: PathCollection using alpha ignores 'none' facecolors #27839

subsurfaceiodev opened this issue Feb 29, 2024 · 1 comment · Fixed by #27845

Comments

@subsurfaceiodev
Copy link

Bug summary

As title, using alpha parameter in PathCollection, either as a list or as a scalar value, causes 'none' facecolors to be ignored / filled

Code for reproduction

import matplotlib.pyplot as plt
from matplotlib.collections import PathCollection
from matplotlib.path import Path

# https://matplotlib.org/stable/gallery/shapes_and_collections/compound_path.html

paths = [
    Path(
        [(1, 1), (1, 2), (2, 2), (2, 1), (0, 0)],
        [Path.MOVETO] + [Path.LINETO] * 3 + [Path.CLOSEPOLY]
    ),
    Path(
        [(4, 4), (5, 5), (5, 4), (0, 0)],
        [Path.MOVETO] + [Path.LINETO] * 2 + [Path.CLOSEPOLY]
    ),
]

fig, ax = plt.subplots()
collection = PathCollection(
    paths,
    edgecolors='black',
    facecolors=['blue', 'none'],
    alpha=[1, 1],
    # alpha=1
)
collection.set_transform(ax.transData)
ax.add_artist(collection)
ax.set(xlim=(0, 5), ylim=(0, 5))
plt.show()

Actual outcome

actual

Expected outcome

expected

Additional information

No response

Operating system

Windows

Matplotlib Version

3.8.3

Matplotlib Backend

No response

Python version

3.12

Jupyter version

No response

Installation

pip

@chaoyihu
Copy link
Contributor

chaoyihu commented Mar 3, 2024

When you set alpha = 1, collections::set_alpha() calls _set_facecolor(), which further calls to_rgba_array() in collections.py, Line 766: self._facecolors = mcolors.to_rgba_array(c, self._alpha) in which c is a list of strings: ["blue", "none"], and alpha is [1, 1].

Incolors::to_rgba_array(), Line 509 - 510, the alpha param is used to replace the default alpha values in the rgba sequence:

if alpha is not None:
    rgba[:, 3] = alpha

resulting in the following change:

facecolor rgba before applying alpha 
[[0. 0. 1. 1.]       # this is blue
 [0. 0. 0. 0.]]      # this has no color
facecolor rgba after applying alpha 
[[0. 0. 1. 1.]       # this is still blue
 [0. 0. 0. 1.]]      # this becomes black

To produce the expected outcome, try the following code:

import matplotlib.pyplot as plt
from matplotlib.collections import PathCollection
from matplotlib.path import Path

# https://matplotlib.org/stable/gallery/shapes_and_collections/compound_path.html

paths = [
    Path(
        [(1, 1), (1, 2), (2, 2), (2, 1), (0, 0)],
        [Path.MOVETO] + [Path.LINETO] * 3 + [Path.CLOSEPOLY]
    ),
    Path(
        [(4, 4), (5, 5), (5, 4), (0, 0)],
        [Path.MOVETO] + [Path.LINETO] * 2 + [Path.CLOSEPOLY]
    ),
]

fig, ax = plt.subplots()
collection = PathCollection(
    paths,
    edgecolors='black',
    facecolors=['blue', (0,0,0,0)]    # represent the desired face color using a explicit rgba tuple
)
collection.set_transform(ax.transData)
ax.add_artist(collection)
ax.set(xlim=(0, 5), ylim=(0, 5))
plt.show()

screenshot-matplotlib-issue-27839

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.

4 participants