Mastering Data Visualization Techniques in Data Science
Matplotlib Pyplot Legend
Matplotlib is the package used for data visualization and is one of the most popular packages in python. As the name suggests Matplotlib works like MATLAB. Pyplot is a cluster of functions that makes matplotlib like MATLAB.
Each pyplot function takes is used to make some changes to a figure e.g., plotting some line in an area, decorating the plot with labels, creating a figure, etc.
Creating visualizations with pyplot is quite easy
Matplotlib.pyplot.legend()
A legend is used to describe elements for a particular area of a graph. Python has a function called legend() which is used to place a legend on the axis.
The legend function has an attribute called loc which is used to denote the location of the legend. The default value of attribute loc is upper left. Other string, such as upper right, lower left, lower right can also be used.
Legend can also be placed at a particular index also there is an attribute named bbox_to_anchor which takes a coordinate (x, y) as input and places the legend at the particular coordinate another attribute ncol can be used to denote the number of columns in the legend.
A few more attributes of legend function are:
- fontsize: This is used to denote the font of the legend. It has a numeric value.
- facecolor: This is used to denote the legend’s background color.
- edgecolor: This is used to denote the legends patch edge color.
- shadow: It is a boolean variable to denote whether to display shadow behind the legend or not.
- title: This attribute denotes the legend’s title.
- numpoints: The number of marker points in the legend when creating an entry for a Line2D (line).