Adding Legend to Boxplot with Multiple Plots
Last Updated :
30 Sep, 2024
Boxplots are an effective way to visualize the distribution of a dataset. When analyzing multiple datasets simultaneously, it can become challenging to differentiate between them without a clear legend. This article will guide you through the process of adding a legend to a Matplotlib boxplot with multiple plots on the same axis, ensuring clarity and effectiveness in your visualizations.
Understanding Boxplots in Matplotlib
Before diving into the specifics of adding legends, it's important to understand how boxplots are created in Matplotlib. A boxplot is a graphical representation of the distribution of a set of data based on a five-number summary: minimum, first quartile, median, third quartile, and maximum.
When dealing with multiple boxplots on the same axis, legends are crucial for identifying which boxplot corresponds to which dataset. Without legends, the plot can be confusing and difficult to interpret.
Here is a simple example of creating a boxplot in Matplotlib:
Python
import matplotlib.pyplot as plt
data1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
data2 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
plt.boxplot([data1, data2])
plt.title("Multiple Boxplots")
plt.show()
Output:
Boxplots in MatplotlibBoxplot with Legend - Customizing the Appearance
To make the plot informative, adding a legend is essential, especially when multiple datasets are represented on the same axes.
# Create custom labels
labels = ['Dataset 1', 'Dataset 2', 'Dataset 3']
# Add a legend
ax.legend([box['boxes'][i] for i in range(len(colors))], labels)
Let's implement an complete code for visualizing boxplot with legend:
Python
import matplotlib.pyplot as plt
import numpy as np
np.random.seed(10)
data1 = np.random.normal(loc=20, scale=5, size=100)
data2 = np.random.normal(loc=30, scale=10, size=100)
data3 = np.random.normal(loc=25, scale=7, size=100)
# Combine data into a list
data = [data1, data2, data3]
# Create a figure and axis
fig, ax = plt.subplots()
# Create the boxplot
box = ax.boxplot(data, patch_artist=True)
# Set labels for x-axis
ax.set_xticklabels(['Dataset 1', 'Dataset 2', 'Dataset 3'])
# Define colors for each dataset
colors = ['lightblue', 'lightgreen', 'lightcoral']
# Apply colors to each boxplot
for patch, color in zip(box['boxes'], colors):
patch.set_facecolor(color)
# Create custom labels
labels = ['Dataset 1', 'Dataset 2', 'Dataset 3']
# Add a legend
ax.legend([box['boxes'][i] for i in range(len(colors))], labels)
plt.title('Boxplot with Legend for Multiple Datasets')
plt.show()
Output:
Boxplots in MatplotlibModifying Legend Position in Boxplot
By default, the legend appears in the upper right corner of the plot. You can customize its position for better visibility.
Python
box = ax.boxplot(data, patch_artist=True)
# Set labels for x-axis
ax.set_xticklabels(['Dataset 1', 'Dataset 2', 'Dataset 3'])
# Define colors for each dataset
colors = ['lightblue', 'lightgreen', 'lightcoral']
# Apply colors to each boxplot
for patch, color in zip(box['boxes'], colors):
patch.set_facecolor(color)
# Create custom labels
labels = ['Dataset 1', 'Dataset 2', 'Dataset 3']
# Add a legend
# Positioning the legend
ax.legend([box['boxes'][i] for i in range(len(colors))], labels, loc='upper left')
# Show the plot
plt.title('Boxplot with Legend for Multiple Datasets')
plt.show()
Output:
Boxplots in MatplotlibHandling Multiple Boxplots in a Loop
If you are plotting multiple boxplots in a loop, you can manage the legend labels and handles within the loop. Here’s an example:
Python
import matplotlib.pyplot as plt
data1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
data2 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
data3 = [3, 4, 5, 6, 7, 8, 9, 10, 11, 12]
datasets = [data1, data2, data3]
labels = ['Dataset 1', 'Dataset 2', 'Dataset 3']
colors = ['blue', 'orange', 'green']
fig, ax = plt.subplots()
for data, label, color in zip(datasets, labels, colors):
bp = ax.boxplot(data, patch_artist=True)
for patch in bp['boxes']:
patch.set_facecolor(color)
patch.set_label(label)
ax.legend()
plt.title("Multiple Boxplots with Legend")
plt.show()
Output:
Boxplots in MatplotlibThis method ensures that each boxplot is correctly labeled and colored, and the legend reflects these labels.
Boxplot Using Custom Legend Handles
For older versions of Matplotlib or for more customized control, you can create custom legend handles. Here’s an example using matplotlib.patches:
Python
import matplotlib.pyplot as plt
import matplotlib.patches as mpatches
data1 = [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
data2 = [2, 3, 4, 5, 6, 7, 8, 9, 10, 11]
plt.boxplot([data1, data2])
legend_handle1 = mpatches.Patch(color='blue', label='Dataset 1')
legend_handle2 = mpatches.Patch(color='orange', label='Dataset 2')
plt.legend(handles=[legend_handle1, legend_handle2])
plt.title("Boxplot with Custom Legend")
plt.show()
Output:
Boxplots in MatplotlibConclusion
Adding legends to boxplots in Matplotlib is a crucial step in ensuring that your data visualizations are clear and interpretable. By using the label parameter in newer versions of Matplotlib or creating custom legend handles, you can effectively manage and customize the legends for your boxplots.
Similar Reads
Adding Titles to Seaborn Boxplots
Seaborn is a powerful Python library for data visualization that makes it easy to create aesthetically pleasing and informative plots. Boxplots are a popular type of plot for visualizing the distribution of a dataset. Adding a title to a Seaborn boxplot can help provide context and enhance the inter
4 min read
How To Join Multiple ggplot2 Plots with cowplot?
In this article, we are going to see how to join multiple ggplot2 plots with cowplot. To join multiple ggplot2 plots, we use the plot_grid() function of the cowplot package of R Language. Syntax: plot_grid(plot1,plot2,label=<label-vector>, ncol, nrow) Parameters: plot1 and plot2 are plots that
3 min read
Plot multiple plots in Matplotlib
Prerequisites: MatplotlibIn Matplotlib, we can draw multiple graphs in a single plot in two ways. One is by using subplot() function and other by superimposition of second graph on the first i.e, all graphs will appear on the same plot. We will look into both the ways one by one.Multiple Plots using
6 min read
Multiple Density Plots with Pandas in Python
Multiple density plots are a great way of comparing the distribution of multiple groups in your data. Â We can make multiple density plots using pandas plot.density() function. However, we need to convert data in a wide format if we are using the density function. Wide data represents different group
2 min read
Adding Notches to Box Plots in R
In this article, we will study how to add a notch to box plot in R Programming Language. The ggvis package in R is used to provide the data visualization. It is used to create visual interactive graphics tools for data plotting and representation. The package can be installed into the working space
2 min read
How to Combine Multiple ggplot2 Plots in R?
In this article, we will discuss how to combine multiple ggplot2 plots in the R programming language. Combining multiple ggplot2 plots using '+' sign to the final plot In this method to combine multiple plots, here the user can add different types of plots or plots with different data to a single p
2 min read
Add Common Main Title for Multiple Plots in R
In this article, we will be looking at the two different approaches to add a common main title for multiple plots in base R and ggplot2 package in the R programming language. Method 1: Using par and mtext() function In this approach to add a common main title for multiple plots, the user needs to ca
4 min read
How to manually add a legend with a color box on a Matplotlib figure ?
A legend is basically an area in the plot which describes the elements present in the graph. Matplotlib provides an inbuilt method named legend() for this purpose. The syntax of the method is below : Example: Adding Simple legend Python3 # Import libraries import matplotlib.pyplot as plt # Creating
2 min read
Plot multiple boxplots in one graph in R
In this article, we will learn how to plot multiple boxplot in one graph in R Programming Language. This can be accomplished by using boxplot() function, and we can also pass in a list, data frame or multiple vectors to it. For this purpose, we need to put name of data into boxplot() function as inp
2 min read
Python Plotly - How to add multiple Y-axes?
In this article let's see how to add multiple y-axes of different scales in Plotly charts in Python. Currently, Plotly Express does not support multiple Y axes on a single figure. So, we shall use Plotly go. Plotly provides a function called make_subplots() to plot charts with multiple Y - axes. Sy
5 min read