1、绘制图例
# 该函数具有返回值
plt.plot(x, y2, label = 'up')
plt.plot(x, y1, color = 'red', linewidth = 1.0, linestyle = '--', lable = 'down')
# 绘制图例
plt.Legend()
plt.show()
l1, = plt.plot(x, y2, label = 'up')
l2, = plt.plot(x, y1, color = 'red', linewidth = 1.0, linestyle = '--', lable = 'down')
# 绘制个性化图例
plt.Legend(handles = [l1, l2],label = ['new', 'name'],loc = 'best')
2、在图片中添加注释
# 绘制图像
# 添加注释点
x0 = 1
y0 = 2*x0 + 1
# 绘制出改点
plt.scatter(x0,y0,s = 50, color = 'b')
plt.plot([x0,x0], [y0,0], 'k--',lw = 2.5)
# 注释方式1,从哪一点开始注释xy = (x0,y0),基于xy点正负30打印文本
# xycoords = 'data',xy的坐标是基于该斜线data的xycoords = 'data'
plt.annotate(r'$2x + 1 = %s$'% y0, xy = (x0,y0), xycoords = 'data',xytext = (+30, -30), textcoords = 'offset points', fontsize = 16, arrowprops = dict(arrowstyle = '->', connectionstyle = 'arc3, rad = .2'))
# 注释方法2
plt.text(-3,7, 3, r'$This\ is\ the\ some\ text.\ \mu\ \sigma_i$',
fontdict = {'size':16, 'color': 'r'})
3.tick能见度(坐标轴被挡问题)
# 把坐标轴数值的label参数拿出来重新设置
for label in ax.get_xticklabels() + ax.get_yticklabels():
label.set_fontsize(12)
# bbox的背景是白色,边框没有,透明度为0.7
label.set_bbox(dict(facecolor = 'white', edgecolor = 'None', alpha = 0.7))