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

生而为人,请务必善良

matplotlib 实现数据可视化

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

实例目录:

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

基础图表实例

1、线性图:

为pandas数据结构绘制线性图

这里写图片描述

import numpy as np
import matplotlib.pyplot as plt
import pandas as pd

data = {'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 plt
import numpy as np

np.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 plt
import numpy as np
index = 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 plt
import numpy as np
index = 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 plt

labels = '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 plt
import numpy as np

N = 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 np
import matplotlib.pyplot as plt

fig , ax = plt.subplots()#函数变成对象,参数分别对应plt.subplots生成的,fig:图表,ax:绘图区域,参数为空默认绘图区域为111
ax.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 np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig = 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 plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
fig = 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 np
import matplotlib.pyplot as plt
import matplotlib.animation as an

fig = plt.figure()
ax = fig.add_subplot(111)
N = 10
x = 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,triangles

def generate():
    while True:
        yield np.random.rand(2,N)

anm = an.FuncAnimation(fig,update,generate,interval=150)

plt.show()
發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章