hsvar
hsvar

Reputation: 197

Cannot change linestyle in Seaborn.FacetGrid

I'm trying to create a FacetGrid with full and dashed lines like in this comment. Based on the code in the comment and on the FacetGrid doc this should work, however, I only get full lines, no dashes.

Could someone please help me out?

Min. working example:

import matplotlib
import pandas as pd
import seaborn as sns

# toy data
x = [i for i in range(10)]*3
y = [0.5*i for i in range(10)]
y.extend([0.7*i for i in range(10)])
y.extend([0.3*i for i in range(10)])
mode = ["A" for i in range(10)]
mode.extend(["B" for i in range(10)])
mode.extend(["C" for i in range(10)])
method = ["X" for i in range(5)]
method.extend(["Y" for i in range(5)])
method = method*3
df = pd.DataFrame({'x' : x, 'y' : y, 'mode' : mode, 'method' : method})

sns.set_context("paper")
sns.set(style="whitegrid")
blue = matplotlib.colors.hex2color('#5862f4')
pink = matplotlib.colors.hex2color('#e059c3')


kw = {'color': [pink, pink, blue], 'linestyle' : ["-","--","-"]}
p = sns.FacetGrid(df, col='method', hue='mode', sharey='row', margin_titles=True, hue_kws=kw)
p.map(sns.lineplot, 'x', 'y')
p.axes[0,0].set_xlim(0,10)
p.add_legend()

plt.savefig("test.png", bbox_inches='tight')

Result

Upvotes: 3

Views: 4679

Answers (1)

ImportanceOfBeingErnest
ImportanceOfBeingErnest

Reputation: 339330

Seaborn lineplot overwrites the linestyle such as to use it with its style parameter. Here it seems you do not want to use the style. But also, there seems no reason to use lineplot at all. Hence a normal plt.plot() will work just fine.

kw = {'color': [pink, pink, blue], 'linestyle' : ["-","--","-"]}
g = sns.FacetGrid(df, col='method', hue='mode', sharey='row', margin_titles=True, hue_kws=kw)
g.map(plt.plot, 'x', 'y')

enter image description here

For completeness, here is how one would use the style argument for lineplot with a FacetGrid.

g = sns.FacetGrid(df, col='method', sharey='row', margin_titles=True)
g.map_dataframe(sns.lineplot, 'x', 'y', style="mode", style_order=list("ABC"))

enter image description here

Note that in order to guarantee the consistent mapping of the items of the "mode" column to styles, the style order needs to be set.

Upvotes: 8

Related Questions