udaykumar gajavalli
udaykumar gajavalli

Reputation: 106

Plotting two subplots in one figure

I have two PCA plots: one for training data and testing test. Using seaborn, I'd like to combine those two and plot like subplots.

sns.FacetGrid(finalDf_test, hue="L", height=6).map(plt.scatter, 'PC1_test', 'PC2_test').add_legend()
sns.FacetGrid(finalDf_train, hue="L", height=6).map(plt.scatter, 'PC1_train', 'PC2_train').add_legend()

Can someone help on that?

Upvotes: 0

Views: 1343

Answers (1)

JohanC
JohanC

Reputation: 80509

FacetGrid is a figure-level function that creates one or more subplots, depending on its col= and row= parameters. In this case, only one subplot is created.

As FacetGrid works on only one dataframe, you could concatenate your dataframes, introducing a new column to diferentiate test and train. Also, the "PC1" and "PC2" columns of both dataframes should get the same name.

An easier approach is to use matplotlib to create the figure and then call sns.scatterplot(...., ax=...) for each of the subplots. It would look like:

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

# create some dummy data
l = np.random.randint(0,2,500)
p1 = np.random.rand(500)*10
p2 = p1 + np.random.randn(500) + l
finalDf_test = pd.DataFrame({'PC1_test': p1[:100], 'PC2_test': p2[:100], 'L':l[:100] })
finalDf_train = pd.DataFrame({'PC1_train': p1[100:], 'PC2_train': p2[100:], 'L':l[100:] })

sns.set()
fig, (ax1, ax2) = plt.subplots(ncols=2, figsize=(12, 6), sharex=True, sharey=True)
sns.scatterplot(data=finalDf_test, x='PC1_test', y='PC2_test', hue='L', ax=ax1)
sns.scatterplot(data=finalDf_train, x='PC1_train', y='PC2_train', hue='L', ax=ax2)
plt.show()

scatterplots on matplotlib subplots

Concatenating the dataframes could look as follows:

sns.set()
finalDf_total = pd.concat({'test': finalDf_test.rename(columns={'PC1_test': 'PC1', 'PC2_test': 'PC2' }),
                          'train':finalDf_train.rename(columns={'PC1_train': 'PC1', 'PC2_train': 'PC2' })})
finalDf_total.index.rename(['origin', None], inplace=True) # rename the first index column to "origin"
finalDf_total.reset_index(level=0, inplace=True) # convert the first index to a regular column
sns.FacetGrid(finalDf_total, hue='L', height=6, col='origin').map(plt.scatter, 'PC1', 'PC2').add_legend()
plt.show()

facetgrid with combined dataframes

The same combined dataframe could also be used for example in lmplot:

sns.lmplot(data=finalDf_total, x='PC1', y='PC2', hue='L', height=6, col='origin')

Upvotes: 2

Related Questions