Matplotlib 循环遍历多个子图的 seaborn 图中的轴

洛可·麦克萨马斯

我想在 seaborn 直方图 (distplot) 上创建五个子图(数据框特定列中的每个类别一个)。

我的数据集是:

prog score
cool 1.9
cool 3.7
yay  4.5
yay  2.6
neat 1.4
neat 7
neat 6
wow  4.1
wow  1.7
wow  1.4
hooray 6.6
hooray 5.6
hooray 4.9
yikes 1.2
yikes 3.9
yikes 6.9

我不希望所有的 'prog's 都被绘制出来,只是一个列表中的每个:

prog_list = ['cool', 'yay', 'neat', 'yikes', 'wow']
scores = df['score']
f, axes = plt.subplots(3, 2, figsize=(15, 15))
# Delete last chart since there are only 5 subplots I need
f.delaxes(ax = axes[2,1])

for i, axes in enumerate(f.axes):
scores = df.loc[(df['prog'] == prog_list[i])]['score']
    axes = sns.distplot(scores, norm_hist=True, color='b')
    sigma = round(scores.std(), 3)
    mu = round(scores.mean(), 2)
    axes.set_xlim(1,7)
    axes.set_xticks(range(2,8))
    axes.set_xlabel('Score - Mean: {} (σ {})'.format(mu, sigma))
    axes.set_ylabel('Density')

但是当我这样做时,它只是将每个子集绘制到同一个图上(这有点酷,但绝对不是我想要的)。

在此处输入图片说明

广黄

试试这个:

# your code use axes and redefine it after every iteration
# I think this would be better
for prog, ax in zip(prog_list, axes.flatten()[:5]):
    scores = df.loc[(df['prog'] == prog)]['score']

    # note how I put 'ax' here
    sns.distplot(scores, norm_hist=True, ax=ax, color='b')

    # change all the axes into ax
    sigma = round(scores.std(), 3)
    mu = round(scores.mean(), 2)
    ax.set_xlim(1,7)
    ax.set_xticks(range(2,8))
    ax.set_xlabel('Score - Mean: {} (σ {})'.format(mu, sigma))
    ax.set_ylabel('Density')

plt.show()

输出:

在此处输入图片说明

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章