使用sns distplot创建图矩阵

时间:2018-11-07 20:07:16

标签: python matplotlib plot seaborn

我正在绘制20多个特征,如下所示:

for col in dsd_mod["ae_analysis"].columns[:len(dsd_mod["ae_analysis"].columns)]:
    if col != "sae_flag":
        sns.distplot(dsd_mod["ae_analysis"].loc[(dsd_mod["ae_analysis"]['sae_flag'] == 1),col],
                color='r',
                kde=True,
                hist=False,
                label='sae_ae = 1')
        sns.distplot(dsd_mod["ae_analysis"].loc[(dsd_mod["ae_analysis"]['sae_flag'] == 0),col],
                color='y',
                kde=True,
                hist=False,
                label='sae_ae = 0') 

为每个功能创建一个单独的图形。如何将所有这些放到矩阵上?还是像一对如何绘制输出?

现在,我在一列中得到了30张这样的图表: enter image description here

如何修改它以便获得6行5列?

谢谢!

1 个答案:

答案 0 :(得分:1)

displot可以使用要绘制绘图的任何轴对象。因此,您只需要创建具有所需几何形状的轴,然后将相关轴传递给函数即可。

fig, axs = plt.subplots(6,5)
# axs is a 2D array with shape (6,5)
# you can keep track of counters in your for-loop to place the resulting graphs
# using ax=axs[i,j]
# or an alternative is to use a generator that you can use to get the next axes
# instance at every step of the loop
ax_iter = iter(axs.flat)
for _ in range(30):
    ax = next(ax_iter)
    sns.distplot(np.random.normal(loc=0, size=(1000,)), ax=ax)
    sns.distplot(np.random.normal(loc=1, size=(1000,)), ax=ax)

enter image description here