Skip to content

[Bug]: Resolution of Collection with unfilled markers edgecolors depends on whether colors are passed in constructor or later #26904

Open

Description

@anntzer

Bug summary

In the example below, I construct two collections of unfilled markers via scatter(..., marker="x"). In one case I set the marker colors in the constructor, in the other case I set it immediately after via an explicit setter call.
I then set the collection's facecolor (it doesn't matter here for unfilled markers, but one can imagine this is generic code where the marker used actually comes from elsewhere and I just have marker=user_specified and here the user just happened to be "x".
I then probe the collection's edgecolor (which is how the cross will actually be drawn). In the first case, the edgecolor actually switched to blue, i.e. it followed the facecolor. In the second case it stays (red, green), i.e. it followed the set_color call and ignored the later set_facecolor.

Code for reproduction

from pylab import *

coll = plt.scatter([0, 1], [0, 1], marker="x",
                   color=["r", "g"])
coll.set_facecolor("b")
print(coll.get_edgecolor())

coll = plt.scatter([0, 1], [0, 1], marker="x")
coll.set_color(["r", "g"])
coll.set_facecolor("b")
print(coll.get_edgecolor())

Actual outcome

[[0. 0. 1. 1.]]
[[1. 0. 0. 1. ]
[0. 0.5 0. 1. ]]

Expected outcome

Same output for both collections.

Additional information

No response

Operating system

macos

Matplotlib Version

3.8

Matplotlib Backend

agg

Python version

3.11

Jupyter version

ENOSUCHLIB

Installation

git checkout

Activity

  1. QuLogic commented on Sep 25, 2023

    @QuLogic
    Member

    I assume this has something to do with property resolution that @efiring refactored some time ago.

  2. efiring commented on Oct 6, 2023

    @efiring
    Member

    The basic problem is that scatter has a special behavior for unfilled markers: they are treated as if they had only a face, not an edge, even though in fact it is the other way around. From the docstring (and #17543):

                For non-filled markers, *edgecolors* is ignored. Instead, the color
                is determined like with 'face', i.e. from *c*, *colors*, or
                *facecolors*.
    

    But scatter then returns a generic PathCollection, initialized with the marker's path, that doesn't know about this behavior, so it has no way to maintain it when color attributes are subsequently changed.

    I think the solution might be to make a MarkerCollection subclass of PathCollection. Some of the color manipulation logic would then be moved from the Axes.scatter() method to the new class.

    Alternatively, just accept and document the current behavioral inconsistency.

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

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions