Juncai Liu
Juncai Liu

Reputation: 47

how to draw multiple distribution

fig_dspl, axes_dspl = plt.subplots(nrows=1, ncols=2, figsize=(9, 4))
sns.distplot(df_08['displ'], ax = axes_dspl[0]) 
_ = axes_dspl[0].set_title('08')
sns.distplot(df_18['displ'], ax = axes_dspl[1])
_ = axes_dspl[1].set_title('18')

can anyone explain the detail of this code above? especially the first line, is this for multiple graphs? i understand how to draw a single plot (sis.displot), don't clearly understand the ax = axes_dspl[0]) ... and what the _ = axes_dspl[0]

Upvotes: 1

Views: 56

Answers (1)

Ami Tavory
Ami Tavory

Reputation: 76316

Create two plots, get the figures and axes

fig_dspl, axes_dspl = plt.subplots(nrows=1, ncols=2, figsize=(9, 4))

In the first axes, draw a seaborn.distplot.

sns.distplot(df_08['displ'], ax = axes_dspl[0]) 

Set '08' as the title of this plot. Assign the result to _ and ignore it (you may as well write axes_dsp... instead of _ = axes_dsp... in this case).

_ = axes_dspl[0].set_title('08')

Similarly do so for the second axes.

sns.distplot(df_18['displ'], ax = axes_dspl[1])
_ = axes_dspl[1].set_title('18')

In conclusion:

  1. The first assignment (to the outcome of distplots) allows greater control of the result, in this case by setting the titles later on.

  2. The later assignments (_ = axes_dspl...) are just moise and are better omitted.

Upvotes: 1

Related Questions