使用面向对象绘制直方图,饼图,环形图,散点图,气泡图,误差棒图

2.5 绘制直方图

  2.5.1 使用面向对象API方法绘制直方图

   

import  matplotlib.pyplot as plt
import numpy as np
fig=plt.figure() #创建画布
ax=fig.add_subplot(111) #绘制图表区域
#准备五十个数据
scores=np.random.randint(0,100,50)
#h绘制直方图
ax.hist(scores,bins=8,histtype='stepfilled',rwidth=0.3)
plt.show()

2.5.2  绘制人脸识别的灰度直方图

import  matplotlib.pyplot as plt
import numpy as np
#face_recognition 人脸识别直方图
#1000个随机数
fig=plt.figure() #创建画布
ax=fig.add_subplot(111) #绘制图表区域
random_state=np.random.RandomState(19680801)
random_x=random_state.randn(1000)
#绘制25个矩形条的直方图
ax.hist(random_x,bins=25)
plt.show()

2.6 绘制饼图或圆环图

    2.5.1 使用面向对象API方法绘制饼图或圆环图

import  matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()#创建画布
ax=fig.add_subplot(111) #绘制图表区域
data=np.array([20,50,10,15,30,55])#准备数据
pie_labels=np.array(['A','B','C','D','E','F']) #设置标签
#绘制饼图:半径为0.5,数值保留一位小数
ax.pie(data,radius=1.5,labels=pie_labels,autopct='%.1f%%')
plt.show()


import  matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()#创建画布
ax=fig.add_subplot(111) #绘制图表区域
data=np.array([20,50,10,15,30,55])#准备数据
pie_labels=np.array(['A','B','C','D','E','F']) #设置标签
#绘制环形图:外院半径为1.5,楔形宽度为0.7
ax.pie(data,radius=1.5,wedgeprops={'width':0.7},labels=pie_labels,autopct='%.1f%%',pctdistance=0.7)
plt.show()

2.6.2 绘制支付宝月账单报告

#_monthlly_bills_of_alipay #支付宝月账单报告
import numpy as np
import  matplotlib.pyplot as plt
import matplotlib as mpl
mpl.rcParams['font.sans-serif']=['SimHei']
mpl.rcParams['axes.unicode_minus']='false'
fig=plt.figure()#创建画布
ax=fig.add_subplot(111) #绘制图表区域
#饼图外侧的说明文字
kinds=['购物','人情往来','餐饮美食','通信物流','生活日用','交通出行','休闲娱乐','其它']
#饼图的数据
money_scale=[800,100,1000,200,300,200,200,200]
dev_position=[0.1,0.1,0.1,0.1,0.1,0.1,0.1,0.1]##爆炸程度
ax.pie(money_scale,labels=kinds,autopct='%.1f%%',shadow='true',explode=dev_position,startangle=90)
plt.show()

2.7 绘制散点图或气泡图

 2.7.1 使用面向对象API方法绘制散点图或气泡图

import  matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()#创建画布
num=50
ax=fig.add_subplot(111) #绘制图表区域
x=np.random.rand(num)#横坐标五十内的随机数
y=np.random.rand(num)#纵坐标五十内的随机数
ax.scatter(x,y)#绘制散点图
plt.show()

import  matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()#创建画布
num=50
ax=fig.add_subplot(111) #绘制图表区域
x=np.random.rand(num)#横坐标五十内的随机数
y=np.random.rand(num)#纵坐标五十内的随机数
area=(30*np.random.rand(num))**2
ax.scatter(x,y,s=area)#绘制气泡图
plt.show()

2.7.2 绘制汽车速度与制动距离关系的散点图

#vihicle_speed_and_braking_distance 汽车速度与控制距离图
import numpy as np
import matplotlib as mpl
mpl.rcParams['axes.unicode_minus']='false'
mpl.rcParams['font.sans-serif']=['SimHei']
fig=plt.figure()#创建画布
ax=fig.add_subplot(111) #绘制图表区域
#准备x和y的的数据
x_speed=np.arange(10,210,10)
y_distance=np.array([0.5,2.0,4.4,7.9,12.3,17.7,24.1,31.5,39.9,49.2,59.9,70.8,83.1,96.4,110.7,126.0,142.2,159.4,177.6,196.8])
#绘制散点图
ax.scatter(x_speed,y_distance,s=20,alpha=0.7)
plt.show()

2.8 绘制误差棒图

 2.8.1  使用面向对象API方法绘制误差棒图

import  matplotlib.pyplot as plt
import numpy as np
fig=plt.figure()#创建画布
ax=fig.add_subplot(111) #绘制图表区域
#准备x和y的数据
x=np.arange(5)
y=(25,32,34,20,25)
y_offset=(3,5,2,3,3)#误差范围
ax.errorbar(x,y,yerr=y_offset,capsize=3,capthick=2,fmt='*-')
plt.show()

 2.8.2  绘制四个季节的细根生物量的误差棒图

#fine_root_biomass
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams['font.family'] = 'SimHei'
plt.rcParams['axes.unicode_minus'] = False
# 创建画布
fig = plt.figure()
# 创建图表绘图区域
ax = fig.add_subplot(111)
# 准备数据
# 准备 x 轴和 y 轴的数据
x = np.arange(3)
y1 = np.array([2.04, 1.57, 1.63])
y2 = np.array([1.69, 1.61, 1.64])
y3 = np.array([4.65, 4.99, 4.94])
y4 = np.array([3.39, 2.33, 4.10])
# 指定测量偏差
error1 = [0.16, 0.08, 0.10]
error2 = [0.27, 0.14, 0.14]
error3 = [0.34, 0.32, 0.29]
error4 = [0.23, 0.23, 0.39]
bar_width = 0.2
# 调用绘图方法绘制图表
width = 0.2
ax.bar(x, y1,width=bar_width)
ax.bar(x+width, y2,align='center',tick_label=['春季','夏季','秋季'],width=bar_width)
ax.bar(x+2*width, y3,width=bar_width)
ax.bar(x+3*width, y4,width=bar_width)
# 添加误差棒
ax.errorbar(x,y1,yerr=error1,capsize=4,capthick=1,fmt='c,')
ax.errorbar(x+width,y2,yerr=error2,capsize=4,capthick=1,fmt='r,')
ax.errorbar(x+2*width,y3,yerr=error3,capsize=4,capthick=1,fmt='k,')
ax.errorbar(x+3*width,y4,yerr=error4,capsize=4,capthick=1,fmt='y,')
plt.show()

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

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

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

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值