Skip to content

[Bug]: axes.prop_cycle behavior change #29915

Open

Description

@larsoner

PR #29808 (identified by git bisect) changed the behavior of:

$ python -c 'import matplotlib; print(matplotlib.rcParams.get("axes.prop_cycle").by_key()["color"])'
['#1f77b4', '#ff7f0e', '#2ca02c', '#d62728', '#9467bd', '#8c564b', '#e377c2', '#7f7f7f', '#bcbd22', '#17becf']

to now be a list of tuple of float rather than a list of str:

$ python -c 'import matplotlib; print(matplotlib.rcParams.get("axes.prop_cycle").by_key()["color"])'
[(0.12156862745098039, 0.4666666666666667, 0.7058823529411765), (1.0, 0.4980392156862745, 0.054901960784313725), (0.17254901960784313, 0.6274509803921569, 0.17254901960784313), (0.8392156862745098, 0.15294117647058825, 0.1568627450980392), (0.5803921568627451, 0.403921568627451, 0.7411764705882353), (0.5490196078431373, 0.33725490196078434, 0.29411764705882354), (0.8901960784313725, 0.4666666666666667, 0.7607843137254902), (0.4980392156862745, 0.4980392156862745, 0.4980392156862745), (0.7372549019607844, 0.7411764705882353, 0.13333333333333333), (0.09019607843137255, 0.7450980392156863, 0.8117647058823529)]

This is potentially problematic for end-users. MNE-Python we know the default prop_cycle (and everyone seems to use it!) so make use of the fact that we know #7f7f7f is in there, remove it, and create a new cycle. Our code now breaks with:

    self.color_list.remove("#7f7f7f")
ValueError: list.remove(x): x not in list

And yes we should make our check better (e.g., remove any color sufficiently close to the bad color, etc.) but it seems like a potential regression that this behavior has changed in any case. Feel free to close if not!

Activity

  1. timhoffm commented on Apr 14, 2025

    @timhoffm
    Member

    Relying on the hex notation is not safe. Note that users could have made their own color cycles and used the numerics (or more likely the named color "tab:grey" https://matplotlib.org/stable/gallery/color/color_cycle_default.html#sphx-glr-gallery-color-color-cycle-default-py).

    I'm inclined to close this as "won't fix" and suggest you use matplotlib.colors.to_hex() as a fix on your side. Or are there significant downstream issues for your users so that we need to consider deferring #20808. An alternative could also be to keep mpl.color_sequences["tab10"] as a list of the named colors and not already resolve them to RGB. This feels a bit nicer, but would be an equally strong breakage on the color_sequence side (though color_sequences are newer and probably less used compared to the default color cycle).

  2. rcomer commented on Apr 14, 2025

    @rcomer
    Member

    @timhoffm do we have a deprecation pathway for changing default rcParams?

  3. larsoner commented on Apr 14, 2025

    @larsoner
    ContributorAuthor

    Relying on the hex notation is not safe.

    Totally! I made a fix already so all good at the MNE-Python end, I'm more bringing this up at the matplotlib end for the change in behavior of matplotlib.rcParams.get("axes.prop_cycle").by_key()["color"], I was surprised it changed from list[str] to list[tuple[float, float float]]. So more along the lines of what @rcomer is saying ☝

  4. QuLogic commented on Apr 15, 2025

    @QuLogic
    Member

    suggest you use matplotlib.colors.to_hex() as a fix on your side.

    Or possibly matplotlib.colors.same_color.

  5. timhoffm commented on Apr 15, 2025

    @timhoffm
    Member

    do we have a deprecation pathway for changing default rcParams?

    No, unfortunately there is no reasonable migration path for changing a parameter, in particular not if the result is logically the same: We don't want to generally warn users when accessing the default property cycle and we can't tell whether the way the color is specified is relevant for them or not (it likely is not if they just feed the color back into matplotlib, but it can be if they operate on it explicitly as in this issue).

  6. timhoffm commented on Apr 15, 2025

    @timhoffm
    Member

    I was surprised it changed from list[str] to list[tuple[float, float float]].

    Technically it is list[matplotlib.typing.ColorType but that's not formalized anywhere (and can't be because it's specific to dict value for the key "color").

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

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions