BigBoy1337
BigBoy1337

Reputation: 4963

How to plot a FacetGrid scatter plot with multiple data frames?

I have 2 dataframes, 1 has training data and the other has labels. There are 6 features/columns in the training data and 1 column in the labels data frame. I want 6 plots in my facet grid - all of them to be a scatter plot. So feature 1 vs label, feature 2 vs label, feature 3 vs label, feature 4 vs label.

Can someone show me how to do this?

for instance, using these sample data frames

In [15]: training
Out[15]:
   feature1  feature2  feature3  feature4  feature5  feature6
0         2         3         4         5         2         5
1         5         4         2         5         6         2

In [16]: labels
Out[16]:
   label
0     34
1      2

This should make 6 separate scatter plots, each with 2 data points.

Upvotes: 2

Views: 4989

Answers (1)

Sam
Sam

Reputation: 4090

Seaborn has a nice FacetGrid function.You can merge your two dataframes wrap the seaborn facetgrid around a normal matplotlib.pyplot.scatter()

import pandas as pd
import random
import matplotlib.pyplot as plt
import seaborn as sns

#make a test dataframe
features = {}
for i in range(7):
    features['feature%s'%i] = [random.random() for j in range(10)]
f = pd.DataFrame(features)
labels = pd.DataFrame({'label':[random.random() for j in range(10)]})

#unstack it so feature labels are now in a single column
unstacked = pd.DataFrame(f.unstack()).reset_index()
unstacked.columns = ['feature', 'feature_index', 'feature_value']
#merge them together to get the label value for each feature value
plot_data = pd.merge(unstacked, labels, left_on = 'feature_index', right_index = True)
#wrap a seaborn facetgrid
kws = dict(s=50, linewidth=.5, edgecolor="w")
g = sns.FacetGrid(plot_data, col="feature")
g = (g.map(plt.scatter, "feature_value", "label", **kws))

enter image description here

Upvotes: 3

Related Questions