gpt4 book ai didi

python - 绘制多个 seaborn 分布图

转载 作者:行者123 更新时间:2023-12-05 01:06:31 26 4
gpt4 key购买 nike

我正在尝试创建按列分组的数据框的 distplot

data_plot = creditcard_df.copy()
amount = data_plot['Amount']
data_plot.drop(labels=['Amount'], axis=1, inplace = True)
data_plot.insert(0, 'Amount', amount)

# Plot the distributions of the features
columns = data_plot.iloc[:,0:30].columns
plt.figure(figsize=(12,30*4))
grids = gridspec.GridSpec(30, 1)
for grid, index in enumerate(data_plot[columns]):
ax = plt.subplot(grids[grid])
sns.distplot(data_plot[index][data_plot.Class == 1], hist=False, kde_kws={"shade": True}, bins=20)
sns.distplot(data_plot[index][data_plot.Class == 0], hist=False, kde_kws={"shade": True}, bins=20)
ax.set_xlabel("")
ax.set_title("Distribution of Column: " + str(index))
plt.show()

plot我尝试对 y 轴使用对数刻度,更改 gridspec 和 figsize;但所有这些都只会弄乱分布。有没有办法使地 block 统一?

最佳答案

导入和测试数据

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

np.random.seed(365)
rows = 10000
data = {'a': np.random.normal(5, 5, rows),
'b': np.random.normal(20, 5, rows),
'c': np.random.normal(35, 5, rows),
'd': np.random.normal(500, 50, rows),
'e': np.random.normal(6500, 500, rows),
'class': np.random.choice([0, 1], size=(rows), p=[0.25, 0.75])}
df = pd.DataFrame(data)

# display(df.head(3))
a b c d e class
0 5.839606 20.807027 34.798230 509.328065 6003.228497 0
1 7.617526 21.691519 40.519995 445.724478 7204.039621 0
2 9.086878 27.193222 32.776264 498.254687 6810.960924 1

使用 seaborn.kdeplot

绘图
fig, axes = plt.subplots(nrows=2, ncols=3, figsize=(15, 7), sharex=False, sharey=False)
axes = axes.ravel() # array to 1D
cols = df.columns[:-1] # create a list of dataframe columns to use

for col, ax in zip(cols, axes):
data = df[[col, 'class']] # select the data
sns.kdeplot(data=data, x=col, hue='class', shade=True, ax=ax)
ax.set(title=f'Distribution of Column: {col}', xlabel=None)

fig.delaxes(axes[5]) # delete the empty subplot
fig.tight_layout()
plt.show()

enter image description here

使用 seaborn.displot

绘图
# convert the dataframe from wide to long
dfm = df.melt(id_vars='class', var_name='Distribution')

# display(dfm.head(3))
class Distribution value
0 0 a 5.839606
1 0 a 7.617526
2 1 a 9.086878

# plot
sns.displot(kind='kde', data=dfm, col='Distribution', col_wrap=3, x='value', hue='class', fill=True, facet_kws={'sharey': False, 'sharex': False})

enter image description here

关于python - 绘制多个 seaborn 分布图,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/69067250/

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