Skip to content

Add the ability to plot striped lines #18351

Closed
@odysseus9672

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.

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