开发者学堂课程【Python 常用数据科学库:子图与标注】学习笔记,与课程紧密联系,让用户快速学习知识。
课程地址:https://developer.aliyun.com/learning/course/546/detail/7497
子图与标注
内容介绍
一、绘制多条线
二、指定线条宽度
三、marker小圆点
四、子图
五、在图上加注释
一、绘制多条线
可以画多条线,x是数组
In [19]: tang_numpy = np.arange(0,10,0.5)
plt.plot (tang_numpy,tang_numpy,’ r- -’)
plt.plot(tang_numpy,tang_numpy**2,’bs’)
plt.plot(tang_numpy,tang_numpy**3,’go’)
Out [19]: [<matplotlib.lines.Line2D at 0x212f82109b0>]
一条一条地画出了三条线
也可以一起画三条线:
In [22]: plt.plot tang_numpy,tang_numpy,’ r- -’
tang_numpy,tang_numpy**2,’bs’
tang_numpy,tang_numpy**3,’go’
Out [22]: [<matplotlib.lines.Line2D at 0x212f930acf8>]
<matplotlib.lines.Line2D at 0x212f930af98>
<matplotlib.lines.Line2D at 0x212f930f828>
二、指定线条宽度
In [23]: x = np.linspace(-10,10)
y = sin(x)
plt.plot(x,y)
Out [25]: [<matlotlib.lines.Line2D at 0x212f9386060f0>]
画出来了抛物线
可以指定线条粗细:
In [23]: x = np.linspace(-10,10)
y = sin(x)
plt.plot(x,y,linwidth = 2.0)
Out [25]: [<matlotlib.lines.Line2D at 0x212f9386060f0>]
三、marker小圆点
指定颜色、线条格式 linestyle 与 marker 标志小圆点:
In [29]: plt.pot (x,y,color=‘ b’,linestyle=‘:’,marker = ‘o’)
Out [29]: [<matlotlib.lines.Line2D at 0x212f7dedcf8>]
指定小圆点颜色为红色:
In [30]: plt.pot (x,y,color=‘ b’,linestyle=‘:’,marker = ‘o’,
markerfacecolor = ‘r’)
Out [30]: [<matlotlib.lines.Line2D at 0x212f7dedcf8>]
指定小圆点大小 size 变大为10:
In [29]: plt.pot (x,y,color=‘ b’,linestyle=‘:’,marker = ‘o’,
markerfacecolor = ‘r’,markersize = 10)
Out [29]: [<matlotlib.lines.Line2D at 0x212f7dedcf8>]
画图指定 line,直接指定参数(透明程度 alpha):
In [34]: line = plt.plot(x,y)
Plt.step line.color=‘ r’,linesidth = 2.0,alpha = 0.5
Out [34]: [None,None,None]
先画线,再设置参数风格
四、子图
画制多个图(三围参数211):
In [39]: plt.subplot 211
plt.plot(x,y,color = ‘ r’)
plt.subplot(212)
plt.plot x,y,color = ‘ b’
Out [39]: [<matlotlib.lines.Line2D at 0x212f7dedcf8>]
就直接画出来上下两个图
解释211与212:
In [39]: #211 表示一会要画的图是2行1列的,最后一个1表示的是子图当中的第一个图
plt.subplot (211)
plt.plot(x,y,color = ‘ r’)
#212 表示一会要画的图是2行1列的,最后一个11表示的是子图当中第1个图
plt.subplot(212)
plt.plot x,y,color = ‘ b’
Out [39]: [<matlotlib.lines.Line2D at 0x212f7dedcf8>]
绘制三行两列的图:
In [39]: #321 表示一会要画的图是3行2列的,最后一个1表示的是子图当中的第一个图
plt.subplot (321)
plt.plot(x,y,color = ‘ r’)
#324 表示一会要画的图是3行2列的,最后一个4表示的是子图当中第2个图
plt.subplot(324)
plt.plot x,y,color = ‘ b’
Out [39]: [<matlotlib.lines.Line2D at 0x212f7dedcf8>]
五、在图上加注释
In [44]: plt.pot (x,y,color=‘ b’,linestyle=‘:’,marker = ‘o’,
markerfacecolor = ‘r’,markersize = 10)
plt.xlabel(’x:- - -’)
plt.ylabel(’y:- - -’)
plt.title(’tang yu di:- - -’)
plt.text(0,0,’tang yu di’)
plt.grid:True
plt.annotate (’tangyudi’,xy (-5,0),xytext(2,0,5) ),
arrowprops = dict(face color=‘black’,shrink = 0.05)
Out [44]: [<matlotlib.lines.Line2D at 0x212f7dedcf8>]
加上了文字注释、格子、注解箭头、表头