How to Create Box Plot in Matplotlib: Simple Guide
To create a box plot in
matplotlib, use the boxplot() function on your data array or list. This function visualizes data distribution by showing median, quartiles, and outliers in a simple plot.Syntax
The basic syntax to create a box plot is matplotlib.pyplot.boxplot(data, notch=False, vert=True, patch_artist=False).
data: The list or array of numbers to plot.notch: IfTrue, draws a notch to show confidence interval around the median.vert: IfTrue, the box plot is vertical; ifFalse, horizontal.patch_artist: IfTrue, fills the box with color.
python
import matplotlib.pyplot as plt data = [1, 2, 3, 4, 5] # Example data plt.boxplot(data, notch=False, vert=True, patch_artist=False) plt.show()
Example
This example shows how to create a simple vertical box plot with sample data. It demonstrates the median, quartiles, and outliers visually.
python
import matplotlib.pyplot as plt import numpy as np # Sample data: 100 random numbers from a normal distribution np.random.seed(0) data = np.random.normal(100, 20, 100) plt.boxplot(data, notch=True, patch_artist=True) plt.title('Box Plot Example') plt.ylabel('Values') plt.show()
Output
A vertical box plot showing the distribution of 100 random values with a notch around the median and colored box.
Common Pitfalls
Common mistakes when creating box plots include:
- Passing data in wrong format (must be list or array of numbers).
- Not calling
plt.show()to display the plot. - Confusing vertical and horizontal orientation by misusing
vertparameter. - Ignoring outliers which can affect the scale of the plot.
python
import matplotlib.pyplot as plt # Wrong: data as a single number instead of list # plt.boxplot(5) # This will cause an error # Right way: data = [5, 7, 8, 5, 6] plt.boxplot(data) plt.show()
Output
A vertical box plot showing the distribution of the small list of numbers.
Quick Reference
Here is a quick summary of key boxplot() parameters:
| Parameter | Description | Default |
|---|---|---|
| data | List or array of numbers to plot | Required |
| notch | Draw notch to show confidence interval | False |
| vert | Plot vertically if True, horizontally if False | True |
| patch_artist | Fill box with color if True | False |
| showmeans | Show mean marker if True | False |
| labels | Labels for each dataset | None |
Key Takeaways
Use plt.boxplot(data) to create a basic box plot in matplotlib.
Set notch=True to add a notch showing median confidence interval.
Always call plt.show() to display the plot.
Pass data as a list or array of numbers, not a single value.
Use patch_artist=True to fill the box with color for better visuals.