Understanding Boxplots in Matplotlib
In this blog, we will explore how to create boxplots in Matplotlib using the plt.boxplot() function. Boxplots, also known as box-and-whisker plots, are a great way to visualize the spread of data points and detect outliers in a dataset.
My Program — Box Plot
import matplotlib.pyplot as plt
data = [1, 2, 19, 20, 21, 39, 40, 77]
plt.boxplot(data)
plt.savefig("Plots/fig_with_boxplot.png")
plt.clf()
new_data = [26, 61, 62, 79, 80, 81, 99, 100]
plt.boxplot(new_data, vert=False, patch_artist=True, notch=True, showmeans=True)
plt.savefig("Plots/fig_with_boxplot_custom.png")
plt.clf()We will go over a simple example of plotting a standard boxplot and then delve into customizing a boxplot with various options such as vertical or horizontal orientation, notches, and displaying the mean.
Importing Matplotlib
Before we begin, we need to import the necessary library. We use matplotlib.pyplot to generate our plots and alias it as plt for convenience.
import matplotlib.pyplot as pltPlotting a Simple Boxplot
The first dataset we will work with is:
data = [1, 2, 19, 20, 21, 39, 40, 77]This dataset has a wide range of values, including a potential outlier (77). Let’s go through the process of creating a simple boxplot and saving it to a file.
Step-by-Step Code Breakdown
Boxplot Setup:
We create the boxplot using the following code:
plt.boxplot(data)- The data values range from 1 to 77.
- The interquartile range (IQR) is calculated, and the box itself represents the middle 50% of the data.
- The whiskers extend to the minimum and maximum values, excluding the outlier, which is plotted as a point.
- The outlier (77) is detected and shown separately.
Saving the Plot:
Once the boxplot is generated, we save it as an image:
plt.savefig("Plots/fig_with_boxplot.png")This saves the figure to the “Plots” directory with the filename fig_with_boxplot.png.
Clearing the Plot:
After saving, it’s important to clear the figure so that subsequent plots do not overlap:
plt.clf()Visualization
Customizing the Boxplot
Now, we will create a customized boxplot using a new dataset:
new_data = [26, 61, 62, 79, 80, 81, 99, 100]This dataset also has a wide range of values with an outlier (26). We’ll apply several customizations to enhance the visual appeal and informational content of the plot.
Step-by-Step Code Breakdown
Creating the Custom Boxplot:
We generate a horizontal boxplot and enable additional features like notches and mean display:
plt.boxplot(new_data, vert=False, patch_artist=True, notch=True, showmeans=True)- Horizontal Orientation (vert=False): The boxplot is drawn horizontally.
- Patch Artist (patch_artist=True): This enables color filling for the box.
- Notch (notch=True): Notches provide a visual indication of the confidence interval around the median.
- Show Mean (showmeans=True): Displays the mean value (in this case, around 73) with a triangle marker.
Saving the Plot:
After customizing the boxplot, we save the image:
plt.savefig("Plots/fig_with_boxplot_custom.png")Clearing the Plot:
Finally, clear the plot again to prevent any interference with future plots:
plt.clf()Visualization
Outlier Detection and Interpretation
In both plots, you can observe that certain points are marked as outliers. These points are calculated using the formula:
Low Bound — 1.5 * IQR and High Bound + 1.5 * IQR
Where IQR is the Interquartile Range, which is the difference between the third and first quartiles. The whiskers in the boxplot mark the minimum and maximum values within this range, while outliers are marked as individual points outside the whiskers.
Conclusion
Boxplots are a powerful way to visualize data distribution, spread, and detect outliers in your datasets. In this blog, we learned how to create both standard and customized boxplots using Matplotlib.
Get Someone’s stories in your inbox
Join Medium for free to get updates from this writer.
Customizations like orientation, notches, color filling, and showing the mean make the plots more informative and visually appealing.
If you’re interested in exploring more about visualizations in Python, feel free to check out my GitHub repository and stay updated on Twitter for my latest posts!
Stay tuned for more tutorials in my Matplotlib series!







