Pyplot tutorial复现

Pyplot tutorial复现

线图、散点图、直方图绘制

import numpy as np
import pandas as pd 
from pandas import DataFrame,Series
import matplotlib.pyplot as plt 
t=np.arange(0,5.,0.2) 

一、线图

plt.plot(t,t,'r--',t,t**2,'bs',t,t**3,'g^')  
plt.show()

在这里插入图片描述

二、散点图

data={'a':np.arange(50),'c':np.random.randint(0,50,50),'d':np.random.randn(50)} 
data['b']=data['a']+10*np.random.randn(50)
data['d']=np.abs(data['d'])*100
plt.scatter('a','b',c='c',s='d',data=data) 
#c控制点的颜色,s控制点的大小
<matplotlib.collections.PathCollection at 0x1a4e0cf5b88>

在这里插入图片描述

三、分类变量绘图

names=['group_a','group_b','group_c'] 
values=[1,10,100]
plt.figure(figsize=(9,3)) 
plt.subplot(131)
plt.bar(names,values)
plt.subplot(132)
plt.scatter(names,values,c='r',s=20)
plt.subplot(133) 
plt.plot(names,values,'k-*',linewidth=2.0)
plt.suptitle('Categorical Plotting') 
plt.show()   

在这里插入图片描述

四、设置:线条:颜色、样式、线宽;title

lines=plt.plot([1,2,3],[1,4,9],[1,2,3],[1,8,27])
plt.setp(lines,color='r',linewidth=2.0) 
[None, None, None, None]

在这里插入图片描述

def f(t):
    return np.exp(-t)*np.cos(2*np.pi*t) 
t1=np.arange(0.0,5.0,0.1)
t2=np.arange(0.0,5.0,0.02) 

#plt.figure()
plt.subplot(211)
plt.plot(t1,f(t1),'bo',t2,f(t2),'k')  
plt.subplot(212)
plt.plot(t2,np.cos(2*np.pi*t2),'r--') 
plt.show() 

在这里插入图片描述

plt.figure(1)
plt.subplot(211)
plt.plot([1,2,3])
plt.title('Easy as 1,2,3')
plt.subplot(212)
plt.plot([4,5,6])
plt.title('Easy as 4,5,6')

plt.figure(2)
plt.plot([4,5,6])

plt.show()

在这里插入图片描述

在这里插入图片描述

五、直方图

mu,sigma=100,15
x=mu+sigma*np.random.randn(1000)
#1000个数据
plt.hist(x,bins=50,density=1,facecolor='b',alpha=0.6) 
#bins直方图条形个数,density是否以频率显示纵坐标,默认纵坐标是个数,alpha条形透明度
plt.xlabel('Smarts')
plt.ylabel('Probability')
plt.title('Histogram of IQ',fontsize=14,color='red') 
plt.text(60,0.025,r'mu=100,sigma=15') 
plt.axis([40,160,0,0.04])  
plt.grid(True)
plt.show() 

在这里插入图片描述

plt.annotate对图上的点添加注释

ax=plt.subplot(111)
t=np.arange(0.0,5.0,0.01)
s=np.cos(2*np.pi*t)
line,=plt.plot(t,s,linewidth=2)
plt.annotate('local max',xy=(2,1),xytext=(3,1.5),arrowprops=dict(facecolor='red',shrink=0.02))
#添加注释,xy是注释点的位置,xytext是注释文本的位置,arrowprops控制箭头的颜色和大小 
plt.ylim(-2,2) 
plt.show() 

在这里插入图片描述

六、对数轴和其他非线型坐标轴

from matplotlib.ticker import NullFormatter 
np.random.seed(19680801)
y=np.random.normal(loc=0.5,scale=0.4,size=1000)
#正态分布loc:均值,scale:标准差,size:大小
#np.random.randn(N)是标准正态分布,均值为0,标准差为1。标准差越大,图形越矮胖;标准差越小,图形越窄高。
y=y[(y>0)&(y<1)]
y.sort()
x=np.arange(len(y))

plt.figure()
plt.subplot(221)
plt.plot(x,y)
plt.yscale('linear')
plt.title('linear')
plt.grid(True)

plt.subplot(222)
plt.plot(x,y)
plt.yscale('log')
plt.title('log')
plt.grid('True') 

plt.subplot(223)
plt.plot(x,y-y.mean())
plt.yscale('symlog',linthrethy=0.05)
#对称的对数
plt.title('symlog')
plt.grid('True')

plt.subplot(224)
plt.plot(x,y)
plt.yscale('logit')
plt.title('logit')
plt.grid(True)

plt.gca().yaxis.set_minor_formatter(NullFormatter())
plt.subplots_adjust(top=0.92,bottom=0.08,left=0.10,right=0.95,hspace=0.35,wspace=0.35)
#前四个参数控制子图相对于整个视图的位置。hspace和wspace控制相邻子图间上下和左右的距离。
plt.show()

在这里插入图片描述

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 0
    评论
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值