Skip to content Skip to sidebar Skip to footer

Matplotlib Loop Through Axes In A Seaborn Plot For Multiple Subplots

I'd like to create five subplots (one for each category in a specific column of a dataframe) on a seaborn histogram (distplot). My dataset is: prog score cool 1.9 cool 3.7 yay 4.5

Solution 1:

Try this:

# 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()

Output:

enter image description here

Post a Comment for "Matplotlib Loop Through Axes In A Seaborn Plot For Multiple Subplots"