matplotlib学习2
2019-07-31 本文已影响2人
Yinawake
定制坐标系
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(-5, 5, 20)
y = x ** 2 + 1
plt.xticks([-4,-3,-1]) #设置X刻度
plt.yticks([2,5,10,20], #设置Y刻度
["two", "five", "ten","twenty"]) #设置Y显示文本
plt.plot(x, y)
plt.show()

图例与提示
plt.plot(x, y, 'r--x', label='base') #label
plt.plot(x+2, y, 'g--o', label= 'moved')
plt.legend(loc='lower right') #显示位置

位置包括upper right
、upper left
、lower left
,lower right
,center left
。
提示 annotate
plt.plot(x, y, 'r--x', label='base') #label
plt.plot(x+2, y, 'g--o', label= 'moved')
plt.legend(loc='lower right') #显示位置
plt.annotate('sample point', #提示文本
xy=(x[2], y[2]), #提示数据坐标
xytext = (0, 22), #提示文本位置
arrowprops= dict(facecolor='black', shrink= 0.05) ) #箭头绘制属性

arrowprops
参数内容
name | desc |
---|---|
width | 宽 |
frac | 箭头的头部占整个箭头的比列 |
headwidth | 箭头头部 宽度 |
shrink | 箭头距离数据点和本文之间的空隙 占 数据点至解释文本的距离 |
facecolor | 颜色 |
添加标题
title()
、xlabel()
、ylabel()
plt.plot(x, y, 'r--x', label='base') #label
plt.title('Easy Matplot', #标题
fontdict={'fontsize': 20}, loc = 'center')
plt.xlabel('X value', fontdict = {'fontsize': 12}) #x轴
plt.ylabel('Y value', fontdict = {'fontsize': 12}) #y轴
