pythonseaborn

How to change the transparency of the confidence interval in a relplot?


I found an answer for regplots, but I can't get the same code to work for relplots. I want to change the transparency of the confidence intervals while keeping the lines of my graph darker, but the alpha input for relplots makes the entire graph more translucent.

My code:

cookie = sns.relplot(
    data=data, x="Day", y="Touches", 
    hue='Sex', ci=68, kind="line", col = 'Event'
    )

The line that works for regplots is

plt.setp(cookie.collections[1], alpha=0.4)

Solution

  • While, regplot returns one ax (subplot), relplot returns a complete grid of subplots (a FacetGrid). Often, the return value is grabbed into a variable named g (calling it cookie can make things very confusing when comparing with code from the documents).

    You can loop through the individual axes of the FacetGrid and make the change for each of them:

    import matplotlib.pyplot as plt
    import seaborn as sns
    
    fmri = sns.load_dataset('fmri')
    g = sns.relplot(data=fmri, x="timepoint", y="signal", col="region",
                    hue="event", style="event", kind="line")
    for ax in g.axes.flat:
        ax.collections[-1].set_alpha(0.4)
    
    plt.show()
    

    sns.relplot with changed transparency

    PS: As mentioned in the comments, if you want to change the alpha of all confidence intervals (instead of just one of them, as in the code of the question), you can use sns.relplot(..., err_kws={"alpha": .4}).