gpt4 book ai didi

python - Python 中带有 4 个面板 (2 x 2) 的 Seaborn 直方图

转载 作者:行者123 更新时间:2023-11-28 22:35:52 25 4
gpt4 key购买 nike

我正在尝试使用 sklearn.datasets.load_irisseaborn 重新创建此图像。我非常喜欢执行 fig, ax = plt.subplots() 然后使用 seabornax=ax 属性的想法。我不知道如何重新创建这个情节:enter image description here

我检查了 stackoverflow 并发现了这个,但它覆盖了它们 How To Plot Multiple Histograms On Same Plot With Seaborn

这是我的代码和情节:

# Iris Dataset
from sklearn.datasets import load_iris
import matplotlib.pyplot as plt
import seaborn as sns; sns.set()

%matplotlib inline

DF_data = pd.DataFrame(load_iris().data,
columns = load_iris().feature_names,
index = ["iris_%d" % i for i in range(load_iris().data.shape[0])])

Se_targets = pd.Series(load_iris().target,
index = ["iris_%d" % i for i in range(load_iris().data.shape[0])],
name = "Targets")

#Visualizing Iris Data
D_targets = {0: 'Iris-Setosa',
1: 'Iris-Versicolor',
2: 'Iris-Virgnica'}

D_features = {0: 'sepal length [cm]',
1: 'sepal width [cm]',
2: 'petal length [cm]',
3: 'petal width [cm]'}

fig, ax = plt.subplots(nrows=2, ncols=2, figsize=(8, 6))

idx_feature = 0

#Plot on 2 x 2 ax object

for i in range(ax.shape[0]):
for j in range(0, ax.shape[1]):
for idx_target, label_target in list(D_targets.items()):
sns.distplot(DF_data.as_matrix()[Se_targets==idx_target, idx_feature],
label=D_features[idx_feature],
kde=False,
bins=10,
ax=ax[i][j])
idx_feature += 1

plt.legend(loc='upper right', fancybox=True, fontsize=8)

plt.tight_layout()
plt.show()

我的情节看起来很糟糕:

enter image description here

更新:

作为对@Cel 回答的回应,我已经完成了这个图,但我无法修复标签并使图周围的线条变暗。

enter image description here

最佳答案

或者你可以做

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

iris = sns.load_dataset("iris")
iris_long = pd.melt(iris, "species", var_name="measurement")
g = sns.FacetGrid(iris_long, hue="species", col="measurement", col_wrap=2, sharex=False)
g.map(plt.hist, "value", alpha=.4)

enter image description here

关于python - Python 中带有 4 个面板 (2 x 2) 的 Seaborn 直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37911731/

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