I'm trying to create a subplot of subplots using Matplotlib's gridspec
, namely a boxplot above a histogram. I have a pandas dataframe of quantitative data. I have this code:
import matplotlib.pyplot as plt
import seaborn as sns
import matplotlib.gridspec as gridspec
fig = plt.figure(figsize=(20, 20))
outer = fig.add_gridspec(6, 2, wspace=0.05, hspace=0.2)
a = 0
for i in range(6):
for j in range(2):
inner = outer[i, j].subgridspec(2, 1, wspace=0, hspace=0)
axs = inner.subplots()
for c, ax in np.ndenumerate(axs):
sns.boxplot(data=data_quant, x=data_quant[data_quant.columns[a]], orient='h', ax=ax)
data_quant[data_quant.columns[a]].hist(bins=50)
ax.set(xticks=[])
a += 1
plt.show()
And this is the result: Histogram and boxplot
My code is inspired by this: Histogram with Boxplot above in Python but obviously I'm not getting the same results, i.e., the histograms are upside down and the boxplot is way too large.
Any clues as to how to do it?