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()
- Label attribute to specify the text of the legend.
- Legend function displays the legend.
Output
Locationcode Attribute
Location string | Location 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()
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()