Search code examples
pythonseabornline-plot

Seaborn lineplot: how to match error band edge linestyle to main line linestyle


I am using Seaborn to make lineplots with an error band indicating full distribution of measurements and making use of the Style grouping variable. As the plotted groups are overlapping, it is not fully clear to which main line the error bands belong

see plot here

I would like to make this more clear by matching the linestyle of the error bands edges to the linestyle of the main line, but cannot find how to fix this.

The plot demonstrating grouping variables (see plot here) on Seaborns Lineplot instructional page illustrates the problem as well. By entering the two different linestyles keywords err_kws={'linestyle': [(0,(1,1)),'solid']} congruent to the grouping variable specific main line linestyle definition dashes={'cue':(), 'stim':(1,1)} (in codeblock below) I tried to adjust the error band edge linestyle, but the first listed linestyle adjustment is then applied to all error band edges, while the second listed error band linestyle specification remains unused.

fmri = sns.load_dataset("fmri")
sns.relplot(data=fmri, x="timepoint", y="signal", size="region", style="event",
         dashes={'cue':(), 'stim':(1,1)}, err_kws={'linestyle': [(0,(1,1)),'solid']},
         kind="line")

I would like to have the groups with solid main line with solid error band edge (see plot here), while the groups with dotted main line should have a dotted error band edge (see plot here).

I also tried to call the main line style variable kws["dashes"], that is defined in Seaborns code in for Relational Plots (relational.py, line 513-518), by passing err_kws={'linestyle': 'kws["dashes"]', but this did not work either.

if "style" in sub_vars:
            attributes = self._style_map(sub_vars["style"])
            if "dashes" in attributes:
                kws["dashes"] = attributes["dashes"]
            if "marker" in attributes:
                kws["marker"] = attributes["marker"]

Is it possible to base the error band linestyle on the main line linestyle in a Seaborn lineplot?


Solution

  • I finally manage to plot how I want the figures to be with the code

    ax = plt.gca()
    for poly, line in zip(ax.collections, ax.lines):
        plt.setp(
            poly,
            linewidth=line._linewidth,
            linestyle=(
                0.0,
                (
                    None
                    if line._dashSeq is None
                    else (np.array(line._dashSeq) / (line._linewidth)).tolist()
                ),
            ),
        )
    

    Here is the final plot

    Here is the final plot