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
Python3
# Import libraries import matplotlib.pyplot as plt # Creating plot plt.plot([ 1 , 2 , 3 , 4 ]) plt.title( 'simple legend example ' ) # Creating legend plt.legend([ 'simple legend example' ]) # Show plot plt.show() |
Output:
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:
Python3
# Import libraries import matplotlib.patches as mpatches import matplotlib.pyplot as plt # Creating plot plt.plot([ 1 , 2 , 3 , 4 ], color = 'blue' ) plt.title( 'simple legend example ' ) # Creating legend with color box blue_patch = mpatches.Patch(color = 'blue' , label = 'blue legend' ) plt.legend(handles = [blue_patch]) # Show plot plt.show() |
Output:
Example 2:
Python3
# Import libraries import matplotlib.pyplot as plt import matplotlib.patches as mpatches import scipy.stats import numpy as np # Setting limit for x values x_min = 0.0 x_max = 50.0 # Setting figure size fig = plt.figure(figsize = ( 16 , 9 )) # Creating first dataset mean = 12.0 std = 3.0 x = np.linspace(x_min, x_max, 100 ) y = scipy.stats.norm.pdf(x,mean,std) # Plotting first dataset plt.plot(x,y, color = 'red' ) plt.fill_between(x, y, color = '#CE5D45' , alpha = 1.0 ) # Creating second dataset mean = 18.0 std = 6.0 x = np.linspace(x_min, x_max, 100 ) y = scipy.stats.norm.pdf(x,mean,std) # Plotting second dataset plt.plot(x,y, color = 'green' ) plt.fill_between(x, y, color = '#5DC83F' , alpha = 1.0 ) # Creating legend with color box pop_a = mpatches.Patch(color = '#5DC83F' , label = 'Population Dataset 1' ) pop_b = mpatches.Patch(color = '#CE5D45' , label = 'Population Dataset 2' ) plt.legend(handles = [pop_a,pop_b]) # Adding grid to plot plt.grid() # Adding X and Y limits plt.xlim(x_min,x_max) plt.ylim( 0 , 0.25 ) # Adding title plt.title( 'simple legend example' ,fontsize = 12 ) # Adding labels plt.xlabel('') plt.ylabel( 'Probability Distribution' ) # Showing plot plt.show() |
Output: