简体   繁体   English

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

[英]Python: dynamically produce seaborn plots then display results side-by-side?

I'm trying to produce multiple seaborn kernel density plots for the numeric variables of my Pandas DataFrame. 我正在尝试为Pandas DataFrame的数字变量生成多个seaborn内核密度图。 I have the names of all of my numeric columns in a list, numberCol . 我在列表numberCol中有所有数字列的名称。 Presently, I can make a kdeplot for each variable that I explicitly name, like so: 目前,我可以为我明确命名的每个变量创建一个kdeplot ,如下所示:

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

Is there a better way to iterate through the numberCol list, produce an sbn.kdeplot for each variable in numberCol , then display them side-by-side with something smarter than something like: 有没有更好的方式,通过迭代numberCol列表,产生sbn.kdeplot在每个变量numberCol ,然后显示它们并排侧有东西比像聪明:

import matplotlib.pyplot as plt
import seaborn as sns

# Here we create a figure instance, and two subplots
fig = plt.figure(figsize = (20,20)) # width x height
ax1 = fig.add_subplot(3, 3, 1) # row, column, position
ax2 = fig.add_subplot(3, 3, 2)
ax3 = fig.add_subplot(3, 3, 3)

# We use ax parameter to tell seaborn which subplot to use for this plot
sns.heatmap(data=subset1.corr(), ax=ax1, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})
sns.heatmap(data=subset2.corr(), ax=ax2, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})
sns.heatmap(data=subset3.corr(), ax=ax3, cmap = cmap, square=True, cbar_kws={'shrink': .3}, annot=True, annot_kws={'fontsize': 12})

If I understand your question, this should do the trick 如果我了解您的问题,就可以解决问题

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)

在此处输入图片说明

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM