Article Categories
- All Categories
-
Data Structure
-
Networking
-
RDBMS
-
Operating System
-
Java
-
MS Excel
-
iOS
-
HTML
-
CSS
-
Android
-
Python
-
C Programming
-
C++
-
C#
-
MongoDB
-
MySQL
-
Javascript
-
PHP
-
Economics & Finance
How to make a mosaic plot in Matplotlib?
A mosaic plot is a graphical method for visualizing categorical data and relationships between variables. In Python, we can create mosaic plots using the statsmodels library along with Matplotlib.
Installation Requirements
First, install the required package ?
pip install statsmodels
The statsmodels package provides statistical computations and is essential for creating mosaic plots in Python.
Basic Mosaic Plot
Here's how to create a simple mosaic plot from a dictionary ?
import matplotlib.pyplot as plt
from statsmodels.graphics.mosaicplot import mosaic
# Set figure size
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
plt.show()
A mosaic plot with rectangles representing each category. Rectangle sizes are proportional to the values in the dictionary.
Two-Variable Mosaic Plot
For more complex data analysis, create a mosaic plot with two categorical variables ?
import matplotlib.pyplot as plt
from statsmodels.graphics.mosaicplot import mosaic
import pandas as pd
# Create sample data with two variables
data = {
('Male', 'Pass'): 45,
('Male', 'Fail'): 15,
('Female', 'Pass'): 35,
('Female', 'Fail'): 5
}
# Create mosaic plot
fig, ax = plt.subplots(figsize=(8, 6))
mosaic(data, ax=ax, title='Gender vs Exam Results')
plt.show()
A mosaic plot showing the relationship between gender and exam results. Each rectangle represents a combination of the two variables.
Customizing Mosaic Plots
You can customize colors and add labels for better visualization ?
import matplotlib.pyplot as plt
from statsmodels.graphics.mosaicplot import mosaic
# Sample data
data = {'Product A': 25, 'Product B': 40, 'Product C': 20, 'Product D': 15}
# Custom colors for each category
colors = {'Product A': 'lightblue', 'Product B': 'lightgreen',
'Product C': 'lightcoral', 'Product D': 'lightyellow'}
# Create customized mosaic plot
fig, ax = plt.subplots(figsize=(10, 6))
mosaic(data, ax=ax, title='Product Sales Distribution',
properties=lambda key: {'color': colors.get(key, 'lightgray')})
plt.show()
A colorful mosaic plot with custom colors for each product category. The plot clearly shows the relative proportion of each product's sales.
Key Features
| Feature | Description | Usage |
|---|---|---|
| Rectangle Size | Proportional to data values | Visual comparison of categories |
| Color Coding | Custom colors for categories | Better visual distinction |
| Multi-variable | Support for nested categories | Complex data relationships |
Conclusion
Mosaic plots are excellent for visualizing categorical data proportions. Use statsmodels.graphics.mosaicplot.mosaic() with dictionaries or DataFrames to create these informative visualizations in Python.
