本文介紹matplotlib 中的 legend 圖例耳峦,就是為了幫我們展示出每個(gè)數(shù)據(jù)對(duì)應(yīng)的圖像名稱. 更好的讓讀者認(rèn)識(shí)到你的數(shù)據(jù)結(jié)構(gòu).
Demo.py
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-3, 3, 50)
y1 = 2*x + 1
y2 = x**2
plt.figure()
#set x limits
plt.xlim((-1, 2))
plt.ylim((-2, 3))
# set new sticks
new_sticks = np.linspace(-1, 2, 5)
plt.xticks(new_sticks)
# set tick labels
plt.yticks([-2, -1.8, -1, 1.22, 3],
[r'$really\ bad$', r'$bad$', r'$normal$', r'$good$', r'$really\ good$'])
# set line syles
l1, = plt.plot(x, y2, label='linear line')
l2, = plt.plot(x, y1, color='red', linewidth=1.0, linestyle='--', label='square line')
#plt.legend(loc='upper right')
# 設(shè)置legend
plt.legend(handles = [l1, l2,], labels = ['a', 'b'], loc = 'best')
plt.show()
結(jié)果: