Python:动态生成seaborn图,然后并排显示结果?

2024-05-12 18:13:29 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在尝试为Pandas数据帧的数值变量生成多个seaborn内核密度图。我有一个列表中所有数值列的名称,numberCol。目前,我可以为我显式命名的每个变量生成一个kdeplot,如下所示:

import seaborn as sbn
sbn.set_style('whitegrid')
sbn.kdeplot(np.array(df.v2), bw=0.5) # for pandas.core.frame.DataFrame input

有没有更好的方法来迭代numberCol列表,为numberCol中的每个变量生成一个sbn.kdeplot,然后用比以下更聪明的方式并排显示它们:

^{pr2}$

Tags: 数据import名称pandas列表asseaborn内核
1条回答
网友
1楼 · 发布于 2024-05-12 18:13:29

如果我理解你的问题,这应该能解决问题

Ncols = 9
cols = ['col_{:d}'.format(i) for i in range(Ncols)]
df = pd.DataFrame(np.random.random(size=(1000,Ncols)),columns=cols)

fig, axs = plt.subplots(3,3) # adjust the geometry based on your number of columns to plot
for ax,col in zip(axs.flatten(), cols):
    sns.kdeplot(df[col], ax=ax)

enter image description here

相关问题 更多 >