0

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 Answer 1

3

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()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.