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
Selected Reading
How to move the legend to outside of a Seaborn scatterplot in Matplotlib?
To move the legend outside of a Seaborn scatterplot, you need to use the bbox_to_anchor parameter in the legend() method. This allows precise control over legend positioning relative to the plot area.
Basic Setup
First, let's create a sample dataset and generate a scatterplot ?
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# Set figure size
plt.rcParams["figure.figsize"] = [8, 5]
plt.rcParams["figure.autolayout"] = True
# Create sample data
df = pd.DataFrame({
'x_values': [2, 1, 4, 3, 5, 2, 6],
'y_values': [5, 2, 1, 4, 3, 6, 2],
'category': ['A', 'B', 'A', 'C', 'B', 'C', 'A']
})
# Create scatterplot with hue for different categories
sns.scatterplot(data=df, x='x_values', y='y_values', hue='category', s=100)
# Move legend outside the plot
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left')
plt.tight_layout()
plt.show()
Different Legend Positions
You can position the legend in various locations outside the plot ?
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# Create sample data
df = pd.DataFrame({
'x_values': [2, 1, 4, 3, 5, 2, 6, 4],
'y_values': [5, 2, 1, 4, 3, 6, 2, 5],
'category': ['Group A', 'Group B', 'Group A', 'Group C', 'Group B', 'Group C', 'Group A', 'Group B']
})
fig, axes = plt.subplots(2, 2, figsize=(12, 8))
# Right side (outside)
sns.scatterplot(data=df, x='x_values', y='y_values', hue='category', ax=axes[0,0])
axes[0,0].legend(bbox_to_anchor=(1.05, 1), loc='upper left')
axes[0,0].set_title('Legend on Right')
# Bottom (outside)
sns.scatterplot(data=df, x='x_values', y='y_values', hue='category', ax=axes[0,1])
axes[0,1].legend(bbox_to_anchor=(0.5, -0.15), loc='upper center', ncol=3)
axes[0,1].set_title('Legend at Bottom')
# Top (outside)
sns.scatterplot(data=df, x='x_values', y='y_values', hue='category', ax=axes[1,0])
axes[1,0].legend(bbox_to_anchor=(0.5, 1.15), loc='lower center', ncol=3)
axes[1,0].set_title('Legend at Top')
# Left side (outside)
sns.scatterplot(data=df, x='x_values', y='y_values', hue='category', ax=axes[1,1])
axes[1,1].legend(bbox_to_anchor=(-0.15, 1), loc='upper right')
axes[1,1].set_title('Legend on Left')
plt.tight_layout()
plt.show()
Key Parameters
| Parameter | Purpose | Common Values |
|---|---|---|
bbox_to_anchor |
Position coordinates (x, y) | (1.05, 1) for right, (0.5, -0.15) for bottom |
loc |
Legend anchor point | 'upper left', 'center', 'lower center' |
ncol |
Number of columns | 1 (default), 2, 3 for horizontal layouts |
borderaxespad |
Padding from axes border | 0 (default), 0.5, 1 |
With Style Customization
You can also customize the legend appearance while positioning it outside ?
import matplotlib.pyplot as plt
import pandas as pd
import seaborn as sns
# Create sample data
df = pd.DataFrame({
'x_values': [1, 2, 3, 4, 5, 6, 7, 8],
'y_values': [2, 5, 3, 8, 7, 1, 6, 4],
'size_values': [50, 100, 150, 200, 120, 80, 180, 90],
'category': ['Alpha', 'Beta', 'Gamma', 'Alpha', 'Beta', 'Gamma', 'Alpha', 'Beta']
})
plt.figure(figsize=(10, 6))
# Create scatterplot with both hue and size
sns.scatterplot(data=df, x='x_values', y='y_values',
hue='category', size='size_values', sizes=(50, 200))
# Customize legend and move outside
plt.legend(bbox_to_anchor=(1.05, 1), loc='upper left',
frameon=True, fancybox=True, shadow=True)
plt.title('Scatterplot with External Legend')
plt.tight_layout()
plt.show()
Conclusion
Use bbox_to_anchor with coordinates like (1.05, 1) to position legends outside plots. Combine with plt.tight_layout() to ensure proper spacing and prevent legend cutoff.
Advertisements
