How to make a mosaic plot in Matplotlib?

MatplotlibPythonData Visualization

To make a mosaic plot in matplotlib, we can take the following steps.


  • Set the figure size and adjust the padding between and around the subplots.

  • Install statsmodel package (pip install statsmodels). It is required to create mosaic plots. statsmodels is a Python package that provides a complement to scipy for statistical computations including descriptive statistics and estimation and inference for statistical models.

  • Make a dictionary for mosaic plot.

  • Create a mosaic plot from a contingency table.

  • To display the figure, use Show() method.


import matplotlib.pyplot as plt
from import mosaic

plt.rcParams["figure.figsize"] = [7.00, 3.50]
plt.rcParams["figure.autolayout"] = True

# Dictionary for mosaic plot
data = {'John': 7, 'Joe': 10, 'James': 5, 'Kate': 1}

# Create mosaic plot
mosaic(data, title='Basic mosaic plot')

# Display the figure


It will produce the following output −

Published on 08-Oct-2021 12:34:58