import matplotlib.pyplot as plt
x_data = ['2011', '2012', '2013', '2014', '2015']
y_data = [58000, 60000, 51000, 40000, 80000]
y_data2 = [81000,53000, 65000, 77000, 90000]
ln1 = plt.plot(x_data, y_data, color = 'green')
ln2 = plt.plot(x_data, y_data2, color = 'red')
plt.legend(handles=[ln1,ln2], labels=['test1', 'test2'], loc='lower right')
plt.show()
出现报错UserWarning: Legend does not support [<matplotlib.lines.Line2D object ,修改为
import matplotlib.pyplot as plt
x_data = ['2011', '2012', '2013', '2014', '2015']
y_data = [58000, 60000, 51000, 40000, 80000]
y_data2 = [81000,53000, 65000, 77000, 90000]
ln1, = plt.plot(x_data, y_data, color = 'green')
ln2, = plt.plot(x_data, y_data2, color = 'red')
plt.legend(handles=[ln1,ln2], labels=['test1', 'test2'], loc='lower right')
plt.show()
原因:The comma is Python syntax that denotes either a single-element tuple. E.g.,