For my project, I use sns.FacetGrid()
to plot multiple subplots each containing multiple lines. My general goal is to draw a mean line for each line in each subplot. My idea was to extract the x- and y-data for each line. For this I iterate over each subplot and then over each line object in each subplot (as described in this stackoverflow post). The problem: Each of the subjects seems to contain four 'empty' line objects, but my subplots contain only three lines each. So my expected output is a list of six tuples, each containing an array for my x- and y-data. Does anyone know where these four empty line objects come from and how to get only the x- and y-data for each of the existing (aka. visible) lines?
Here's my code:
import numpy as np
import pandas as pd
import seaborn as sns
# simulate data frames #########################################################
n_outer_folds = 10
plot_df_1 = pd.DataFrame({'Outer Fold':np.linspace(start=1,stop=10,num=n_outer_folds),
'train_BAC':np.random.uniform(low=0.6,high=1.0,size=n_outer_folds).tolist(),
'train_SPEC':np.random.uniform(low=0.6,high=1.0,size=n_outer_folds).tolist(),
'test_BAC':np.random.uniform(low=0.1,high=0.8,size=n_outer_folds).tolist(),
'test_SPEC':np.random.uniform(low=0.1,high=0.8,size=n_outer_folds).tolist()
})
plot_df_2 = pd.DataFrame({'Outer Fold':np.linspace(start=1,stop=10,num=n_outer_folds),
'train_BAC':np.random.uniform(low=0.6,high=1.0,size=n_outer_folds).tolist(),
'train_SPEC':np.random.uniform(low=0.6,high=1.0,size=n_outer_folds).tolist(),
'test_BAC':np.random.uniform(low=0.1,high=0.8,size=n_outer_folds).tolist(),
'test_SPEC':np.random.uniform(low=0.1,high=0.8,size=n_outer_folds).tolist()
})
plot_df_list = [plot_df_1,plot_df_2]
# append 'Model' column to make each plot df identifiable
for idx,plot_df in enumerate(plot_df_list):
plot_df['Model'] = idx
# concatenate all plot dfs
plot_df = pd.concat(plot_df_list)
# create a plotable Dataframe
plot_df_melt = pd.melt(plot_df,
id_vars=['Outer Fold','Model'],
value_vars=['train_BAC','test_BAC','train_SPEC'],
var_name ='Scores',
value_name='Score'
)
# plot data
g = sns.FacetGrid(plot_df_melt,col="Model",height=4,aspect=2,col_wrap=1)
g.map(sns.lineplot,'Outer Fold','Score','Scores')
# get line data
axes_data = []
ax_lines_data = []
for ax in g.axes.flat:
axes_data.append(ax)
for line in ax.lines:
ax_lines_data.append((line.get_xdata(),line.get_ydata()))