如何使用多个数据框绘制FacetGrid散点图?

时间:2016-03-03 02:21:39

标签: python pandas seaborn

我有2个数据帧,1个有训练数据,另一个有标签。训练数据中有6个特征/列,标签数据框中有1列。我想在我的小平面网格中有6个绘图 - 所有这些都是散点图。因此,功能1与标签,功能2与标签,功能3与标签,功能4与标签相比。

有人可以告诉我该怎么做吗?

例如,使用这些样本数据框

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

这应该制作6个独立的散点图,每个散点图有2个数据点。

1 个答案:

答案 0 :(得分:3)

Seaborn有一个很好的FacetGrid函数。你可以合并你的两个数据帧将seaborn facetgrid包裹在普通的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