To display a legend with Matplotlib, you can use the plt.legend()
function after plotting your data. This function takes an optional labels
parameter where you can specify the names of the labels you want to display in the legend. You can also customize the location of the legend using the loc
parameter, which takes values such as "upper right", "lower left", "center", etc. Additionally, you can adjust the size and styling of the legend using parameters like fontsize
, title
, and shadow
. Legends are useful for identifying different elements within a plot, such as lines, markers, or colors, and can enhance the understanding of your data visualizations.
How to add a legend without labels in matplotlib?
You can add a legend without labels in matplotlib by creating a custom legend handler function and specifying it when creating the legend. Here is an example code snippet to do this:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 |
import matplotlib.pyplot as plt import matplotlib.lines as mlines # Create a custom legend handler function def no_label_legend(color, linestyle): line = mlines.Line2D([], [], color=color, linestyle=linestyle) return line # Plot some data plt.plot([1, 2, 3], label='Line 1', color='blue') plt.plot([3, 2, 1], label='Line 2', color='red', linestyle='dashed') # Add legend without labels plt.legend(handles=[no_label_legend('blue', '-'), no_label_legend('red', '--')], loc='upper left') plt.show() |
In this code snippet, the no_label_legend
function creates a custom legend handler that simply creates a colored line with the specified color and linestyle. This function is then used when creating the legend by passing the desired color and linestyle for each line. This will create a legend with colored lines but without any labels.
How to hide a legend in matplotlib?
To hide the legend in a matplotlib plot, you can simply call the legend()
function with no arguments. Here's an example:
1 2 3 4 5 6 7 8 9 10 11 12 13 |
import matplotlib.pyplot as plt # Create some data x = [1, 2, 3, 4, 5] y = [10, 15, 13, 18, 16] # Plot the data plt.plot(x, y) # Hide the legend plt.legend() plt.show() |
This will plot the data without displaying a legend.
How to create a custom legend handler in matplotlib?
To create a custom legend handler in matplotlib, you can use the HandlerBase
class to define a new handler function. Here is an example of how you can create a custom legend handler for a scatter plot:
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 |
import matplotlib.pyplot as plt from matplotlib.legend_handler import HandlerBase class CustomLegendHandler(HandlerBase): def create_artists(self, legend, orig_handle, xdescent, ydescent, width, height, fontsize, trans): size = orig_handle.get_sizes()[0] marker = orig_handle.get_paths()[0] handle = plt.Line2D([0],[0], marker=marker, markersize=size, color='black') return [handle] # Create a scatter plot x = [1, 2, 3, 4, 5] y = [2, 3, 4, 5, 6] sizes = [20, 40, 60, 80, 100] paths = [plt.Circle((0, 0), s) for s in sizes] fig, ax = plt.subplots() sc = ax.scatter(x, y, s=sizes, marker=paths) # Create custom legend handler plt.legend([sc], ['Data'], handler_map={type(sc): CustomLegendHandler()}) plt.show() |
In this example, we create a custom legend handler CustomLegendHandler
that creates a Line2D object with the same marker shape and size as the original scatter plot. We then use this custom legend handler when creating the legend for the scatter plot.
You can customize the create_artists
method in the CustomLegendHandler
class to create different types of artists for your legend, depending on your specific requirements.