Skip to content

Add the ability to plot striped lines #18351

Closed

Description

@odysseus9672

Problem

Sometimes plots (especially histograms with narrow bins) have small features that are obscured or missed by ordinary dashed line plots.

Proposed Solution

Add the ability to specify an offcolor=(0.0, 0.0, 0.0, 0.0) kwarg that fills in the blanks when a dashed line style, or dash pattern, is used. In principle, even more colors could be specified, but a simple on-color/off-color would be sufficient and require the fewest changes to the API.
This can already be done by plotting multiple lines that have carefully matching dash patterns, but the issue is that the stripes aren't represented in plot legends.

Additional context and prior art

Right now I do this with this function (where the off-color is set to half the opacity of the oncolor):

def plotstripedline(ax, xvals, yvals, oncolor, dashpattern, lname):
    if len(oncolor) != 4:
        raise ValueError(
            "oncolor must have four floating point elements 0 to 1, rgba.")
    linestyle = (0, dashpattern)
    ax.plot(xvals, yvals, ls=linestyle, color=oncolor,
            label=lname)
    
    gapdp = list(dashpattern[1:])
    gapdp.append(dashpattern[0])
    gapstyle = (-dashpattern[0], gapdp)
    gapcol = [ x for x in oncolor ]
    gapcol[-1] *= 0.5
    ax.plot(xvals, yvals, ls=gapstyle, color=gapcol)
    
    return None

See, also, this discussion on StackExchange.

Activity

  1. diegopetrola commented on Aug 26, 2020

    @diegopetrola
    Contributor

    While this implementation is discussed, I believe you can make do with the following code:

    import numpy as np
    import matplotlib.pyplot as plt
    
    x = np.arange(0, 2*np.pi, 0.2)
    y = np.cos(x)
    
    blue_line, = plt.plot(x, y, linestyle='-', lw=3)
    dashed_line, = plt.plot(x, y, linestyle=(2, (2, 2)), lw=3)
    
    plt.legend([(blue_line, dashed_line)], ['data'])
    

    image

    I looked briefly at the tutorials and it seems the legends in matplotlib are very powerful! If you wish, you can check more interesting examples about legends here.

  2. jklymak commented on Aug 26, 2020

    @jklymak
    Member

    Someone would have to dig into linestyles to do this, and figure out a reasonable API for specifying the style. Other stroke styles have been discussed in the past, so there may be a desire to make a "LineStyle" container class for specifying. I'd say this is rather involved, but not an unreasonable request.

  3. added
    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issues
    on Aug 26, 2020
  4. added this to the milestone on Aug 26, 2020
  5. WeatherGod commented on Aug 26, 2020

    @WeatherGod
    Member
  6. tacaswell commented on Aug 26, 2020

    @tacaswell
    Member

    I agree with @jklymak 's assessment and @WeatherGod

    An immediate path would be to make a new artist that has as children the 2 lines in @diegopetrola 's comment.

  7. timhoffm commented on Aug 26, 2020

    @timhoffm
    Member

    Maybe it would make sense to borrow ideas from the filled two-color markers?

    API-wise an alt_color defaulting to transparent is enough.

  8. jklymak commented on Aug 26, 2020

    @jklymak
    Member

    This sort of is similar to #12980, in particular #12980 (comment) which talks about pen and stroke styles. I think it'd be a lot of work to do this, however.

    I guess alt_color is a possibility. It would be less flexible, but certainly a more tractable problem.

  9. timhoffm commented on Aug 26, 2020

    @timhoffm
    Member

    This sort of is similar to #12980, in particular #12980 (comment) which talks about pen and stroke styles. I think it'd be a lot of work to do this, however.

    AFAICS #12980 is quite different. It has a single shaped pen, whereas here, you change between two differently colored "simple" pens. I don't think one of the problems can be expressed as the other.

    But I agree that #12980 would be a lot of work, and alt_color is significantly simpler.

  10. WeatherGod commented on Aug 26, 2020

    @WeatherGod
    Member
  11. timhoffm commented on Aug 26, 2020

    @timhoffm
    Member

    for consistency, shouldn't the name be coloralt?

    Maybe. Even though I find that quite unreadable. Alternatively, we could find something new like gapcolor.

  12. lucielavigne commented on Oct 21, 2020

    @lucielavigne

    Hello,
    I am a student in software engineering and I work with a fellow student on a project. We would like to contribute on the implementation of this functionality

  13. timhoffm commented on Oct 21, 2020

    @timhoffm
    Member

    @lucielavigne You are welcome to!

    Technical remarks

    I think the new-artist-proposal is not feasible. This would get us in all sorts of synchronization trouble:

    • What if the user changes data -> would have to be synchronously propagated to both child artists.
    • What should we do if the user changes linestyle from/to solid?

    The correct solution is to dig into the low-level drawing and draw the spaces there as well.

    We will need a discussion at some point on proper naming of the parameter (gapcolor / coloralt / alt_color), but that should not stop you to start writing that functionality. - Renaming is easy.

    Organizational remarks

    tl;dr: Please reflect if you've feel largely capable of the task. No offence meant. I'll assume you do, but want to make sure that we don't get into troubles down the road.

    We've had mixed experiences with student projects. Some resulted in valuable contributions, some ended up abandoned after a lot of back and forth. To prevent troubles down the road, I'd like to set expectations:

    We welcome contributions from people with all experience levels. However, the difficulty of the task should match the experience. We'll happily give advice on specific aspects, but we're no teaching site (We don't have the capacity for requests "I'd like to work on this issue. Please walk me through step-by-step what I should do).
    This issue is marked as medium-difficult. You should be able to find the relevant code locations and propose reasonable changes yourself. If you need help with some details, don't hesitate do ask.

    CC @story645 as our community lead: Please check if wording and content matches our policy.

  14. story645 commented on Oct 22, 2020

    @story645
    Member

    Give or take - our code base is dense enough that I think it's okay to ask/confirm about relative code locations, but a lot of that thrashing will happen on the PR. @lucielavigne please don't take @timhoffm's response as dissuading you from contributing; if you're unsure about how much guidance you'll need, open a draft and we can take it from there. If you end up needing more help than we have the bandwidth for, we'll be at the PyData global sprints (which should be open to the public) https://global.pydata.org/talks/matplotlib

  15. lucielavigne commented on Nov 4, 2020

    @lucielavigne

    Hi,
    We are three students working on it, we have 6 weeks to work on it. We are willing to solve the issue.
    However the link you gave me is not working.
    We looked at the previous comments and the code, maybe you could give us some pointers to narrow our searchs.
    Thank you

  16. timhoffm commented on Nov 4, 2020

    @timhoffm
    Member

    I did not look closely at this, but I suppose you need to draw a second path with the inverted pattern at

    renderer.draw_path(gc, tpath, affine.frozen())
    .

  17. story645 commented on Nov 4, 2020

    @story645
    Member

    Sorry, the new link is https://global.pydata.org/talks/99, but there aren't details posted yet so check back at https://gitter.im/matplotlib/matplotlib

  18. lucielavigne commented on Dec 1, 2020

    @lucielavigne

    Hello,
    I submitted a pull request under add the ability to plot stripped lines.

  19. linked a pull request that will close this issueAdd the ability to plot striped lines #19053on Dec 1, 2020
  20. modified the milestones: , v3.6.0 on Jun 23, 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

    Difficulty: Mediumhttps://matplotlib.org/devdocs/devel/contribute.html#good-first-issuesNew feature

    Type

    No type

    Projects

    No projects

      Milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions