0

I've produced two code snippets for seaborn histograms in subplots. First one

fig, axes = plt.subplots(1, 2, figsize=(15, 5), sharey=False)
sns.histplot(ax=axes[1,0],x=data.age)
sns.histplot(ax=axes[0,0],x=data.duration)
plt.show()

which gives me an error:

sns.histplot(ax=axes[1,0],x=data.age)
IndexError: too many indices for array: array is 1-dimensional, but 2 were indexed

and the second one

fig, axes = plt.subplots(1, 2, figsize=(15, 5), sharey=False)
sns.histplot(ax=axes[1],x=data.age)
sns.histplot(ax=axes[0],x=data.duration)
plt.show()

which gives me enter image description here

The data are from bank marketing dataset from uci.

Pleas advise me what I'm doing wrong in the first snippet.

data_b77
  • 415
  • 6
  • 19
  • 2
    You can write `fig, axes = plt.subplots(..., squeeze=False)` to always have axes with 2D indices. The default is `squeeze=True` with 1D indices in case there is only one row or only one column of axes (or even no indices when there is only one ax). The default permits writing `fig, (ax1, ax2) = plt.subplots(1, 2)` instead of the unfamiliar looking `fig, ((ax1, ), (ax2, )) = plt.subplots(1, 2, squeeze=False)`. Especially when the number of rows is calculated on-the-fly, `squeeze=False` will be helpful. – JohanC Apr 21 '21 at 20:11
  • works :) thank you – data_b77 Apr 21 '21 at 21:17

0 Answers0