Matplotlib.axes.Axes.legend() in PythonLegend() is the part of the matplotlib library that creates the box-like structure on the plot, which describes the information about the elements present. Syntax
Not Specifying any Labels And Calling the legend() FunctionIf we use the legend() function without specifying the labels of the elements of the graph, it will not create any box-like structure on the graph. ExampleHere is a clear example of calling legend() without specifying the labels: Program Output: Explanation If we call the legend() function without specifying the labels, it will create the plot without any legend. Elements to be Displayed in the Legend are Automatically DetectedExampleLet us consider an example program: Program Output: Explanation We have defined the x, y1 and y2 points in this example to plot a graph. Then, we used the legend() method to add labels about the axes present on the graph. Instead of adding the labels with a separate function, we used the plot function. The legend() function takes the labels defined while plotting as input. Passing Additional ParametersExampleHere is an example program: Program Output: Explanation In this example, we have defined the x, y1 and y2 points to plot the graph then we used the legend() method to add labels about the axes present on the graph for that, we used the legend() function with the loc, fontsize, and title parameters to customize the appearance and position of the legend here we are not adding the labels directly in the function instead legend() function takes the labels defined while plotting. Setting Labels Using set_labelSetting labels using set_label will add the label to the axes present on the graph, and if we use the legend() function without passing the parameter, it will detect the labels set by the set_label function and create the legend with the labels. ExampleLet us consider an example program. Program Output: Explanation In this example, we imported the required library and then defined the data points to plot with the help of the plot() function. We plotted each data, then used the set_lebel() method for each line, and the legend function detected the labels and created the legend. Finally, we displayed the plot using the show() method. legend() With Scatter PlotExampleHere is an example program: Program Output: Explanation In this example, we have two sets of data points named y1 and y2, respectively, that are plotted against the same set of x-values. The scatter() function creates scatter plots for each dataset, while the label parameter specifies each label. Finally, the legend() function is called without any arguments to automatically include the labels specified in the scatter function. The resulting legend will display the labels and their corresponding marker styles and colours. Customizing the Position of the LegendTo change the legend's position in a plot using Matplotlib, you can use the loc parameter of the legend function. The loc parameter accepts different string values representing various positions for the legend. Some examples of common values for the loc parameter are:
ExampleHere is an example program to set the legend position to the lower left: Program Output: Explanation Here, we have specified the position of the legend as upper right using the loc parameter in the legend function. Placing the Legend Outside the Plot in MatplotlibWe can use the bbox_to_anchor parameter with the loc parameter to place the legend outside the plot. ExampleLet us see an example program to place the legend outside the plot. Program Output: Explanation In the provided example, the parameter bbox_to_anchor has been set to (1,0.5), indicating that the legend has been placed on the right side of the plot. You can modify the values of bbox_to_anchor to change the legend's position. ConclusionIn conclusion, the `legend()` function in Matplotlib is a powerful tool for enhancing the interpretability of plots by providing labels for various elements within a graph. It is extensively employed to differentiate between disparate data series and is particularly advantageous when multiple lines, markers, or other plot elements are present. The function provides a straightforward means of incorporating a legend, with default positioning that minimally disrupts the plot. Additionally, users can effortlessly customize the legend's location, font size, frame appearance, and background colour to meet specific visualization requirements. Regardless of whether the legend is positioned within or outside the plot, the `legend()` function offers flexibility and versatility, thereby contributing to the overall clarity and communicative efficacy of Matplotlib visualizations. Next TopicMatplotlib axes axes plot in python |
We provides tutorials and interview questions of all technology like java tutorial, android, java frameworks
G-13, 2nd Floor, Sec-3, Noida, UP, 201301, India