VOOZH about

URL: https://www.geeksforgeeks.org/python/how-to-manually-add-a-legend-with-a-color-box-on-a-matplotlib-figure/

⇱ How to manually add a legend with a color box on a Matplotlib figure ? - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

How to manually add a legend with a color box on a Matplotlib figure ?

Last Updated : 23 Dec, 2020

A legend is basically an area in the plot which describes the elements present in the graph. Matplotlib provides an inbuilt method named legend() for this purpose. The syntax of the method is below :

Example: Adding Simple legend

Output:

👁 Image

Creating legend with color box

To create a legend with a color box, patches are used provided by the matplotlib.patches module. A patch nothing but a 2D artist with face color and edge color. Below is a simple example of this:

Example 1:

Output:

👁 Image

Example 2:

Output:

👁 Image
Comment