Seaborn有一个很好的FacetGrid function.You可以合并你的两个dataframes环绕正常matplotlib.pyplot.scatter()的seaborn facetgrid
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))
来源
2016-03-03 03:17:38
Sam
我喜欢你的答案,但有可能for循环中的一个小小的技术错误 - 对于我在范围(7)中......然后我再次用于“[random.random()for i in range(10)]”......也许应该更改为“j”什么的? –
我想你会发现,如果你测试代码,它会得出随机生成的测试数据帧的预期结果;但我同意我的两次使用可能会有点混乱。 – Sam
啊..我猜你正在使用Python 3?是的,Python版本2泄漏了控制变量。参考:http://stackoverflow.com/a/4199355/904032 ...我在版本2上运行它。 –