0
votes

I am trying to create subplots of a column in pandas dataframe grouped by each of the other columns. Here I create and iterate through subplots and attempt to add a boxplot to each one.

fig, axes = plt.subplots(nrows=2, ncols=2) # create 2x2 array of subplots

axes[0,0] = df.boxplot(column='price') # add boxplot to 1st subplot
axes[0,1] = df.boxplot(column='price', by='bedrooms') # add boxplot to 2nd subplot
# etc.
plt.show()

this results in

enter image description here

As you can see, the boxplots are not being added to the subplots. I am not sure where I am going wrong. All the documentation I have found says that [0,0] is the upper left corner, and the boxplots work.. I need to use df.boxplots() specifically.

1

1 Answers

0
votes

You should pass axes as argument to plot function:

fig, axes = plt.subplots(nrows=2, ncols=2) # create 2x2 array of subplots

df.boxplot(column='price', ax=axes[0,0]) # add boxplot to 1st subplot
df.boxplot(column='price', by='bedrooms', ax=axes[0,1]) # add boxplot to 2nd subplot
# etc.
plt.show()