【学习笔记】python数据可视化之matplotlib实践第二章

使用统计函数绘制简单图形

1.bar()函数:在x轴上绘制定性数据的分布特征,用于绘制柱状图

函数基本参数介绍:plt.bar(x,height,color = 'c',edgecolor = 'y',width = 0.8,align = 'center',tick_label = ['白菜','卷心菜'],hatch = '/' )

x柱状图的x轴坐标,每个数值对应于一个柱子的位置
height柱状图的高度,即y轴上的数值
color柱子的填充颜色
edgecolor柱子边缘的颜色
width柱子的宽度
align柱子与x轴的对齐方式,有center和edge对齐方式
tick_labelx轴的标签,表示x轴上的每个位置(每个柱子)对应的标签
hatch柱子的图案,图案颜色和边缘颜色相同,未设置边缘颜色为黑色

其他参数参考:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.bar.html 

代码例子实现:

#导入后续需要使用的代码
import matplotlib as mpl
import matplotlib.pyplot as plt
#设置显示
mpl.rcParams['font.sans-serif'] = ['SimHei']#设置默认字体为黑体,确保绘图中中文字符能够正确显示,避免出现乱码或显示为方块的情况
mpl.rcParams['axes.unicode_minus'] = False#确保负号能够正常显示

x = [1,2,3,4,5,6]
height = [3,1,4,5,8,9]

plt.bar(x,height,color = 'c',edgecolor = 'y',width = 0.8,align='center',tick_label = ['q','a','c','e','f','r'],hatch = '/')

plt.xlabel('箱子编号')
plt.ylabel('箱子重量(kg)')
plt.show()

代码运行结果如下: 

2.barh():在y轴上绘制定性数据的分布特征,用于绘制条形图

barh()函数与bar()函数参数设置基本相同,特别地,width这个参数在barh()函数中设置比bar()要复杂一些,在此处就不在使用width进行代码演示

具体参数说明参考:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.barh.html 

#导入后续需要使用的代码
import matplotlib as mpl
import matplotlib.pyplot as plt
#设置显示
mpl.rcParams['font.sans-serif'] = ['SimHei']#设置默认字体为黑体,确保绘图中中文字符能够正确显示,避免出现乱码或显示为方块的情况
mpl.rcParams['axes.unicode_minus'] = False#确保负号能够正常显示

x = [1,2,3,4,5,6]
height = [3,1,4,5,8,9]

plt.barh(x,height,color = 'c',edgecolor = 'r',align='center',tick_label = ['q','a','c','e','f','r'],hatch = '/')

plt.xlabel('箱子重量(kg)')
plt.ylabel('箱子编号')
plt.show()

以上代码的运行结果:

3.hist(): 在x轴上绘制定量数据的分布特征,用于绘制直方图

函数基本参数介绍:plt.hist(x,bins = bins,color = 'g',edgecolor = 'black',histtype = 'bar',rwidth = 1,alpha = 0.6)

x提供大量数据,将x中的数据按bins指定的区间进行分组
binsbins为序列[1,2,3,4],产生的区间为[1,2),[2,3),[3,4]
color设置直方图填充颜色
edgecolor设置直方图边缘颜色
histtype设置绘制直方图类型
rwidth条形的相对宽度
alpha透明度

更多的参数设置参考:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.hist.html

代码示例:

#导入后续需要使用的代码
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
#设置显示
mpl.rcParams['font.sans-serif'] = ['SimHei']#设置默认字体为黑体,确保绘图中中文字符能够正确显示,避免出现乱码或显示为方块的情况
mpl.rcParams['axes.unicode_minus'] = False#确保负号能够正常显示

boxWeight = np.random.randint(0,10,100)
#设置x作为大量的数据样本,生成更光滑的直方图
x = boxWeight

bins = range(0,11,1)
plt.hist(x,bins = bins,color = 'g',histtype = 'bar',rwidth = 1,alpha = 0.6,edgecolor = 'black')

plt.xlabel('箱子重量(kg)')
plt.ylabel('箱子编号')
plt.show()

代码运行结果: 

4.pie():绘制定性数据的不同类别的百分比,用于绘制饼图

函数基本参数介绍:plt.pie(x,labels = kinds,autopct='%3.1f%%',startangle=60,colors = colors)
x楔形大小
labels为每个楔形提供标签的字符串序列
autopct

设置显示数值的格式

startangle饼图起点旋转的角度 从 x 轴逆时针方向
colors可以设置每块饼图的颜色

其他参数参考:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.pie.html

示例代码:

#导入后续需要使用的代码
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
#设置显示
mpl.rcParams['font.sans-serif'] = ['SimHei']#设置默认字体为黑体,确保绘图中中文字符能够正确显示,避免出现乱码或显示为方块的情况
mpl.rcParams['axes.unicode_minus'] = False#确保负号能够正常显示

kinds = '简易箱','保温箱','行李箱','密封箱'
#使用十六进制编码设置颜色
colors= ['#e41a1c','#377eb8','#4daf4a','#984ea3']
#设置每块饼图的占比
x = [0.05,0.45,0.15,0.35]

