Lots of 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() 

make the plot of the sea plot as follows: Example facet plot

Now I would like to add another line to this graph with another variable, name it Y2 on the y axis. The result should look similar to the vertical stacking of two graphs obtained using

 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 one graph without a duplicated x axis and headers ("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 achieve this, because it leads to the fact that the curve for Y1 and Y2 is displayed in the same subtask for each value of A.

+8
python matplotlib pandas plot seaborn
source share
1 answer

I used the following code to create a synthetic dataset that seems to match yours:

 import pandas import numpy import seaborn as sns import matplotlib.pyplot as plt # Generate synthetic data omega = numpy.linspace(0, 50) A0s = [1., 18., 40., 100.] dfs = [] for A0 in A0s: V_w_dr = numpy.sin(A0*omega) V_w_tr = numpy.cos(A0*omega) dfs.append(pandas.DataFrame({'omega': omega, 'V_w_dr': V_w_dr, 'V_w_tr': V_w_tr, 'A0': A0})) dataframe = pandas.concat(dfs, axis=0) 

Then you can do what you want (thanks @mwaskom in the comments for )sharey='row', margin_titles=True ):

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

The result is

The result of the construction of fused data

+1
source share

All Articles