Matplotlib

### 一:基本知识

1. 第一层是底层的容器层,主要包括Canvas(画板)、Figure(画布)、Axes(绘画区)

2. 第二层是辅助显示层,主要包括Axis(坐标)、Spines(边框线)、Tick(刻度)、Grid(网格)、Legend(图例)、Title(标题)等

3. 第三层为图像层,即通过plot(折线)、hist(直方图)、contour、bar(条形图)、barbs、scatter(散点),pie(饼图)等方法绘制的图像。

##### 基本概念

![image](https://wx4.sinaimg.cn/mw690/005EpKr8ly1g1dslzabgzj30gd0fnwfb.jpg)

##### 绘图参数《一》

![image](https://wx1.sinaimg.cn/mw690/005EpKr8ly1g1dslza6zmj30dz0hkjrw.jpg)

##### 绘图参数《二》

![image](https://wx1.sinaimg.cn/mw690/005EpKr8ly1g1dslzbha2j30ej04mmx1.jpg)

### 二.案例

```

import matplotlib.pyplotasplt

importnumpyasnp

fig=plt.figure()

fig.set_figwidth(1200)

########饼图############

axes=fig.add_subplot(2,3,1)

labels='A','B','C','D'

fracs=[15,30,45,10]

axes.set_aspect(aspect=1)#plt.axes(aspect=1)使x y轴比例相同

explode=[0,0.05,0,0]#突出某一部分区域

axes.pie(x=fracs,labels=labels,autopct='%.0f%%',explode=explode)#autopct显示百分比

########线图###########

axes=fig.add_subplot(2,3,2)

x=np.arange(1,5)

y=x*2

axes.plot(x,y)

axes.grid(True,color='g',linestyle='--',linewidth='1')

#########多条线#########

axes=fig.add_subplot(2,3,3)

x=np.arange(1,11,1)

axes.plot(x,x*2)

axes.plot(x,x*3)

axes.plot(x,x*4)

axes.legend(['Normal','Fast','Faster'])

########散点图##########

axes=fig.add_subplot(2,3,4)

x=np.random.randn(1000)

y=x+np.random.randn(1000)*0.5

axes.scatter(x,y,s=5,marker='<')# s表示面积,marker表示图形

#########条形图##########

axes=fig.add_subplot(2,3,5)

y=[20,10,40,25,15]

index=np.arange(5)

axes.bar(left=index,height=y,color='green')

#########箱形图##########

axes=fig.add_subplot(2,3,6)

data=np.random.normal(size=(1000,4),loc=0,scale=1)#标准整体分布

labels=['A','B','C','D']

axes.boxplot(data,labels=labels)

plt.show()

#########直方图##########

mu=100

sigma=20

x=mu+sigma*np.random.randn(20000)#样本数量

plt.hist(x,bins=100,color='green',normed=True)# bins显示有几个直方,normed是否对数据进行标准化

plt.show()

#########等高线##########

#定义等高线图的横纵坐标x,y

#从左边取值为从-3到3,各取5个点,一共取5*5 = 25个点

x=np.linspace(-3,3,5)

y=np.linspace(-3,3,5)

#将原始数据变成网格数据

X, Y=np.meshgrid(x, y)

#各地点对应的高度数据

#Height是个5*5的数组,记录地图上25个点的高度汇总

Height= [[0,0,1,2,2],[0,-2,-2,1,5],[4,2,6,8,1],[3,-3,-3,0,5],[1,-5,-2,0,3]]

#填充颜色

plt.contourf(X, Y, Height,10,alpha=0.6,cmap=plt.cm.hot)

#绘制等高线

C=plt.contour(X, Y, Height,10,colors='black',linewidth=0.5)

#显示各等高线的数据标签

plt.clabel(C,inline=True,fontsize=10)

plt.show()

```

![image](https://wx2.sinaimg.cn/mw690/005EpKr8ly1g1dslza9qrj30hs0dcab2.jpg)

figure布局

![image](https://wx1.sinaimg.cn/mw690/005EpKr8ly1g1dslz9qg1j30hs0dcdfn.jpg)

figure图2

![image](https://wx1.sinaimg.cn/mw690/005EpKr8ly1g1dslzaknuj30hs0dcq4n.jpg)

figure图3

### 三.自定义函数图形

```

importmatplotlib.pyplotasplt

importnumpyasnp

x=np.linspace(-3,3,50)

y1=2*x+1

y2=x**2

# plt.figure()

# plt.plot(x,y1)

plt.figure()

l1=plt.plot(x,y2,label='up')

l2=plt.plot(x,y1,color='red',linewidth=1.0,linestyle='--',label='down')

plt.xlim((-1,2))

plt.ylim((-3,5))

plt.ylabel('I am y')

plt.xlabel('I am x')

new_ticks=np.linspace(-1,2,5)

print(new_ticks)

plt.xticks(new_ticks)

plt.yticks([-2,-1.8,-1,1,3],[r'$re\ bad$',r'$re2\alpha$',r'$re3$','re4','re5'])

ax=plt.gca()

ax.spines['right'].set_color('none')

ax.spines['top'].set_color('none')

#ax.xaxis.set_ticks_position('bottom')

#ax.yaxis.set_ticks_position('left')

ax.spines['bottom'].set_position(('data',0))

ax.spines['left'].set_position(('data',0))

plt.legend()

#plt.legend(handles=[l1,],labels=['aa',],loc='best')

plt.show()

```

![image](https://wx3.sinaimg.cn/mw690/005EpKr8ly1g1dslzamsej30hs0dcq3b.jpg)

你可能感兴趣的:(Matplotlib)