In this article, we will learn how to Change the legend position in Matplotlib. Let’s discuss some concepts :
- Matplotlib is a tremendous visualization library in Python for 2D plots of arrays. Matplotlib may be a multi-platform data visualization library built on NumPy arrays and designed to figure with the broader SciPy stack. It had been introduced by John Hunter within the year 2002.
- A legend is an area describing the elements of the graph. In the matplotlib library, there’s a function called legend() which is used to Place a legend on the axes.
- The attribute Loc in legend() is used to specify the location of the legend.Default value of loc is loc=”best” (upper left). The strings best upper right, upper left, lower left, lower right, right, center left, center right, lower center, upper center, and center place the legend at the corresponding corner of the axes/figure.
|Location String||Location String|
- Import Library (Matplotlib)
- Import / create data.
- Plot a chart.
- Add legend.
- Set position of legend using loc.
Attention geek! Strengthen your foundations with the Python Programming Foundation Course and learn the basics.
To begin with, your interview preparations Enhance your Data Structures concepts with the Python DS Course.