python替代matlab做分析,绘制图表

近期想做一个项目,需要用到matlab绘制图表,做数据分析,但我的电脑配置不是很好,启动matlab相对较慢,且matlab占的内存较大。刚好最近在学习python,就想着能不能在python这么多库文件中找到一个合适的库包替代他,经过查找,刚好有一个matplotlab包可以使用,并且可以完美绘制经常使用的图表。通过pythonjupyter notebook一步一步设置,使得绘图更加顺利。现将一些常用的图表程序整理如下。
更多的图表参考文件:https://download.csdn.net/download/lengxi1/87376790


常用图表如下

一、折线图

from pylab import *
plt.plot([1,10,3,6])
plt.axis([0,5,0,20])
plt.title('my first plot')

在这里插入图片描述

二、散点图

import math
import numpy as np
from pylab import *
t= np.arange(0,2.5,0.1)
y1= np.sin(math.pi*t)
y2= np.sin(math.pi*t+math.pi/2)
y3= np.sin(math.pi*t-math.pi/2)
plt.plot(t,y1,'b*',t,y2,'g^',t,y3,'ys')

在这里插入图片描述

三、一个图表多个图

from pylab import *
t =np.arange(0,5,0.1)
y1=np.sin(2*np.pi*t)
y2=np.sin(2*np.pi*t)
plt.subplot(211)#分为两部分,控制第一个图
plt.plot(t,y1,'b-')
plt.subplot(212)
plt.plot(t,y2,'r--')

在这里插入图片描述

四、加标签

from pylab import *
plt.axis([0,5,0,20])#坐标轴取值范围
plt.title('my third plot',fontsize=20,fontname='Times New Roman')
plt.xlabel('counting',color ='green')
plt.ylabel('square valuse',color='gray')#坐标轴
plt.text(1,1.5,'First',fontsize=20,fontname='Times New Roman')
plt.text(2,4.5,'Second')
plt.text(3,9.5,'Third')
plt.text(4,16.5,'Four')#插入文本
plt.text(1.1,12,r'$y=x^2$',bbox={'facecolor':'yellow','alpha':0.2},fontsize=20)#插入数学表达式
plt.grid(True)#添加网格
plt.plot([1,2,3,4],[1,4,9,16],'ro')
plt.legend(['first series'],loc =2)#添加图列

在这里插入图片描述

五、数学函数

from pylab import *
x= np.arange(-2*np.pi,2*np.pi,0.01)
y=np.sin(3*x)/x
y2=np.sin(2*x)/x
y3=np.sin(5*x)/x
plt.plot(x,y)
plt.plot(x,y2,'m--')
plt.plot(x,y3)
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi,],[r'$-2\pi$',r'$-\pi$',r'$0$',r'$+\pi$',r'$+2\pi$'])
plt.yticks([-1,0,1,2,3,4,5])

在这里插入图片描述

5.1修改坐标轴位置
x= np.arange(-2*np.pi,2*np.pi,0.01)
y=np.sin(3*x)/x
y2=np.sin(2*x)/x
y3=np.sin(5*x)/x
plt.plot(x,y)
plt.plot(x,y2,'m--')
plt.plot(x,y3)
plt.xticks([-2*np.pi,-np.pi,0,np.pi,2*np.pi,],[r'$-2\pi$',r'$-\pi$',r'$0$',r'$+\pi$',r'$+2\pi$'])
plt.yticks([-1,0,1,2,3,4,5])
#修改坐标轴
ax = plt.gca()
ax.spines['right'].set_color('none')
ax.spines['top'].set_color('none')
ax.xaxis.set_ticks_position('bottom')
ax.spines['bottom'].set_position(('data',0))
ax.yaxis.set_ticks_position('left')
ax.spines['left'].set_position(('data',0))

在这里插入图片描述

六、直方图

import matplotlib.pyplot as plt
import numpy as np
index =np.arange(5)
values1=np.random.randint(0,20,5)
values2=np.random.randint(0,20,5)
values3=np.random.randint(0,20,5)
bw =0.3
plt.bar(index,values1,bw,color='b')
plt.bar(index+bw,values2,bw,color='g')
plt.bar(index+2*bw,values3,bw,color='m')
plt.xticks(index+bw,['A','B','C','D','E'])
for x,y in zip(index,values1):
    plt.text(x,y+1,y,ha='center',va ='top')
for x,y in zip(index,values2):
    plt.text(x+0.3,y+1,y,ha='center',va ='top')
