一. 添加图示图例 1. 代码: 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$']) l1, = plt.plot(x, y1, label='linear line') l2, = plt.plot(x, y2, color='red', linewidth=1.0, linestyle='--', label='square line') plt.legend(loc='lower right') # 图例显示在右下角 # plt.legend(handles=[l1, l2], labels=['up', 'down'], loc='best') 这是对于参数的介绍,主要有三个参数,handles是线段,labels是给线取的名字,loc是图例所在位置 # the "," is very import