Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot multiple DataFrame columns in Seaborn FacetGrid

I am using the following code

import seaborn as sns

g = sns.FacetGrid(dataframe, col='A', hue='A')
g.map(plt.plot, 'X', 'Y1')
plt.show()

to make a seaborn facet plot like this: Example facet plot

Now I would like to add another row to this plot with a different variable, call it Y2, on the y axis. The result should look similar to vertically stacking the two plots obtained by

g = sns.FacetGrid(dataframe, col='A', hue='A')
g.map(plt.plot, 'X', 'Y1')
plt.show()

g = sns.FacetGrid(dataframe, col='A', hue='A')
g.map(plt.plot, 'X', 'Y2')
plt.show()

Example plot with two rows

but in a single plot, without the duplicate x axis and titles ("A=<value>") and without creating a new FacetGrid object.

Note that

g = sns.FacetGrid(dataframe, col='A', hue='A')
g.map(plt.plot, 'X', 'Y1')
g.map(plt.plot, 'X', 'Y2')
plt.show()

does not achive this, because it results in both the curve for Y1 and Y2 being displayed in the same subplot for each value of A.

like image 800
arccos Avatar asked Jun 03 '15 14:06

arccos


People also ask

How do you plot multiple plots in Seaborn?

In Seaborn, we will plot multiple graphs in a single window in two ways. First with the help of Facetgrid() function and other by implicit with the help of matplotlib. data: Tidy dataframe where each column is a variable and each row is an observation.

What does FacetGrid do in Seaborn?

FacetGrid object takes a dataframe as input and the names of the variables that will form the row, column, or hue dimensions of the grid. The variables should be categorical and the data at each level of the variable will be used for a facet along that axis.

What is Factorplot in Seaborn?

Factor Plot is used to draw a different types of categorical plot . The default plot that is shown is a point plot, but we can plot other seaborn categorical plots by using of kind parameter, like box plots, violin plots, bar plots, or strip plots.


1 Answers

I used the following code to create a synthetic dataset which appears to match yours:

import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib.pyplot as plt

# Generate synthetic data
omega = np.linspace(0, 50)

A0s = [1., 18., 40., 100.]

dfs = []
for A0 in A0s:
    V_w_dr = np.sin(A0*omega)
    V_w_tr = np.cos(A0*omega)
    dfs.append(pd.DataFrame({'omega': omega,
                             'V_w_dr': V_w_dr,
                             'V_w_tr': V_w_tr,
                             'A0': A0}))
df = pd.concat(dfs, axis=0)

Then you can do what you want. Thanks to @mwaskom in the comments for sharey='row', and margin_titles=True:

dfm = df.melt(id_vars=['A0', 'omega'], value_vars=['V_w_dr', 'V_w_tr'])
g = sns.FacetGrid(dfm, col='A0', hue='A0', row='variable', sharey='row', margin_titles=True)
g.map(plt.plot, 'omega', 'value')

This results in

enter image description here

Update

  • As of this update, the correct method is to use seaborn.relplot, which plots a FacetGrid.
sns.relplot(data=dfm, x='omega', y='value', col='A0', hue='A0', row='variable', kind='line')

enter image description here

like image 69
chthonicdaemon Avatar answered Oct 15 '22 03:10

chthonicdaemon