Skip to content

Patches eagerly resolve colors and apply alpha, but should do so lazily like Line2Ds #11710

Open

Description

@anntzer

Consider the following example:

from matplotlib.pyplot import Line2D, Rectangle

l = Line2D([], [], alpha=.5)
l.set_color("red")
print(l.get_color())

r = Rectangle((0, 0), 1, 1, alpha=.5)
r.set_facecolor("red")
print(r.get_facecolor())

The line reports its color as "red", but the rectangle reports it as (1, 0, 0, .5). This is because patches immediately resolve colors and apply alphas; to do so they need to maintain an elaborate dance involving _original_facecolor and _original_edgecolor. Conversely, Line2D doesn't resolve colors eagerly (they just store whatever was passed in), and perform the conversion in draw().

As an other example, consider

from pylab import *
from matplotlib.pyplot import Line2D, Rectangle

ax = gca()
ax.add_artist(Line2D([0, 1], [.5, 1], color="C0"))
ax.add_artist(Rectangle((0, 0), 1, .5, color="C0"))
rcParams["axes.prop_cycle"] = cycler(color=["r", "g", "b"])

show()

This will draw a red line (draw-time resolution is affected by the new prop_cycle), but a blue rectangle (eager resolution uses the original prop_cycle).

I think the behavior of Line2D makes more sense (at least the output of get_facecolor() immediately after a set_facecolor() should be what we set...), this would also avoid issues such as #11702/#11703.

(mpl master and probably since a long time ago)

Activity

  1. added
    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …
    on Jul 25, 2018
  2. github-actions commented on May 9, 2023

    @github-actions

    This issue has been marked "inactive" because it has been 365 days since the last comment. If this issue is still present in recent Matplotlib releases, or the feature request is still wanted, please leave a comment and this label will be removed. If there are no updates in another 30 days, this issue will be automatically closed, but you are free to re-open or create a new issue if needed. We value issue reports, and this procedure is meant to help us resurface and prioritize issues that have not been addressed yet, not make them disappear. Thanks for your help!

  3. added
    keepItems to be ignored by the “Stale” Github Action
    and removed
    status: inactiveMarked by the “Stale” Github Action
    on May 9, 2023
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …keepItems to be ignored by the “Stale” Github Action

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions