Python-matplotlib入门--基础图表的绘制

来源:互联网 发布:知乎,京九高铁 编辑:程序博客网 时间:2024/05/16 10:57

生而为人,请务必善良

matplotlib 实现数据可视化

前提:安装matplotlib库、numpy库及pandas库

实例目录:

1、线性图
2、直方图
3、条状图
4、多序列条状图
5、饼状图
6、极坐标图
7、散点图
8、3D曲面
9、3D散点
10、动画

基础图表实例

1、线性图:

为pandas数据结构绘制线性图

这里写图片描述

import numpy as npimport matplotlib.pyplot as pltimport pandas as pddata = {'series1':[1,2,3,4,5],        'series2':[2,3,4,5,6],        'series3':[3,4,5,6,7],        }df = pd.DataFrame(data)x = np.arange(5)plt.axis([0,5,0,9])plt.plot(x,df)plt.legend(data,loc=2)plt.show()

2、直方图:

这里写图片描述

#直方图绘制import matplotlib.pyplot as pltimport numpy as npnp.random.seed(0)#随机种子m,sigma = 50,5 #均值和标准差a = np.random.normal(m,sigma,size=100)#一百个元素#该函数参数为直方图上面的信息,最后两个参数为颜色的设置#第二个参数为 bin:生成直方图的个数#normed=1表示出现的概率,=0,表示出现的个数plt.hist(a,40,normed=0,histtype='stepfilled',facecolor='b',alpha=0.7)plt.title('test')plt.savefig('hist.png',dpi=600)#保存为PNG文件,dpi为像素plt.show()

3、条状图:

这里写图片描述

#条状图import matplotlib.pyplot as pltimport numpy as npindex = np.arange(5)values = [5,3,2,4,5]plt.bar(index,values,color='b',alpha=0.7)plt.title('bar')plt.xticks(index+0.2,['s','m','i','l','e'])#设置刻度标签plt.savefig('bar.png')#保存为图片plt.show()

4、多序列条状图:

这里写图片描述

#多序列条状图import matplotlib.pyplot as pltimport numpy as npindex = np.arange(5)values1 = [5,3,2,4,5]values2 = [6,5,3,5,7]values3 = [7,4,4,6,8]b = 0.25#设置b为调控距离plt.axis([0,5,0,10])#x轴起止,y轴起止坐标plt.bar(index,values1,b,color='r',alpha=0.7)plt.bar(index+b,values2,b,color='y',alpha=0.7)plt.bar(index+2*b,values3,b,color='b',alpha=0.7)plt.title('A bar chart',fontsize=15)#标题及字体plt.xticks(index+b,['s','m','i','l','e'])#设置刻度标签plt.show()

5、饼状图:

这里写图片描述

#饼图绘制import matplotlib.pyplot as pltlabels = 'Dog','Cat','Fish','Hen' #饼图标签sizes = [30,35,15,20]#饼图的大小explode = (0,0.1,0,0)#0.1为第二个元素凸出距离#饼图绘制函数plt.pie(sizes,explode=explode,labels=labels,\        autopct='%1.1f%%',shadow=False,startangle=90)plt.axis('equal')#plt.savefig('bing',dpi=600)#保存为PNG文件,dpi为像素plt.show()

6、极坐标图:

这里写图片描述

#极坐标图绘制import matplotlib.pyplot as pltimport numpy as npN = 7#极坐标图中数据的个数t = np.linspace(0.0,2*np.pi,N,endpoint=False)#0-2π 按照个数分出N个不同的角度rd = 10*np.random.rand(N)#生成每一个角度对应的值w = np.pi / 2*np.random.rand(N)#宽度值colors = np.array(['navy','darkgreen','lightgreen','pink','blue','brown','violet'])#颜色数组#绘制极坐标方法ax = plt.subplot(111,projection='polar')#一个绘图区域,projection='polar'极坐标参数,采取面向对象方法bars = ax.bar(t, rd, width=w, bottom=0.0,color=colors)#分别对应:left(绘制极坐标系中颜色区域开始位置),height(从中心点到边缘绘制的长度),width(每一个绘图区域的面积)'''  plt.show()

7、散点图:

这里写图片描述

#面型对象方法绘制散点图import numpy as npimport matplotlib.pyplot as pltfig , ax = plt.subplots()#函数变成对象,参数分别对应plt.subplots生成的,fig:图表,ax:绘图区域,参数为空默认绘图区域为111ax.plot(10*np.random.randn(68),10*np.random.randn(68),'*')#(横轴,纵轴,标点方式) randn生成的随机数呈现正态分布乘以10空间范围内,显得更扩散ax.set_title('stars')plt.show()

8、3D曲面:

这里写图片描述

#3D曲面import numpy as npfrom mpl_toolkits.mplot3d import Axes3Dimport matplotlib.pyplot as pltfig = plt.figure()ax = Axes3D(fig)X = np.arange(-5,5,0.5)Y = np.arange(-5,5,0.5)X,Y = np.meshgrid(X,Y)#函数(变换函数可有不同效果)def f(x,y):    return (1-Y**4+X**4)*np.exp(-x**2-y**2)#z=f(x,y)生成三维结构ax.plot_surface(X,Y,f(X,Y),rstride=1,cstride=1,cmap=plt.cm.hot)#cmap关键字可指定各颜色ax.view_init(elev=10,azim=125)#函数功能:旋转曲面,elev,azim两个参数作用分别为:查看曲面高度;曲面旋转角度plt.show()

9、3D散点:

这里写图片描述

#3D散点图import matplotlib.pyplot as pltimport numpy as npfrom mpl_toolkits.mplot3d import Axes3Dfig = plt.figure()ax = Axes3D(fig)x = np.random.randint(30,40,50)y = np.random.randint(20,30,50)z = np.random.randint(10,20,50)x1 = np.random.randint(70,80,50)y1 = np.random.randint(60,70,50)z1 = np.random.randint(50,70,50)x2 = np.random.randint(40,50,50)y2 = np.random.randint(50,60,50)z2 = np.random.randint(30,40,50)ax.scatter(x,y,z)ax.scatter(x1,y1,z1,c='b',marker='^')ax.scatter(x2,y2,z2,c='r',marker='*')ax.set_xlabel('x')ax.set_ylabel('y')ax.set_zlabel('z')plt.show()

10、制作动画:

#制作动画import numpy as npimport matplotlib.pyplot as pltimport matplotlib.animation as anfig = plt.figure()ax = fig.add_subplot(111)N = 10x = np.random.rand(N)y = np.random.rand(N)z = np.random.rand(N)circles,triangles,dots = ax.plot(x,'ro',y,'g^',z,'b.')#设置颜色及形状ax.set_ylim(0,1)plt.axis('off')def update(data):    circles.set_ydata(data[0])    triangles.set_ydata(data[1])    return circles,trianglesdef generate():    while True:        yield np.random.rand(2,N)anm = an.FuncAnimation(fig,update,generate,interval=150)plt.show()
阅读全文
0 0
原创粉丝点击