标签:标准 表示 rpd 百分比 变化 lock 图库 平移 seed
python的底层绘图库,主要做数据可视化图表,名字取材于Matlab
,模仿Matlab
构建
身高-体重
代码
from matplotlib import pyplot as plt
import numpy as np
height=[161,170,182,175,173,165]
weight=[50,58,80,70,69,55]
plt.scatter(height,weight)
plt.show()
正相关、负相关、不相关
from matplotlib import pyplot as plt
import numpy as np
N=1000
x=np.random.randn(N)
y1=np.random.randn(N)
plt.scatter(x,y1)
plt.show()
from matplotlib import pyplot as plt
import numpy as np
N=1000
x=np.random.randn(N)
y=x+np.random.randn(N)*0.5
plt.scatter(x,y)
plt.show()
函数图--二次曲线
from matplotlib import pyplot as plt
import numpy as np
x=np.linspace(-10,10,100)
y=x**2
plt.plot(x,y)
plt.show()
日期调整:
import numpy as np
import matplotlib.pyplot as plt
import matplotlib.dates as mdates
# xxx是标志股票涨跌的数据,第一列date为日期,第三列open为开盘价,第五列close为收盘价,skiprows=1代表跳过第一列数据(第一列为数据标示),usecols表示使用哪几列数据,unpack为True则拆分列,为False则不进行拆分
date,open,close=np.loadtxt(‘xxx‘,delimeter=‘,‘,converters={0:mdates.strpdate2num(‘%m/%d/%Y‘)},skiprows=1,usecols=(0,1,4),unpack=True) # 这里将第0列,即日期转化为matplotlib认识的数值型
plt.plot_date(date,open,‘-‘)
plt.show()
横向条形图
from matplotlib import pyplot as plt
import numpy as np
N=5
y=[30,10,30,25,15]
index=np.arange(N)
p1=plt.bar(x=index,height=y,color=‘green‘,width=0.7)
plt.show()
纵向条形图
from matplotlib import pyplot as plt
import numpy as np
N=5
y=[30,10,30,25,15]
index=np.arange(N)
p1=plt.barh(y=index,width=y,color=‘green‘,height=0.7)
# 使用barh函数,参数需要更改
plt.show()
层叠式条形图
样式一
from matplotlib import pyplot as plt
import numpy as np
index=np.arange(4)
sales_BJ=[52,55,83,53]
sales_SH=[44,66,55,41]
bar_width=0.3
plt.bar(index,sales_BJ,bar_width,color=‘b‘)
plt.bar(index+bar_width,sales_SH,bar_width,color=‘r‘)
# x值变化起到平移的效果
plt.show()
样式二
from matplotlib import pyplot as plt
import numpy as np
index=np.arange(4)
sales_BJ=[52,55,83,53]
sales_SH=[44,66,55,41]
bar_width=0.3
plt.bar(index,sales_BJ,bar_width,color=‘b‘)
plt.bar(index,sales_SH,bar_width,color=‘r‘,bottom=sales_BJ)
# bottom参数
plt.show()
均值为100,标准差为20的数据
![截屏2021-01-09 15.11.19](https://gitee.com/Jenner_s/my-blog-pics/raw/master/img/截屏2021-01-09 15.11.19.png)
from matplotlib import pyplot as plt
import numpy as np
mu = 100 # mean of distribution
sigma = 20 #standard deviation of distribution
x = mu + sigma * np.random.randn(2000)
plt.hist(x,bins=10,color=‘red‘,edgecolor=‘white‘,density=True)
plt.show()
2-D直方图
from matplotlib import pyplot as plt
import numpy as np
x = np.random.randn(1000)+2
y = np.random.randn(1000)+3
plt.hist2d(x,y,bins=40) # 以颜色深浅表示分布密度
plt.show()
from matplotlib import pyplot as plt
import numpy as np
explode=[0,0.05,0,0] # explode代表各项远离圆心的距离
labels= ‘A‘,‘B‘,‘C‘,‘D‘
fracs=[15,30,45,10]
plt.pie(x=fracs,labels=labels,autopct=‘%.2f%%‘,explode=explode,shadow=True)
# autopct代表显示比例格式,shadow代表阴影效果
plt.show()
from matplotlib import pyplot as plt
import numpy as np
#np.random.seed(100)
data=np.random.normal(size=1000,loc=0,scale=1)
plt.boxplot(data)
plt.show()
多组箱形图
from matplotlib import pyplot as plt
import numpy as np
#np.random.seed(100)
data=np.random.normal(size=(1000,4),loc=0,scale=1)
labels=[‘A‘,‘B‘,‘C‘,‘D‘]
plt.boxplot(data,sym=‘o‘,labels=labels)
plt.show()
标签:标准 表示 rpd 百分比 变化 lock 图库 平移 seed
原文地址:https://www.cnblogs.com/JennerShao/p/14341848.html