python#作图函数1

plt.plot([1,2,3],[3,2,1])
----------
x-->[1,2,3]
y-->[3,2,1]
x=np.loatxt('文件路径',delimter=',',skiprows=1')
delimter=','-----文件的数据用','号分割的
skiprows=1-----跳过第一行
usecols=(3,4,5)---取文本的34列
unpack=False--可以把数据分开分列
y,x=np.loatxt('文件路径',delimter=',',skiprows=1',usecols=(3,4),unpack=Ture)
unpack=Ture------2列数据放在y , x 里面
y=np.sort(x)-----y是一个新生产的数组,经过排序的
y=x.sort ------- x改变了

mat

散点图
height = [161,170,182,175,173,165]
weight = [50,58,70,70,69,55]
plt.scatter(height,weight)
plt.show()
scatter(x,y,s=100)
s=点的面积大小
c='r'---点的颜色
marker----点的样子
alpha=0.5-----透明度
折线图

观察数据,与实际变化

x = np.linspace(-10,10,100)  # (-10,10)取100份点
y = x**2
plt.plot(x,y,color='red',marker='o',linestyle='--'
plt.show()

plt.polt_date(x,y)—polt_date()专门画时间的

条形图
树状的条形图
N=5 
y=[20,10,30,25,15]
index = np.arange(N)
pl = plt.bar(x = index, height = y,color="r",width=0.5)

x—是x轴上的数据
height-----是x上数据的高度
width=0.5----条形图的宽度

水平条形图

bar

N=5 
y=[20,10,30,25,15]
index = np.arange(N)
plt.bar(x=0,bottom=index,width=y,height=0.5,orientation='horizontal')

bottom --变为纵坐标
width=y------是纵坐标上数据的高度
height=0.5----条形图的宽度
orientation=‘horizontal’----变水平的必要条件

N=5 
a=[20,10,30,25,15]
index = np.arange(N)
plt.barh(index,a,height=0.5)
--------
直接水平就好
原来的长度变成了宽度

3.两条并列的

index=np.arange(4)
BJ=[52,55,63,53]
SH=[44,66,55,41]
bar_with=0.3
plt.bar(index,BJ,bar_with,color='b')
plt.bar(index+bar_with,SH,bar_with,color='r')

4.层叠图

index=np.arange(4)
BJ=[52,55,63,53]
SH=[44,66,55,41]
bar_with=0.3
plt.bar(index,BJ,bar_with,color='b')
plt.bar(index,SH,bar_with,color='r',bottom=BJ)

bottom–有一种开始位置的意思,所以说,你懂的

直方图

hist/hist2d

mu = 100
sigma =20
x= mu+ sigma*np.random.randn(20000)
plt.hist(x,bins=10,color='g',normed=True)

bins----就是有几个柱子
normed=标准化—标准化了以后,出现频率

1.1双变量的直方图

x=np.random.randn(1000)+2
y=np.random.randn(1000)+3

plt.hist2d(x,y,bins=40)
饼状图

pie

labels = 'A','B','C','D'
fracs = [15,30,45,10]
plt.axes(aspect=1)#变成圆形
plt.pie(x=fracs,labels=labels,autopct='%.0f%%',explode=[0,0.1,0,0],shadow=True)

autopct=’%.0f%%’------显示百分百
explode=[0,0.1,0,0]-----离开圆心的距离
shadow=True------阴影有立体感觉

格式字符串

y= np.arange(1,5)
plt.plot(y,'r.--')

‘r.–’…第一个是写颜色r
第2个是. 标志处的样式
----线段的样式

子图

figure()就相当于定义了一个窗口

#1总行数,1总列数,1,位置
x=np.arange(1,100)
fig=plt.figure()
ax1=fig.add_subplot(221)
ax1.plot(x,x)
ax2=fig.add_subplot(222)
ax2.plot(x,-x)
ax3=fig.add_subplot(223)
ax3.plot(x,x*x)

ax1=fig.add_subplot(221)
221------#2总行数,2总列数,位置在1处

多个图

#1总行数,1总列数,1,位置
fig1=plt.figure()
ax1=fig1.add_subplot(111)
ax1.plot([1,2,3],[3,2,1])
fig2=plt.figure()
ax2=fig2.add_subplot(111)
ax2.plot([1,2,3],[1,2,1])

网格

1.交互式

y = np.arange(1,5)
plt.plot(y,y*2)
plt.grid(True)

plt.grid(True)------出现网格

2.面向对象

x=np.arange(0,10,1)
fig=plt.figure()
ax=fig.add_subplot(111)
plt.plot(x,x*2)
ax.grid(c='g')

图例

x=np.arange(1,11,1)
plt.plot(x,x*2,label='Noraml')
plt.plot(x,x*3,label='Fast')
plt.plot(x,x*4,label='Faster')
plt.legend(loc=0,ncol=3)

plt.legend()-----显示图例
loc=0 loc=1 loc=2 loc=3 loc=4—不写就行 图例的位置
ncol=3----分成几列


x=np.arange(1,11,1)
fig=plt.figure()
ax=fig.add_subplot(111)
l=plt.plot(x,x,label='xxx')
ax.legend()

坐标轴范围的调整

x=np.arange(-10,11,1)
plt.plot(x,x*x)
plt.axis([-6,5,-20,60])
plt.xlim([2,5])#x轴的范围
plt.ylim([2,3])#y轴的范围

plt.axis([-6,5,-20,60])----[x的范围,y的范围]

x=np.arange(1,11,1)
plt.plot(x,x)
ax=plt.gca()
ax.locator_params(nbins=5)

--------- x,y轴出现5格

添加坐标轴
x=np.arange(2,20,1)
y1=x*x
y2=np.log(x)
plt.plot(x,y1)
plt.twinx()#第2个坐标轴
plt.plot(x,y2,'r')
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值