Search code examples
python-3.xcolorsseabornsubplotfacet-grid

Seaborn FacetGrid lineplot: set specific line color in single subplot


When building small-multiples lineplot charts (or any chart type) using FacetGrid in Seaborn, how do you manually override the line color for a single and specific subplot in order to, say, highlight something and contrast it to the other subplots?

In Matplotlib, this is a relatively simple task, but I cannot find a way to access a specific facet and then customize the styles within it, leaving all others unchanged..

The following code generates a 2x2 chart with uniform styling across all the subplots:

tips = sns.load_dataset("tips")
tips = sns.load_dataset("tips")
g = sns.FacetGrid(tips, row="sex", col="smoker", margin_titles=True)
g.map(sns.lineplot, "total_bill", 'tip')

....if my goal is to increase the linewidth and change the color in the subplot in the first position, while maintaining the other subplots as they are, how would I achieve that?


Solution

  • You will have to access one of the axes created by FacetGrid, then access the artists that you want to customize, and change its properties.

    tips = sns.load_dataset("tips")
    g = sns.FacetGrid(tips, row="sex", col="smoker", margin_titles=True)
    g.map(sns.lineplot, "total_bill", 'tip')
    
    # customize first subplot
    ax = g.facet_axis(0,0) # could also do ax=g.axes[0,0]
    l = ax.get_lines()[0] # get the relevant Line2D object (in this case there is only one, but there could be more if using hues)
    l.set_linewidth(3)
    l.set_color('C1')
    

    enter image description here