How to Customize Legends with Matplotlib
In this tutorial, we will explain how we can learn to alter the legend of matplotlib. Matplotlib is a well-known software for the visualization of data. This is a graphing library written in Python and has the NumPy extension for numerical computation.
Legend is an area on the graph that describes each element that makes up the. A graph may be straightforward in the sense that it's. If we include titles, labels for X, the Y label, and the legend, it will be clearer. When we look at the names, we are able to determine what the graph represents easily and the type of data it represents.
First, let's look at how we can create an image legend using matplotlib.
Syntax of legend in Matplotlib is:
It can be described by the following terms:
How to Customize the Legend
Legends add meaning to graph plots. The addition of the font, place of origin, and so on makes the legend more visible and easily identifiable. Let's take a look at the various programs available to alter the legends for the plots.
Sometimes the legend could be incorrect or not be placed in the correct spot. Matplotlib lets us specify the place where we would like to put the legend. This way, we can put the legend where it doesn't overshadow the plot. This means that the plots will appear more tidy and clean.
It is possible to pass it by following the steps:
To improve the look of the legend, we can also alter the size of legend. By connecting the parameter for font dimensions to the operation, we can alter the font size within the legend box in the same way as the plot title.
This can be passed as, 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'
Colour of the Legend
Sometimes we think that it would be wonderful to have the legend box coloured to make it look more appealing and help it stand out from plots. Matplotlib can also help by allowing us to change the look of legends by altering the colour of text and background and even the edges that the legend displays.
In the example above, we can clearly discern which arguments for keywords add which colour in the legend block.
Here, if we see the legend box,
The marker is put first, while the label is put in the second. The markerfirst parameter allows us to alter the location of the marker. If we set it to False, the positions of the marker and labels are swapped.
Changing the Looks:
The legend can include some of the fundamental CSS properties such as creating a shadow, putting in frames, and turning the corners, and let us make the legend transparent box if you do not want to obscure those tiny aspects of the plot by the frame.
shadow: This argument creates shadows to the legend.
frameon: Gives the frame to the legend.
fancybox: Gives smooth edges for the title.
framealpha: Provides transparency to the background of the legend.
Example 1: For adding shadow and round corners:
Example 2: Removing the frame of the legend box:
Title of the Legend:
The addition of a title to the legend is crucial to include in the legend boxes. The title parameter can let us create the legend of a title, and the title_size lets us choose a particular fontsize for the name.
title: It will give title to the legend
title_fontize: It will give size to the title.
This can be, 'xx-small', 'x-small', 'small', 'medium', 'large', 'x-large', 'xx-large'
In this tutorial, we have discussed how we can customize the legend, such as adding the title, shadow, removing the frame, changing font size and colours, etc.
JavaTpoint offers too many high quality services. Mail us on [email protected], to get more information about given services.
JavaTpoint offers college campus training on Core Java, Advance Java, .Net, Android, Hadoop, PHP, Web Technology and Python. Please mail your requirement at [email protected]
Duration: 1 week to 2 week