gpt4 book ai didi

python-3.x - 使用 seaborn 绘制多个直方图

转载 作者:行者123 更新时间:2023-12-05 08:32:09 24 4
gpt4 key购买 nike

我有一个包含 36 列的数据框。我想使用 seaborn 一次 (6x6) 绘制每个特征的直方图。基本上复制 df.hist() 但使用 seaborn。我下面的代码只显示了第一个功能的情节,所有其他功能都是空的。

enter image description here

测试数据框:

df = pd.DataFrame(np.random.randint(0,100,size=(100, 36)), columns=range(0,36))

我的代码:

import seaborn as sns
# plot
f, axes = plt.subplots(6, 6, figsize=(20, 20), sharex=True)
for feature in df.columns:
sns.distplot(df[feature] , color="skyblue", ax=axes[0, 0])

最佳答案

我想同时遍历轴和特征是有意义的。

f, axes = plt.subplots(6, 6, figsize=(20, 20), sharex=True)
for ax, feature in zip(axes.flat, df.columns):
sns.distplot(df[feature] , color="skyblue", ax=ax)

Numpy 数组按行展平,即您将在第一行获得前 6 个特征,在第二行获得第 6 到 11 个特征,依此类推。

如果这不是你想要的,你可以手动定义轴数组的索引,

f, axes = plt.subplots(6, 6, figsize=(20, 20), sharex=True)
for i, feature in enumerate(df.columns):
sns.distplot(df[feature] , color="skyblue", ax=axes[i%6, i//6])

例如以上将逐列填充子图。

关于python-3.x - 使用 seaborn 绘制多个直方图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53171496/

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