gpt4 book ai didi

python - 如何绘制具有多个数据框的 FacetGrid 散点图?

转载 作者:行者123 更新时间:2023-11-30 22:59:24 26 4
gpt4 key购买 nike

我有 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 个数据点。

最佳答案

Seaborn 有一个不错的 FacetGrid函数。您可以合并两个数据框,将seabornfacetgrid包裹在正常的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

关于python - 如何绘制具有多个数据框的 FacetGrid 散点图?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35761985/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com