Methods to Remove Legends in Seaborn
- Method 1: Using
legend=False
- Method 2: Using
ax.legend_.remove()
- Method 3: Using
ax.get_legend().remove()
- Method 4: Using
plt.legend().remove()
Method 1: Using legend=False
The legend is not displayed when legend=False is set explicitly in the Seaborn plotting function.
import seaborn as sns
import matplotlib.pyplot as plt
# Load the Iris dataset
iris = sns.load_dataset("iris")
# Create a figure with two subplots side by side
fig, axes = plt.subplots(1, 2, figsize=(16, 6))
# Plot 1: Histplot with legend
sns.histplot(data=iris, x="sepal_length", hue="species", multiple="stack",ax=axes[0])
axes[0].set_title('Histplot with Legend')
# Plot 2: Histplot without legend using legend=False
sns.histplot(data=iris, x="sepal_length", hue="species", multiple="stack", legend=False, ax=axes[1])
axes[1].set_title('Histplot without Legend')
plt.tight_layout()
plt.show()
Output:
Method 2: Using ax.legend_.remove()
After plotting, you can use the remove() function to remove the legend object linked to the axes.
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")
fig, axes = plt.subplots(1, 2, figsize=(16, 6))
# Plot 1: Scatter plot with legend
sns.scatterplot(data=iris, x="sepal_length", y="sepal_width", hue="species", ax=axes[0])
axes[0].set_title('Scatter Plot with Legend')
# Plot 2: Scatter plot without legend
sns.scatterplot(data=iris, x="sepal_length", y="sepal_width", hue="species", ax=axes[1])
# Remove the legend
axes[1].legend_.remove()
axes[1].set_title('Scatter Plot without Legend')
plt.tight_layout()
plt.show()
Output:
Method 3: Using ax.get_legend().remove()
This method uses the remove() function to retrieve and delete the legend linked to the axes, just like method 2.
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")
fig, axes = plt.subplots(1, 2, figsize=(16, 6))
# Plot 1: Histplot with legend
sns.histplot(data=iris, x="sepal_length", hue="species", multiple="stack", ax=axes[0])
axes[0].set_title('Histplot with Legend')
# Plot 2: Histplot and then remove legend using ax.get_legend().remove()
sns.histplot(data=iris, x="sepal_length", hue="species", multiple="stack", ax=axes[1])
axes[1].get_legend().remove()
axes[1].set_title('Histplot without Legend')
plt.tight_layout()
plt.show()
Output:
Method 4: Using plt.legend().remove()
This function uses plt.legend() to directly access the current plot’s legend, and then it uses the remove() method to delete it.
import seaborn as sns
import matplotlib.pyplot as plt
iris = sns.load_dataset("iris")
fig, axes = plt.subplots(1, 2, figsize=(16, 6))
# Plot 1: Scatter plot with legend
sns.scatterplot(data=iris, x="sepal_length", y="sepal_width", hue="species", ax=axes[0])
axes[0].set_title('Scatter Plot with Legend')
# Plot 2: Scatter plot without legend
sns.scatterplot(data=iris, x="sepal_length", y="sepal_width", hue="species", ax=axes[1])
plt.legend().remove() # Remove the legend
axes[1].set_title('Scatter Plot without Legend')
plt.tight_layout()
plt.show()
Output:
How To Remove Legend From The Figure In Seaborn?
A legend in a plot can be said as a guide that describes the key elements of the plot. It describes the symbols, colors, or patterns used to denote various datasets or categories. It helps accurately comprehend the visual data and provides crucial context. On the other hand, in certain situations—like when we want the plot to be simple or when the legend is overlapping important content in the graph, we want the legend to be removed. So, for removing the legend, in this article, we will see various techniques to eliminate the legend from a plot.
Table of Content
- Methods to Remove Legends in Seaborn
- Method 1: Using legend=False
- Method 2: Using ax.legend_.remove()
- Method 3: Using ax.get_legend().remove()
- Method 4: Using plt.legend().remove()
- Performance Considerations when Removing Legends in Seaborn