![]() |
VOOZH | about |
In this article, you learn to customize the legend in Matplotlib. matplotlib is a popular data visualization library. It is a plotting library in Python and has its numerical extension NumPy.
Legend is an area of the graph describing each part of the graph. A graph can be as simple as it is. But adding the title, X label, Y label, and legend will be more clear. By seeing the names we can easily guess what the graph is representing and what type of data it is representing.
syntax: legend(*args, **kwargs)
This can be called as follows,
legend() -> automatically detects which element to show. It does this by displaying all plots that have been labeled with the label keyword argument.
legend(labels) -> Name of X and name of Y that is displayed on the legend
legend(handles, labels) -> A list of lines that should be added to the legend. Using handles and labels together can give full control of what should be displayed in the legend. The length of the legend and handles should be the same.
Plots on a graph gain insight from legends. The legend is made more readable and recognizable by the addition of the font, origin, and other details. Let's explore the several methods that can be used to change the plot legends.
Create a simple plot with two curves, these curves, adds labels, displays a legend, and finally shows the legends with Matplotlib.
Output:
👁 Screenshot-2023-12-05-230047
Matplotlib in Python to create a plot with two curves representing the sine and cosine functions. It customizes the plot by assigning colors to each curve, setting a legend with a title and specific colors, and adding a title to the plot along with labels for the x and y axes.
Sometimes the legend may or may not be in the appropriate place. In matplotlib, we can also add the location where we want to place it. With this flexibility, we can place the legend somewhere where it does not overlay the plots, and hence the plots will look much cleaner and tidier.
Syntax: legend(loc='')
It can be passed as follows,
'upper left', 'upper right', 'lower left', 'lower right'-> It is placed on the corresponding corner of the plot.
'upper center', 'lower center', 'center left', 'center right' -> It is placed on the center of corresponding edge.
'center' -> It is placed exact center of the plot.
'best' -> It is placed without the overlapping of the artists
Adding a title to the legend will be an important aspect to add to the legend box. The title parameter will let us give a title for the legend and the title_size let us assign a specific fontsize for the title.
Syntax:
legend(title=”” , title_fontsize=”)
It can be, ‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’
Output:
👁 Screenshot-2023-12-05-230457
To make the legend more appealing we can also change the font size of the legend, by passing the parameter font size to the function we can change the fontsize inside the legend box just like the plot titles.
Syntax: legend(fontsize=”)
It can be passed as, ‘xx-small’, ‘x-small’, ‘small’, ‘medium’, ‘large’, ‘x-large’, ‘xx-large’
Output:
👁 Screenshot-2023-12-05-230614
Sometimes we can feel that it would be great if the legend box was filled with some color to make it more attractive and makes the legends stand out from the plots. Matplotlib also covers this by letting us change the theme of the legend by changing the background, text, and even the edge color of the legend.-+
Syntax:
legend(labelcolor=”)
labelcolor is used to change the color of the text.
legend(facecolor=”)
facecolor is used to change background color of the legend.
legend(edgecolor=”)
edgecolor is used to change the edge color of the legend
Output:
👁 Screenshot-2023-12-05-230728
The legend is customized using the Line2D class to create custom legend handles for the different line styles and colors used in the plot. The resulting legend includes a title, as well as labels for the x and y axes, entries for a dashed line and a dash-dot line, each represented by a custom line with a specified color and linestyle.
Output:
👁 Screenshot-2023-12-05-230858
Custom legend positioned in the upper-right corner with labels for the sine and cosine functions and a title. The overall plot is given a title ('Sine and Cosine Functions in Specific Axes') and labeled x and y axes.
Output:
👁 Screenshot-2023-12-05-231010
Syntax: legend(markerfirst = bool, default: True)
By default, the marker is placed first and the label is placed second. marker first parameter is used to change the position of the marker. By making it False, the marker and labels places will be swapped.
Output:
👁 Screenshot-2023-12-05-235646
The legend is customized to use specific symbols for each line and adjusted line widths. The resulting plot has a title, labels for the x and y axes, and a legend in the upper-right corner with entries for the sine and cosine functions, each indicating its line style and width.
Output:
👁 Screenshot-2023-12-05-235705
We can make the legend have some of the basic CSS properties like adding a shadow, adding a frame and making the corners round, and also let us add transparency to the legend box if you don’t want to cover those small details in the plot by a frame.
shadow -> This argument gives shadow behind the legend.
frameon -> Gives the frame to legend.
fancybox -> Gives round edges to the legend.
framealpha -> Gives transparency to legend background.
Output:
👁 Screenshot-2023-12-05-235721
The legend is customized to have colored markers corresponding to each curve. The resulting plot has a title, labels for the x and y axes, and a legend in the upper-right corner with entries for the sine and cosine functions, each represented by a marker with a specified color.