Reputation: 137
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:
Is there a way to add a non-boxplot plot to a group of boxplots?
Upvotes: 0
Views: 30