Reed Richards
Reed Richards

Reputation: 4378

How to plot Pandas multilabel data scatterplots in Seaborn using facetgrid?

I have a dataset which have multiple labels. I want to create a Facetgrid of scatterplots using Pandas and Seaborn. In addition this dataset has different origin which I also want to compare. (For example, this could be the prediction of different ML algorithms and the true labeling in two plots side by side). The plot I want is something like this with room for more methods and labelings:

enter image description here

Here the right column is the first labeling and the left is the second.

Currently my Pandas dataframe looks like this: l1, l2, method, x, y. Where l1 and l2 are the different labelings. I can plot one column using the following code:

g = sns.FacetGrid(df, row='method', hue='l1')
g.map(plt.scatter, 'x', 'y')
sns.plt.show()

But how do I get the second column? The intuitive thing would be to have the hue parameter be a list but that doesn't work.

Upvotes: 0

Views: 2008

Answers (1)

jack6e
jack6e

Reputation: 1522

I think you need to modify your dataframe:

l1_df = df[['l1','method','x','y']]
l1_df['label_type'] = 'l1'
l1_df.rename(columns={'l1':'label'}, inplace=True)

l2_df = df[['l2','method','x','y']]
l2_df['label_type'] = 'l2'
l2_df.rename(columns={'l2':'label'}, inplace=True)

df = pd.concat([l1_df,l2_df])

Then use the truly intuitive option, col, from the docs:

g = sns.FacetGrid(df, row='method', col='label_type', hue='label')
g.map(plt.scatter, 'x', 'y')
sns.plt.show()

Upvotes: 1

Related Questions