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 add a legend on Seaborn facetgrid bar plot using Matplotlib?
Adding a legend to a Seaborn FacetGrid bar plot requires using map_dataframe() with the plotting function and then calling add_legend(). This approach works with various plot types including bar plots, scatter plots, and line plots.
Basic FacetGrid with Legend
Let's start with a simple example using sample data ?
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Set figure parameters
plt.rcParams["figure.figsize"] = [10, 4]
plt.rcParams["figure.autolayout"] = True
# Create sample data
df = pd.DataFrame({
'category': ['A', 'B', 'C', 'A', 'B', 'C'],
'value': [3, 7, 8, 5, 6, 4],
'group': ['X', 'X', 'X', 'Y', 'Y', 'Y']
})
# Create FacetGrid
g = sns.FacetGrid(df, col="group", hue="category", height=4)
g.map_dataframe(sns.barplot, x="category", y="value")
g.add_legend(title="Category")
plt.show()
FacetGrid with Scatter Plot and Legend
Here's the original example with proper data structure ?
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
plt.rcParams["figure.figsize"] = [7.50, 3.50]
plt.rcParams["figure.autolayout"] = True
# Create more meaningful data
df = pd.DataFrame({
'col1': ['Group1', 'Group2', 'Group3'],
'x_val': [1, 2, 3],
'y_val': [3, 7, 8]
})
g = sns.FacetGrid(df, col="col1", hue="col1")
g.map_dataframe(sns.scatterplot, x="x_val", y="y_val")
g.set_axis_labels("X", "Y")
g.add_legend()
plt.show()
Advanced Example with Bar Plot
For a more practical bar plot example with multiple categories ?
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Create realistic dataset
data = pd.DataFrame({
'region': ['North', 'South', 'East', 'West'] * 3,
'quarter': ['Q1', 'Q1', 'Q1', 'Q1', 'Q2', 'Q2', 'Q2', 'Q2', 'Q3', 'Q3', 'Q3', 'Q3'],
'sales': [100, 120, 80, 95, 110, 130, 85, 100, 105, 125, 90, 110],
'product': ['A', 'A', 'A', 'A', 'B', 'B', 'B', 'B', 'C', 'C', 'C', 'C']
})
# Create FacetGrid with bar plot
g = sns.FacetGrid(data, col="quarter", hue="product", height=4, aspect=0.8)
g.map_dataframe(sns.barplot, x="region", y="sales")
g.add_legend(title="Product", loc='upper right')
g.set_axis_labels("Region", "Sales")
plt.show()
Key Points
- map_dataframe() − Use this method to apply plotting functions to the FacetGrid
- add_legend() − Adds a legend based on the hue parameter
- hue parameter − Defines which column to use for color coding and legend
-
Legend positioning − Use
locparameter inadd_legend()to control placement
Legend Customization Options
import pandas as pd
import seaborn as sns
import matplotlib.pyplot as plt
# Sample data
df = pd.DataFrame({
'x': [1, 2, 3, 1, 2, 3],
'y': [2, 4, 6, 3, 5, 7],
'category': ['A', 'A', 'A', 'B', 'B', 'B'],
'group': ['Type1', 'Type1', 'Type1', 'Type2', 'Type2', 'Type2']
})
g = sns.FacetGrid(df, col="group", hue="category", height=4)
g.map_dataframe(sns.barplot, x="category", y="y")
# Customize legend
g.add_legend(title="Categories", bbox_to_anchor=(1.25, 0.5), loc='center left')
plt.show()
Conclusion
Use map_dataframe() with your desired plotting function and add_legend() to add legends to FacetGrid plots. The hue parameter determines what gets color-coded and included in the legend.
Advertisements