plt.pie(x,labels = kinds,autopct='%3.1f%%',startangle=60,colors = colors)

plt.title('不同类型箱子的销售数量占比')
plt.show()

代码运行结果: 

5.polar():在极坐标上绘制折线图,用于绘制极限图

函数基本参数介绍:plt.polar(theta,r,color = 'chartreuse',linewidth = 2,marker = '*',mfc = 'b',ms = 10)
theta极坐标中的角度
r极坐标中的半径

color

设置线条颜色

linewidth设置线条宽度
marker设置数据点的标记
mfc设置数据点的填充颜色
ms设置数据点的形状大小

示例代码:

#导入后续需要使用的代码
import matplotlib.pyplot as plt
import numpy as np


barslices = 12
theta = np.linspace(0.0,2*np.pi,barslices,endpoint=False)#endpoint = False表示不包含结束值
r = 30*np.random.rand(barslices)

plt.polar(theta,r,color = 'chartreuse',linewidth = 2,marker = '*',mfc = 'b',ms = 10)

plt.show()

代码运行结果: 

6.scatter():二维数据借助气泡大小展示三维数据,用于绘制气泡图

 函数基本参数介绍:plt.scatter(a,b,s = np.power(10*a+20*b,2),c = np.random.rand(100),cmap = mpl.cm.RdYlBu,marker = 'o')

ax轴上的数值
by轴上的数值
s散点标记点的形状大小
c散点标记的颜色
cmap将浮点数映射成颜色的颜色映射表
marker指定散点的形状

其他参数参考:matplotlib.pyplot.scatter — Matplotlib 3.9.2 文档

示例代码:

#导入后续需要使用的代码
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

a = np.random.randn(100)
b = np.random.randn(100)
#mpl.cm.RdYlBu表示一个从红色到蓝色再到黄色的颜色映射表
plt.scatter(a,b,s = np.power(10*a+20*b,2),c = np.random.rand(100),cmap = mpl.cm.RdYlBu,marker = 'o')


plt.show()

运行结果:

7.stem():绘制离散有序数据,用于绘制棉棒图

 函数基本参数介绍:plt.stem(x,y,linefmt='-.',markerfmt='o',basefmt='-')

x表示棉棒的x位置
y表示棉棒头的y坐标
linefmt设置垂直线的样式
markerfmt设置棉棒头处的标记形状为圆形
basefmt设置基线的样式

其他参数设置参考:https://matplotlib.org/stable/api/_as_gen/matplotlib.pyplot.stem.html

示例代码:

#导入后续需要使用的代码
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.5,2*np.pi,20)
y = np.random.randn(20)

plt.stem(x,y,linefmt='-.',markerfmt='o',basefmt='-')

plt.show()

运行结果: 

8.boxplot():用于绘制箱线图

函数基本参数介绍:boxplot(x)

x绘制箱线图的输入数据

其他参数设置参考:matplotlib.pyplot.boxplot — Matplotlib 3.9.2 文档

示例代码:

#导入后续需要使用的代码
import matplotlib.pyplot as plt
import numpy as np
import matplotlib as mpl

mpl.rcParams['font.sans-serif'] = ['FangSong']
mpl.rcParams['axes.unicode_minus'] = False

x = np.random.randn(1000)

plt.boxplot(x)
plt.xticks([1],['随机生成器AlphaRM'])#表示x轴上只有一个刻度位置,即位置1
plt.ylabel('随机数值')
plt.title('随机数生成器抗干扰能力的稳定性')
#axis = 'y'表示只在y轴上显示网格线,ls = ':'表示网格线的样式为虚线,lw设置网格线宽度
plt.grid(axis = 'y',ls = ':',lw = 1,color = 'gray',alpha = 0.4)
plt.show()

运行代码结果: 

9.errorbar():绘制y轴方向或是x轴方向的误差范围,用于绘制误差棒图

函数基本参数介绍:plt.errorbar(x,y,fmt = 'bo:',yerr = 0.2,xerr = 0.02)

x数据点的横坐标

y

数据点的纵坐标
fmt指定数据点的标记样式
yerry轴方向的数据点的误差计算方式
xerrx轴方向的数据点的误差计算方式

其他参数设置参考:matplotlib.pyplot.errorbar — Matplotlib 3.9.2 文档

示例代码:

#导入后续需要使用的代码
import matplotlib.pyplot as plt
import numpy as np

x = np.linspace(0.1,0.6,6)
y = np.exp(x)
#fmt = 'bo:'表示设置标记点为蓝色,用圆点表示,:表示虚线连接数据点
plt.errorbar(x,y,fmt = 'bo:',yerr = 0.2,xerr = 0.02)

plt.xlim(0,0.7)

plt.show()

代码运行结果: 

参考书:python数据可视化之matplotlib实践 ,作者:刘大成

以上表述有任何错误,希望大家能够批评指正。

文章中如有侵权部分,请联系我删除,很抱歉为您带来困扰! 

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值