for x,y in zip(index,values3):
    plt.text(x+0.6,y+1,y,ha='center',va ='top')

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
import pandas as pd
data={'series1':[1,2,3,4,5],
      'series2':[2,5,8,6,3],
      'series3':[1,6,3,5,4],
}
df =pd.DataFrame(data)
df.plot(kind='bar',stacked=True)

在这里插入图片描述

import matplotlib.pyplot as plt
import numpy as np
x0=np.arange(8)
y1=np.random.randint(0,10,8)
y2=np.random.randint(0,10,8)
plt.bar(x0,y1,0.9,color='g')
plt.bar(x0,-y2,0.9,color='b')
plt.xticks(())
plt.grid(True)
for x,y in zip(x0,y1):
    plt.text(x,y+1,y,ha='center',va ='top')
for x,y in zip(x0,y2):
    plt.text(x,-y,y,ha='center',va ='top')

在这里插入图片描述

七、饼图

import matplotlib.pyplot as plt
labels =['A','B','C','D']
y1=[10,30,45,15]
colors=['green','yellow','blue','red']
explode=[0,0.1,0,0]
plt.pie(y1,labels=labels,colors=colors,startangle=90,explode=explode,shadow=True,autopct='%1.1f%%')
plt.axis('equal')

在这里插入图片描述

八、等高图

import matplotlib.pyplot as plt
import numpy as np
dx =0.01
dy =0.01
x =np.arange(-2.0,2.0,dx)
y =np.arange(-2.0,2.0,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.contour(X,Y,f(X,Y),8)
plt.clabel(C,inline=1,fontsize=10)

在这里插入图片描述

九、极轴图

import matplotlib.pyplot as plt
import numpy as np
N=8
theta =np.arange(0.,2*np.pi,2*np.pi/N)
radii =np.array([4,7,5,3,1,5,6,7])
plt.axes([0.025,0.025,0.95,0.95],polar=True)
colors=np.array(['red','blue','darkred','lightgreen','yellow','orange','plum','violet'])
#colors=np.array(['#4bb2c5','#c5b47f','#EAA288','#579575','#839577','#968c12','#953579','#4b5de4'])
bars=plt.bar(theta,radii,width=(2*np.pi/N),bottom=0.0,color=colors)

在这里插入图片描述

十、三维空间图

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
fig =plt.figure()
ax =Axes3D(fig)
dx =0.01
dy =0.01
x =np.arange(-2.0,2.0,dx)
y =np.arange(-2.0,2.0,dy)
X,Y =np.meshgrid(x,y)
def f(x,y):
    return (1-y**5+x**5)*np.exp(-x**2-y**2)
ax.plot_surface(X,Y,f(X,Y),rstride=1,cstride=1,cmap=plt.cm.hot)
ax.view_init(elev=30,azim=125)

在这里插入图片描述

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
import numpy as np
x=np.arange(8)
y=np.random.randint(0,10,8)
y2 =y+np.random.randint(0,3,8)
y3 =y2+np.random.randint(0,3,8)
y4 =y3+np.random.randint(0,3,8)
y5 =y4+np.random.randint(0,3,8)
clr=np.array(['red','blue','darkred','lightgreen','yellow','orange','plum','violet'])
fig =plt.figure()
ax =Axes3D(fig)
ax.bar(x,y,0,zdir='y',color=clr)
ax.bar(x,y2,10,zdir='y',color=clr)
ax.bar(x,y3,20,zdir='y',color=clr)
ax.bar(x,y4,30,zdir='y',color=clr)
ax.bar(x,y5,40,zdir='y',color=clr)
ax.set_xlabel('X label')
ax.set_ylabel('Y label')
ax.set_zlabel('Z label')
ax.view_init(elev=30)

在这里插入图片描述

十一、分布嵌入图

import matplotlib.pyplot as plt
import numpy as np
gs =plt.GridSpec(3,3)
fig =plt.figure(figsize=(6,6))
x1 =np.array([1,3,2,5])
y1 =np.array([4,3,7,3])
x2=np.arange(5)
y2 =np.array([3,2,5,3,4])
s1 =fig.add_subplot(gs[1,:2])
s1.plot(x,y,'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,color='k')
s5 =fig.add_subplot(gs[2,1:])
s5.plot(x1,y1,'b^',x2,y2,'yo')

在这里插入图片描述

  • 3
    点赞
  • 7
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值