0

I would like to create 5 boxplots from a pandas dataframe and add an additional plot with a bar chart to create a full 3x2 matrix of plots. However, I am getting an error in several approaches.

Try #1: Trying to generate the subplots I want with plt.subplots(), however the error tells me I can only have 5 axes in the ax argument:

fig, axes = plt.subplots(3, 2)

df.groupby('characteristic').boxplot(
    by='month', sharey=False, ax=axes, 
)

plt.tight_layout()

returns:

ValueError: The number of passed axes must be 5, the same as the output plot

Try #2: Running the .boxplot() method first and adding the Axes to a separate plot after, but the Axes don't seem to copy correctly to a new plot.

# create the first 5 axes
boxplots = df.groupby('characteristic').boxplot(
    by='month', sharey=False,
)

# initialize the final 6 axis grid
fig, axes = plt.subplots(3, 2)

# add the new plot
second_df.plot(kind='bar', ax=axes[0, 0])

# attempt to copy the boxplots into the new set of axes
axes[0, 1] = boxplots.A
axes[1, 0] = boxplots.B
axes[1, 1] = boxplots.C
axes[2, 0] = boxplots.D
axes[2, 1] = boxplots.E

plt.tight_layout()

This method, however, produces two figures in the output, neither of which contains all the plots: undesired output from attempt #2 showing one figure with the 5 boxplots and another with just the second plot

Is there a way to add a non-boxplot plot to a group of boxplots?

1
  • 2
    (*bxp_axes, last_ax) = axes.flat ? Commented Jan 29 at 1:37

0

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.