Open In App

Adding Legend to Boxplot with Multiple Plots

Last Updated : 30 Sep, 2024
Comments
Improve
Suggest changes
Like Article
Like
Report

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:

multiple_boxplots
Boxplots in Matplotlib

Boxplot 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:

boxplot_legend
Boxplots in Matplotlib

Modifying 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:

legend
Boxplots in Matplotlib

Handling 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:

customloop
Boxplots in Matplotlib

This 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:

boxplot
Boxplots in Matplotlib

Conclusion

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.


Next Article

Similar Reads