Skip to content

[ENH]: contour kwarg for negative_linestyle #23028

Closed

Description

@jklymak

Problem

if you contour a negative quantity, it gets dashed lines. Leaving aside whether this is a good default or not, the only way to toggle this is via rcParams['contour.negative_linestyle']=False.

Proposed solution

I think this should be togglable via kwarg, though I appreciate that overlaps with linestyle and only is activated with monochrome contours.

(I actually think the default should be False, FWIW - this surprises me every time, and I make quite a few contour plots).

Activity

  1. added
    API: consistencyConsistency of the matplotlib API, including naming, behavior, defaults, …
    and removed on May 9, 2022
  2. andrew-fennell commented on May 13, 2022

    @andrew-fennell
    Contributor

    Should the current linestyles kwarg be used to accomplish this or should a new kwarg be added? I have a simple solution adding a new kwarg (though it would need a little more work before it is ready).

    The following code snippet and images will show a solution with an added kwarg, but I expect this is not exactly what you had in mind.

    import numpy as np
    import matplotlib.pyplot as plt
    
    delta = 0.025
    x = np.arange(-3.0, 3.0, delta)
    y = np.arange(-2.0, 2.0, delta)
    X, Y = np.meshgrid(x, y)
    Z1 = np.exp(-X**2 - Y**2)
    Z2 = np.exp(-(X - 1)**2 - (Y - 1)**2)
    Z = (Z1 - Z2) * 2
    
    # Negative contour defaults to dashed
    fig, ax = plt.subplots()
    CS = ax.contour(X, Y, Z, 6, colors='k')
    ax.clabel(CS, fontsize=9, inline=True)
    ax.set_title('Single color - negative contours dashed (default)')
    
    # Set negative contours to be solid instead of dashed (default)
    # using negative_linestyle='solid'
    fig2, ax2 = plt.subplots()
    CS = ax2.contour(X, Y, Z, 6, colors='k', negative_linestyles='solid')
    ax2.clabel(CS, fontsize=9, inline=True)
    ax2.set_title('Single color - negative contours solid')
    

    Default

    image

    negative_linestyles='solid'

    image

  3. timhoffm commented on May 13, 2022

    @timhoffm
    Member

    Should the current linestyles kwarg be used to accomplish this or should a new kwarg be added?

    How would we squeeze this into the current linestyles parameter? The only thing I could imagine is something like linestyles={'positive': 'solid', 'negative': 'dotted'}, which I find a bit cumbersome. I suppose an extra kwarg is simpler.

  4. andrew-fennell commented on May 13, 2022

    @andrew-fennell
    Contributor

    How would we squeeze this into the current linestyles parameter?

    I had no good solution for this either, but thought I would ask just in case.

    The next question is:

    1. should this be a boolean (the rcParams method seems to be)
    2. or should the kwarg accept a separate entry for negative linestyles?

    I think the latter makes a lot more sense, but I don't work with contour plots regularly.

  5. jklymak commented on May 13, 2022

    @jklymak
    MemberAuthor

    I think since the rcParam is separate, its fine to keep the (potential) kwarg separate. BTW I'm not wedded to this idea, but it does seem strange to have a feature only accessible by rcParam.

    As for bool vs string, I'd say both? right now the rcParam defaults to "dashed" - I think that's fine for a kwarg as well, but being able to say False to skip the behaviour would be nice as well.

  6. andrew-fennell commented on May 13, 2022

    @andrew-fennell
    Contributor

    BTW I'm not wedded to this idea

    I can mock-up a basic solution. Maybe we can get opinions from other contributors and maintainers in the meantime.

    I think this would add value because it would make customization of negative contours more straightforward. Since this doesn't require removing any current functionality, I don't see any cons to adding the kwarg.

  7. added this to the v3.6.0 milestone on Jun 25, 2022
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, …API: default changesChanges to default behavior

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions