Two lines from different dataframes in seaborn's facetgrid

Viewed 269

I have two dataframes df1 and df2... I need to plot two lines coming from each dataframe in the same plot. I've been able to plot the one line, but I don't know how to plot the second line...I have:

g = sns.FacetGrid(df1, col="S")
g.map(sns.lineplot, "Q", "M")

How do I add a second line (which is on the same scale) but coming from df2, to g?

1 Answers

Assign a unique frame id field to each frame and concat them. Pass the frame id to hue parameter of FacetGrid.

Example

df1 = pd.DataFrame({'Q': np.arange(10),
                    'M': np.random.randn(10),
                    'S': np.random.choice([1, 2], 10)})

df2 = pd.DataFrame({'Q': np.arange(10),
                    'M': np.random.randn(10),
                    'S': np.random.choice([1, 2], 10)})

data = pd.concat([df1.assign(frame='df1'),
                  df2.assign(frame='df2')])

g = sns.FacetGrid(data, col="S", hue='frame')
g.map(sns.lineplot, "Q", "M")
plt.legend()

[out]

enter image description here

Related