python matplotlib study

2019-07-03 17:20:36 浏览数 (1)

Matplotlib是一个图形库,是以Numpy库为基础的一个库。我们主要看到的是Artist层。其结构如下图。

下面分别介绍各种图形的画法:

1.画线,

代码语言:javascript复制
import matplotlib.pyplot as plt 
import numpy as npt=np.arange(0,6.28,0.1)
t1=np.arange(0,20,0.5)
y1=np.sin(t)
#y2=np.cos(t1)
plt.title('study the matplotlib',fontsize=20,fontname='Times New Roman')
plt.plot(t1,y2,'r--')

2.画数学函数,

代码语言:javascript复制
import matplotlib.pyplot as plt
import numpy as np
t=np.arange(0,6.28,0.1)
t1=np.arange(0,20,0.5)
y1=np.sin(t)
y2=np.cos(t1)

plt.subplot(2,1,1)
plt.title('study the matplotlib',fontsize=20,fontname='Times New Roman')
plt.plot(t,y1,'b--')

plt.subplot(2,1,2)
plt.plot(t1,y2,'r--')
plt.plot(t)
plt.xlabel('the x axis value',color='red')
plt.ylabel('the y value',color='blue')
plt.text(0.8,0.8,'python_matplotlib')
plt.text(10,4,'$y=cos(x)$',fontsize=10,bbox={'facecolor':'yellow','alpha':0.9})
plt.grid(True)
plt.legend(['first series','second series'],loc=1)
plt.savefig(r'C:\Users\Administrator\PycharmProjects\chart\study_matplotlib.png')

plt.show()

3.画直方图

代码语言:javascript复制
pop=np.random.randint(0,100,100)
plt.hist(pop,bins=30)
plt.show()

4.画条状图,

代码语言:javascript复制
index=['A','B','C','D','E']
values=[5,7,3,4,7]
std1=[0.8,0.4,-.6,0.9,1.4]
plt.title('The bar chart')
plt.barh(index,values,xerr=std1,error_kw={'ecolor':'0.1','capsize':6},alpha=0.7,label='First')
plt.yticks(index,['A B','B C','C D','D E','E F'])
plt.legend(loc=4)
plt.grid()
plt.show()

5.多序列条状图(多序列水平条状图),

代码语言:javascript复制
# multiseries bar chart
series1=np.array([3,4,5,6,7])
series2=np.array([4,5,6,7,8])
series3=np.array([5,6,7,8,9])

index=np.arange(1,6)
plt.axis([0,8,0,30])
plt.bar(index,series1,color='b',hatch='xx')
plt.bar(index,series2,color='g',hatch='///',bottom=series1)
plt.bar(index,series3,color='r',hatch='\\\',bottom=(series1 series2))
plt.xticks(index 0.1,['Jan13','Feb15','Mar16','Apr19','June1'])
plt.show()

6.对比倒影条状图, y1=np.array([1,3,4,6,4,3,2,1]) y2=np.array([1,2,5,4,3,3,2,1]) plt.ylim([-7,7]) plt.bar(x0,y1,0.9,facecolor='r',edgecolor='w') plt.bar(x0,-y2,0.9,facecolor='b',edgecolor='w') plt.xticks(()) plt.grid(True) for x,y in zip(x0,y1): plt.text(x 0.2,y 0.1,'%d' %y,ha='center',va='bottom') for x,y in zip(x0,y2): plt.text(x 0.2,-y-0.5,'%d' %y,ha='center',va='bottom') plt.show()

7.画等高线图

代码语言:javascript复制
dx=0.01; dy=0.01
x=np.arange(-2,2,dx)
y=np.arange(-2,2,dy)
X,Y=np.meshgrid(x,y)
def f(x,y):
    return (1-y**5 x**5)*np.exp(-x**2-y**2)
C=plt.contour(X,Y,f(X,Y),8,colors='black')
plt.contourf(X,Y,f(X,Y),8)
plt.clabel(C,inline=1,fontsize=10)
plt.colorbar()
plt.show()

8.画多面板图形,

代码语言:javascript复制
gs=plt.GridSpec(3,3)
fig=plt.figure(figsize=(6,6))
x1=np.array([1,3,2,5])
y1=np.array([4,3,7,2])
x2=np.arange(5)
y2=np.array([3,2,4,6,4])
s1=fig.add_subplot(gs[1,:2])
s1.plot(x1,y1,'r')
s2=fig.add_subplot(gs[0,:2])
s2.bar(x2,y2)
s3=fig.add_subplot(gs[2,0])
s3.barh(x2,y2,color='g')
s4=fig.add_subplot(gs[:2,2])
s4.plot(x2,y2,'k')
s5=fig.add_subplot(gs[2,1:])
s5.plot(x1,y1,'b^',x2,y2,'yo')
plt.show()

0 人点赞