Skip to content

Implicit addition of "color" to property_cycle breaks semantics #6380

Closed

Description

@WeatherGod

In the 2.x branch, the recent addition of the CN feature also added an implicit assumption that "color" will always be in the property cycle. This conflicts with the feature of prop_cycle that it only advances for a plotting function if the user does not specify at least one of the properties made available by the property cycle.

This is important because several plotting functions will make multiple calls to plot()/fill() under the hood, but should have already advanced the cycle. Also, this mimics pre-1.5 behavior of the color_cycle where it did not advance if color was already specified.

The test (which is currently being modified to paper over the problem) which noticed this change is the following:

    fig, ax = plt.subplots()
    ax.set_prop_cycle('linewidth', [2, 4])
    for c in range(1, 4):
        ax.plot(np.arange(10), c * np.arange(10), lw=0.1)
    ax.plot(np.arange(10), 4 * np.arange(10))
    ax.plot(np.arange(10), 5 * np.arange(10))

In the v1.5.x branch, this would result in the cycler not being advanced for the first three lines, and then two lines are made with linewidths of 2 and 4 in that order. In 2.x branch, the cycler gets advanced because of the implicit inclusion of "color" to the property cycle. Therefore, the cycler has a property which is not specified by the user, and it thinks it needs to advance itself. This results in a plot that has three thin lines and then a line of width 4 and then 2 (because of the odd number of plot calls prior).

Now, consider the following scenario:

    fig, ax = plt.subplots()
    ax.set_prop_cycle('color', 'rb')
    for c in range(1, 4):
        ax.plot(np.arange(10), c * np.arange(10), color='k')
    ax.plot(np.arange(10), 4 * np.arange(10))
    ax.plot(np.arange(10), 5 * np.arange(10))

In this case, the cycler will not advance for the first three plots, but will for the last two -- as expected. This difference in behavior will cause confusion among users, and will be very difficult to explain.

I shall re-iterate my design concept behind the property-cycle feature that I added for v1.5: "Color is not special!". I am fine with having a failover mechanism for "C0" in the case that color isn't in the cycler, but to actually modify the cycler provided by the user only leads to confusion. What would we do when we want to expand the "CN" notation to other properties? Are we going to start implicitly include them into the property cycle as well?

Activity

  1. added this to the milestone on May 7, 2016
  2. tacaswell commented on May 23, 2016

    @tacaswell
    Member

    Trying to address this brought up an other corner case.

    In the test we do

    ax.set_prop_cycle(linewidth=[2, 3, 4, 5, 6], facecolor='bgcmy')

    so if we remove the implicit color plot falls back to C0 which in the color module looks at the prop_cycle in rcparams.

    I think this is just a wart of how this, as the C0 explicitly is a global thing. If you want to change the meaning you have to use a context manager.

    The other option is to start injecting cyclers into every call to to_rgba which seems less than good.

  3. efiring commented on May 23, 2016

    @efiring
    Member

    On 2016/05/22 5:37 PM, Thomas A Caswell wrote:

    so if we remove the implicit color |plot| falls back to |C0| which in
    the |color| module looks at the prop_cycle in |rcparams|.

    I'm not sure I see how this is a problem--do you mean it is because the
    user is expecting to see plot use the full cycle from rcparams, but
    instead it keeps repeating the C0 from rcparams? Or the user is
    expecting facecolor to override the missing color?

    I think this is just a wart of how this, as the |C0| explicitly is a
    global thing. If you want to change the meaning you have to use a
    context manager.

    The other option is to start injecting cyclers into every call to
    |to_rgba| which seems less than good.

    Downright unacceptable.

  4. tacaswell commented on May 23, 2016

    @tacaswell
    Member

    If the user sets an axes level prop cycle, the CN notation will still refer
    to the global prop cycle which could be confusing.

    We could also move some if the color conversion machinery to the axes
    objects to give them a chance to deal with the CN before it gets resolved
    globally.

    On Mon, May 23, 2016, 00:04 Eric Firing notifications@github.com wrote:

    On 2016/05/22 5:37 PM, Thomas A Caswell wrote:

    so if we remove the implicit color |plot| falls back to |C0| which in
    the |color| module looks at the prop_cycle in |rcparams|.

    I'm not sure I see how this is a problem--do you mean it is because the
    user is expecting to see plot use the full cycle from rcparams, but
    instead it keeps repeating the C0 from rcparams? Or the user is
    expecting facecolor to override the missing color?

    I think this is just a wart of how this, as the |C0| explicitly is a
    global thing. If you want to change the meaning you have to use a
    context manager.

    The other option is to start injecting cyclers into every call to
    |to_rgba| which seems less than good.

    Downright unacceptable.

    —
    You are receiving this because you commented.
    Reply to this email directly or view it on GitHub
    #6380 (comment)

  5. efiring commented on May 28, 2016

    @efiring
    Member

    Would it be possible to add a cycler kwarg to to_rgba and to_rgba_array? If None, the rcParams cycler would be used. Every call to one of these functions from a function with an Axes would substitute the cycler from that Axes.

  6. tacaswell commented on May 28, 2016

    @tacaswell
    Member

    That seems reasonable to me.

    attn @anntzer

  7. anntzer commented on May 29, 2016

    @anntzer
    Contributor

    Actually I have no idea of what's happening in the "interesting" block of to_rgba:

        if _is_nth_color(c):
            from matplotlib import rcParams
            from matplotlib.rcsetup import cycler
            prop_cycler = rcParams['axes.prop_cycle']
            if prop_cycler is None and 'axes.color_cycle' in rcParams:
                clist = rcParams['axes.color_cycle']
                prop_cycler = cycler('color', clist)
            colors = prop_cycler._transpose().get('color', 'k')
            c = colors[int(c[1]) % len(colors)]
    

    which I just copy-pasted from the previous implementation... so I'll let someone else take care of it.

  8. efiring commented on May 29, 2016

    @efiring
    Member

    @tacaswell, that block of code looks a little odd: if there is neither a 'axes.prop_cycle' nor a 'axes.color_cycle', prop_cycler will be None, and the call to ._transpose() will fail. Does this need to be trapped with a more informative exception? If rcsetup guarantees that this cannot occur, then a comment to that effect would be helpful. Actually, if that is the case, then the test for 'axes.color_cycle' in rcParams is not needed.

  9. tacaswell commented on May 29, 2016

    @tacaswell
    Member

    The rc validation means rcParams['axes.prop_cycle'] can never be None. Would we want to fall back to the color cycle if color is not in prop_cycle? I think not because that means the user changed the default prop_cycle and we should respect that.

  10. self-assigned this
    on May 29, 2016
  11. tacaswell commented on May 30, 2016

    @tacaswell
    Member

    closed by #6499

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

Labels

Type

No type

Projects

No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions