Remove the legend border in Matplotlib
0 948
Introduction
When creating plots with Matplotlib in Python, legends are often used to label different data series. However, there are scenarios where you might want to remove the legend to declutter your plot. This guide demonstrates various methods to remove the legend in Matplotlib.
Why Remove the Legend?
Legends can sometimes overlap with data points or take up valuable space in a plot. Removing the legend can make the visualization cleaner and more focused, especially when the plot is simple or when the labels are self-explanatory.
Method 1: Using legend().remove()
The remove() method can be called on the legend object to remove it from the plot. This method is straightforward and effective.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
fig, ax = plt.subplots()
ax.plot(x, y1, c='r', label='Sine')
ax.plot(x, y2, c='g', label='Cosine')
# Add legend
leg = ax.legend()
# Remove the legend
ax.get_legend().remove()
plt.show()
Method 2: Using set_visible(False)
The set_visible(False) method hides the legend without removing it entirely. This can be useful if you might want to display the legend again later.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
fig, ax = plt.subplots()
ax.plot(x, y1, c='r', label='Sine')
ax.plot(x, y2, c='g', label='Cosine')
# Add legend
leg = ax.legend()
# Hide the legend
ax.get_legend().set_visible(False)
plt.show()
Method 3: Using label='_nolegend_'
By setting the label to '_nolegend_' when plotting, Matplotlib will exclude that plot element from the legend.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
fig, ax = plt.subplots()
ax.plot(x, y1, c='r', label='_nolegend_')
ax.plot(x, y2, c='g', label='_nolegend_')
plt.show()
Method 4: Using legend_ = None
Directly setting the legend_ attribute of the axes to None removes the legend from the plot.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
fig, ax = plt.subplots()
ax.plot(x, y1, c='r', label='Sine')
ax.plot(x, y2, c='g', label='Cosine')
# Add legend
leg = ax.legend()
# Remove the legend
ax.legend_ = None
plt.show()
Method 5: Using legend([])
Calling legend([]) with an empty list effectively removes the legend from the plot.
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(-3, 3, 1000)
y1 = np.sin(x)
y2 = np.cos(x)
fig, ax = plt.subplots()
ax.plot(x, y1, c='r', label='Sine')
ax.plot(x, y2, c='g', label='Cosine')
# Remove the legend
ax.legend([])
plt.show()
Conclusion
Removing the legend in Matplotlib can be achieved through various methods, each suitable for different scenarios. Whether you choose to remove it entirely or hide it temporarily, these techniques provide flexibility in customizing your plots.
For dedicated UPSC exam preparation, we highly recommend visiting www.iasmania.com. It offers well-structured resources, current affairs, and subject-wise notes tailored specifically for aspirants. Start your journey today!
Share:


Comments
Waiting for your comments