Creating you first boxplot with histogram using only Matplotlib

Bráulio Hermanson
2 min readSep 26, 2022

Hi, this article will be more technical, so if you need help understanding some parts, check out the Matplot library to understand the basis, apply the codes, to create your first boxplot histogram chart.

And then jump to this article!

In the first part, we need to call for nrows=2 and ncols = 1 to have a boxplot and histogram in the same position.

import matplotlib.pyplot as plt
fig, ax = plt.subplots(nrows = 2,ncol= 1, sharex=True, figsize=(18,7))

Then we call a boxplot chart first to appear up and after the histogram chart to appear on the button.

ax[0,0].boxplot(casas['MedHouseVal'],patch_artist=True, medianprops={"color": "white", "linewidth": 0.5},boxprops={"facecolor": "#52688F", "edgecolor": "white","linewidth": 0.5},vert=False, labels=['MedHouseVal'],showfliers=True)ax[1, 0].hist(casas['MedHouseVal'],bins=20,color = '#52688F')

And then, we call plt.show() to plot the graphs. Easy right?

The other command I’m using is to clean the figure and focus more on the charts.

ax[0,0].spines['right'].set_visible(False)
ax[0,0].spines['top'].set_visible(False)
ax[0,0].spines['left'].set_visible(False)
ax[0,0].spines['bottom'].set_visible(False)
ax[0,0].axis('off')
ax[0,0].set_title('MedHouseVal', color='Grey', size=24, loc='left')
ax[1, 0].spines['right'].set_visible(False)
ax[1, 0].spines['top'].set_visible(False)
plt.show()

Here my github page with all the code and more examples if you need!

--

--

Bráulio Hermanson

Data Analyst — Marketing and Specialist Data. | Python | Power BI | DataBricks | SQL | Hue Hadoop | Excel |Spark.