Articles → Matplotlib → Legend Function In Matplotlib

Legend Function In Matplotlib






Purpose





Example


import matplotlib.pyplot as plt
import array 

arr1 = array.array('i', [1, 2, 3])
arr2 = array.array('i', [4, 5, 6])

plt.plot(arr1,arr2, label="line")
# Add a legend
plt.legend()
plt.show()




  1. Label attribute to specify the text of the legend.
  2. Legend function displays the legend.

Output


Picture showing the output of legend function in matplotlib



Locationcode Attribute




Location stringLocation code
'best'0
'upper right'1
'upper left'2
'lower left'3
'lower right'4
'right'5
'center left'6
'center right'7
'lower center'8
'upper center'9
'center'10




import matplotlib.pyplot as plt
import array 

arr1 = array.array('i', [1, 2, 3])
arr2 = array.array('i', [4, 5, 6])

plt.plot(arr1,arr2, label="line")
# Add a legend
plt.legend(loc = 4)
plt.show()




Picture showing the output of LocationCode attribute in matplotlib




import matplotlib.pyplot as plt
import array 

arr1 = array.array('i', [1, 2, 3])
arr2 = array.array('i', [4, 5, 6])

plt.plot(arr1,arr2, label="line")
# Add a legend
plt.legend(loc = (.2, .2))
plt.show()




Picture showing the output of LocationCode attribute in matplotlib



Posted By  -  Karan Gupta
 
Posted On  -  Monday, June 17, 2019

Query/Feedback


Your Email Id
 
Subject
 
Query/FeedbackCharacters remaining 250