1. 柱状图
函数 bar(x,y,align = "center",color = "b",tick_label = ["A","B","C","D","E"],alpha = 0.6)
参数含义:
x:柱状图中的柱体标签值;
y:柱状图中的柱体高度;
align:柱体对齐方式;
color:柱体颜色;
tick_label:刻度标签值;
alpha:柱体透明度
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams["font.sans-serif"] = ["SimHei"]#字体选 SimHei
mpl.rcParams["axes.unicode_minus"] = False
x = [1,2,3,4,5]
y = [6,10,4,5,1]
plt.bar(x,#柱状图中的柱体标签值
y,#柱状图中的柱体高度
align = "center",#柱体对齐方式
color = "b",#柱体颜色
tick_label = ["A","B","C","D","E"],#刻度标签值
alpha = 0.6)#柱体透明度
plt.xlabel = ("测试难度")
plt.ylabel = ("试卷份数")
plt.grid(True,axis = "y",ls = ":",color = "r",alpha = 0.3)
plt.show()
2. 条形图
函数 barh(x,y,align = "ceter",color = "k",tick_label = ["A","B","C","D","E"])
参数含义:
x:y轴上的柱体标签值;
y:柱体的宽度在x轴显示;
align:柱体对齐方式;
color:柱体颜色;
tick_label:刻度标签值;
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]
y = [6,10,4,5,1]
plt.barh(x,#y轴上的柱体标签值
y,#柱体的宽度在x轴显示
align = "center",#柱体对齐方式
color = "c",#柱体颜色
tick_label = ["A","B","C","D","E"])#刻度标签值
plt.ylabel = ("测试难度")
plt.xlabel = ("试卷份数")
plt.grid(True,
axis = "x",
ls = ":",
color = "r",
alpha = 0.3
)
plt.show()
3. 堆积图 将若干统计图形堆叠起来
函数 bar()/barh()
3.1 堆积柱状图
函数 bar(x,y,bottom = y,color = "r")
参数含义:
x:y轴上的柱体标签值;
y:柱体的宽度在x轴显示;
bottom:取值设定为列表y
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]
y = [6,10,4,5,1]
y1 = [2,6,3,8,5]
plt.bar(x,y,
align = "center",
color = "#66c2a5",
tick_label = ["A","B","C","D","E"],
label = "班级A"
)
plt.bar(x,y,
align = "center",
bottom = y,
color = "#8da0cb",
label ="班级B"
)
plt.xlabel = ("测试难度")
plt.ylabel = ("试卷份数")
plt.legend()#两个变量加图例更加直观
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/9de98ef2ba91b218864980fd7e8843ae.png)
3.2 堆积条形图
函数 barh(x,y1,left = y,color = "r")
参数含义:
y1:另一套试卷的份数;
left:取值设定为列表y
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams["font.sans-serif"] = ["simhei"]
mpl.rcParams["axes.unicode_minus"] = False
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]
y = [6,10,4,5,1]
y1 = [2,6,3,8,5]
plt.barh(x,y,
align = "center",
color = "#66c2a5",
tick_label = ["A","B","C","D","E"],
label = "班级A")
plt.barh(x,y1,
align = "center",
left = y,
color = "#8da0cd",
label = "班级B")
plt.ylabel = ("测试难度")
plt.xlabel = ("试卷份数")
plt.legend()
plt.show()
4. 分块图 对比多数据分布差异
4.1 多数据并列柱状图
函数 bar()
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
x = np.arange(5)
y = [6,10,4,5,1]
y1 = [2,6,3,8,5]
bar_width = 0.35
tick_label = ["A","B","C","D","E"]
plt.bar(x,y,
bar_width,color = "c",
align = "center",
label = "班级A",
alpha = 0.5)
plt.bar(x+bar_width,y1,
bar_width,
color = "b",
align = "center",
label = "班级B",
alpha = 0.5
)
#坐标轴标签名字
plt.xlabel = "测试难度"
plt.ylabel = "试卷份数"
plt.xticks(x+bar_width/2,tick_label)
plt.legend()
plt.show()
4.2 多数据平行条形图
函数 barh()
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#负值显示
x = np.arange(5)
y = [6,10,4,5,1]
y1 = [2,6,3,8,5]
bar_width= 0.35
tick_label = ["A","B","C","D","E"]
plt.barh(x,y,
bar_width,
color = "c",
align = "center",
label = "班级A",
alpha = 0.5
)
plt.barh(x + bar_width,
y1,bar_width,
color = "b",
align = "center",
label = "班级B",
alpha = 0.5)
plt.xlabel = "试卷份数"
plt.ylabel = "测试难度"
plt.yticks(x+bar_width/2 , tick_label)
plt.legend()
plt.show()
5. 参数探索 柱体上设置装饰线和装饰图
函数 bar(hatch = "@")
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]
y = [6,10,4,5,1]
plt.bar(x,y,
align = "center",
color = "c",
label = "试卷份数",
tick_label = ["A","B","C","D","E"],
hatch = "///"
)
plt.gca().set_facecolor('yellowgreen')
plt.xlabel = "测试难度"
plt.ylabel = "试卷份数"
plt.legend()
plt.show()
6. 衍生图(折线图,柱状图,条形图基础上衍生出来的图形)
6.1 堆积折线图
函数 stackplot()
'''
堆积折线图 通过绘制不同数据集的折线图生成,
堆积折线图 是按照垂直方向上彼此堆叠且又不相互覆盖的排列顺序,绘制若干条折线图而组成的
'''
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(1,6,1)
y = [0,4,3,5,6]
y1 = [1,3,4,2,7]
y2 = [3,4,1,6,5]
labels = ["BluePlanet","BrownPlanet","GreenPlanet"]
colors = ["#8da0cb","#fc8d62","#66c2a5"]
plt.stackplot(x,y,y1,y2,labels = labels,colors = colors)
plt.legend(loc = "upper left")
plt.show()
6.2 间断条形图
函数 broken_barh()
'''
间断条形图是在条形图的基础上绘制而成的,
主要用来可视化 定性数据 的相同标准在时间维度上的指标值的变化情况,
实现定性数据的相同指标的变化情况的有效直观比较
'''
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["lisu"]#隶书
mpl.rcParams["axes.unicode_minus"] = False
#间断条形图
plt.broken_barh([(30,100),(180,50),(260,70)],
(20,8),
facecolors = "#1f78b4")
plt.broken_barh([(60,90),(190,20),(230,30),(280,60)],#从起点x轴60的位置,x轴正方向移动90单位 为柱体的距离和水平起始位置
(10,8),#起点y轴的数值 为10的位置起,沿y轴正方向移动8个单位 为每一个柱体的高度和垂直起始位置
facecolors = ("#7fc97f","#beaed4","#fdc086","#ffff99"))#柱体填充颜色
plt.xlim(0,360)
plt.ylim(5,35)
plt.xlabel("演出时间")
plt.xticks(np.arange(0,361,60))
plt.yticks([15,25],["歌剧院A","歌剧院B"])
plt.grid(ls = "-",lw = 1,color = "gray")
plt.title("不同地区的歌剧院的演出时间比较")
plt.show()
6.3 阶梯图
函数 step()
'''
阶梯图 反映数据的趋势变化或者 周期 规律的,
在时间序列数据的可视化任务中,凸显 时序数据的波动周期和规律
'''
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(1,10,10)
y = np.sin(x)
#阶梯图
plt.step(x,y,
color = "#8dd3c7",
where = "pre",
lw = 2)
plt.xlim(0,11)
plt.xticks(np.arange(1,11,1))
plt.ylim(-1.2,1.2)
plt.show()
7. 直方图
用来展示连续型数据分布特征的统计图形,直观地分析出数据的集中趋势和波动情况
7.1 应用场景
定量数据的分布展示中
7.2 绘制原理
函数 hist()
参数含义:
x:连续型数据输入值;
bins:用于确定柱体的个数或是柱体边缘范围;
color:柱体的颜色;
histtype:柱体类型;
label:图例内容;
rwidth:柱体的相对宽度,取值范围是[0.0,1.0]
'''
参数含义:
hist(x,bins = bins,color = "b",histtype = "bar",label = "score",rwidth = 10)
x:连续型数据输入值;
bins:用于确定柱体的个数或者柱体边缘范围;
color:柱体的颜色;
histtype:柱体的类型;
label:图例内容;
rwidth:柱体的相对宽度[0-1]
'''
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
scoresT = np.random.randint(0,100,100)
x = scoresT
bins = range(0,101,10)
plt.hist(x,
bins = bins,
color = "#377eb8",
histtype = "bar",
rwidth = 1.0)
plt.xlabel("测试成绩")
plt.ylabel("学生人数")
plt.show()
7.3 直方图和柱状图的关系
一方面:直方图描述的是连续型数据的分布,柱状图描述的是离散型数据的分布;
另一方面:直方图柱体间没有空隙,柱状图柱体间有空隙;
7.4 堆积直方图
函数 hist(stacked = True) 堆积直方图
函数 hist(stacked = False) 并排直方图
# 函数 hist(stacked = True) 堆积直方图
# 函数 hist(stacked = False) 并排直方图
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
scoresT1 = np.random.randint(0,100,100)
scoresT2 = np.random.randint(0,100,100)
plt.hist(x = [scoresT1,scoresT2],
bins = range(0,101,10),
color = ["#8dd3c7","#bebada"],
histtype = "bar",
rwidth = 1.0,
stacked = True,#堆积直方图
# stacked = False,#并排直方图
label = ["班级A","班级B"])
plt.xlabel("测试成绩(分)")
plt.ylabel("学生人数")
plt.title("不同班级的测试成绩的直方图")
plt.legend(loc = "upper left")
plt.show()
7.5 直方图的不同形状
函数 hist(histtype = "stepfilled") 阶梯型直方图
函数 hist(histtype = "stepfilled",stacked = True)堆积阶梯型直方图
# 函数 hist(histtype = "stepfilled") 阶梯型直方图
# 函数 hist(histtype = "stepfilled",stacked = True)堆积阶梯型直方图
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
scoreT1 = np.random.randint(0,100,100)
scoreT2 = np.random.randint(0,100,100)
plt.hist(x = [scoreT1,scoreT2],
color = ["#8dd3c7","#bebada"],
label = ["班级A","班级B"],
rwidth = 1.0,
histtype = "stepfilled",
stacked = True
)
plt.xlabel("测试成绩(分)")
plt.ylabel("学生人数")
plt.title("不同班级的测试成绩的直方图")
plt.legend()
plt.show()
8. 饼图 用来展示定性数据(离散型数据)比例分布特征的统计图形
函数 pie()
'''
函数 pie()
参数含义:
pie(students,explode = explode,labels = labels,autopct = "%3.1f%%",shartangle = 45,shadow = True,colors = colors ,rwidth = 10)
students:饼片代表的百分比;
explode:饼片边缘偏离半径的百分比;
labels:标记饼片的文本标签内容;
autopct:饼片文本标签内容对应的数值百分比样式;
starangle:从x轴作为起始位置,第一个饼片逆时针旋转的角度;
shadow:是否绘制饼片的阴影;
colors:饼片的颜色;
pctdistance 控制百分比数值显示位置
labeldistance 控制标签值显示位置
'''
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams["font.sans-serif"] = ["simhei"]
mpl.rcParams["axes.unicode_minus"] = False
students = [0.35,0.15,0.20,0.30]#饼片代表的百分比
labels = "A难度水平","B难度水平","C难度水平","D难度水平"#标记饼片文本标签内容
plt.pie(students,#饼片代表的百分比
labels = labels,#标签内容
explode = (0.1,0.1,0.1,0.1),#偏离半径
autopct = "%3.1f%%",#文本标签对应的数值百分比样式
startangle = 45,#第一个饼片逆时针旋转角度
shadow = True,#是否绘制饼片的阴影
colors = colors)#饼片的颜色
plt.title("选择不同难度测试试卷的学生占比")
plt.show()
#测试练习
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams["font.sans-serif"] = ["simhei"]
mpl.rcParams["axes.unicode_minus"] = False
students = [2.97,2.87,2.49,2.22,2.14,1.95,1.8,1.49,1.44,1.41]#饼片代表的百分比
labels = ["纽康恩-现结","金妈家","深圳比芙肉食有限公司","物以稀为贵(杭州)供应链管理有限公司","北京我爱小城科技有限公司",
"毕","河南牛匠食品有限公司","荣成福泽交洋贸易有限公司","范总杭州牛茂","徐小军"]#标签内容
explode = (0.2,0.18,0.16,0.14,0.12,0.10,0.08,0.06,0.04,0.02)#偏离半径
colors = ["#377eb8","#4daf4a","#984ea3","#ff7f00","#377eb8","#4daf4a","#984ea3","#ff7f00"]#饼片的颜色
plt.pie(students,#饼片代表的百分比
labels = labels,#标签内容
explode = explode,#偏离半径
autopct = "%3.2f%%",#文本标签对应的数值百分比样式
startangle = 0,#第一个饼片逆时针旋转角度
shadow = True,#是否绘制饼片阴影
colors = colors)#饼片的颜色
plt.title("客户综合加权得分")
plt.show()
8.1 非分裂式饼图
不绘制 分裂式饼图 只需要去除参数explode
此外 以下参数 以半径长度比例值作为显示位置依据的
pctdistance 控制百分比数值
labeldistance 控制标签值显示位置
# 不绘制 分裂式饼图 只需要去除参数explode
# 此外 以下参数 以半径长度比例值作为显示位置依据的
# pctdistance 控制百分比数值显示位置
# labeldistance 控制标签值显示位置
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams["font.sans-serif"] = ["simhei"]
mpl.rcParams["axes.unicode_minus"] = False
students = [0.35,0.15,0.20,0.30]#饼片代表的百分比
labels = "A难度水平","B难度水平","C难度水平","D难度水平"#标记饼片文本标签内容
plt.pie(students,#饼片代表的百分比
labels = labels,#标签内容
# explode = (0.1,0.1,0.1,0.1),#偏离半径
autopct = "%3.1f%%",#文本标签对应的数值百分比样式
startangle = 45,#第一个饼片逆时针旋转角度
pctdistance = 0.7,# pctdistance 控制百分比数值显示位置
labeldistance = 1.2,# labeldistance 控制标签值显示位置
shadow = True,#是否绘制饼片的阴影
colors = colors)#饼片的颜色
plt.title("选择不同难度测试试卷的学生占比")
plt.show()
8.2 内嵌环形饼图 多个饼图嵌套
从单一数据集比例分布情况转换为展示多数据集的比例分布情况
# 从单一数据集比例分布情况转换为展示多数据集的比例分布情况
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["simhei"]
plt.rcParams["axes.unicode_minus"] = False
elements = ["面粉","砂糖","奶油","草莓酱","坚果"]
weight1 = [40,15,20,10,15]
weight2 = [30,25,15,20,10]
colormapList = ["#e41a1c","#377eb8","#4daf4a","#984ea3","#ff7f00"]
outer_colors = colormapList
inner_colors = colormapList
wedges1,texts1,autotexts1 = plt.pie(weight1,
autopct = "%3.1f%%",
radius = 1,
pctdistance = 0.85,
colors = outer_colors,
textprops = dict(color = "w"),
wedgeprops = dict(width = 0.3,edgecolor = "w"))
wedges2,texts2,autotexts2 = plt.pie(weight2,
autopct = "%3.1f%%",
radius = 0.7,
pctdistance = 0.75,
colors = inner_colors,
textprops = dict(color = "w"),
wedgeprops = dict(width = 0.3,edgecolor = "w"))
plt.legend(wedges1,elements,
fontsize = 12,
title = "配料表",
loc = "center left",
bbox_to_anchor = (0.91,0,0.3,1))
plt.setp(autotexts1,size = 15,weight = "bold")
plt.setp(autotexts2,size = 15,weight = "bold")
plt.setp(texts1,size = 12)
plt.title("不同果酱面包配料比例表")
plt.show()
#测试 圆环图
import matplotlib as mpl
import matplotlib.pyplot as plt
plt.rcParams["font.sans-serif"] = ["simhei"]
plt.rcParams["axes.unicode_minus"] = False
elements = ["面粉","砂糖","奶油","草莓酱","坚果"]
weight1 = [40,15,20,10,15]
wedges1,texts1,autotexts1 = plt.pie(weight1,autopct = "%3.1f%%",
radius = 1,
pctdistance = 0.85,
textprops = dict(color = "w"),
wedgeprops = dict(width = 0.3,edgecolor = "w"))
plt.legend(wedges1,elements,
fontsize = 12,
title = "配料表",
loc = "center left",
bbox_to_anchor = (0.91,0,0.3,1))
plt.setp(autotexts1,size = 10,weight = "bold")
plt.setp(texts1,size = 15)
plt.title("不同果酱面包配料比例表")
plt.show()
9. 箱线图 箱体(四分位数)和箱须 组成 (多组定量数据的分布比较)
9.1 应用场景
一系列测量或观测数据的比较场景中
箱须的末端以外的数值 可以理解成离群值 ,箱须是对一组数据范围的大致直观描述
9.2 绘制原理
函数 boxplot()
参数含义:
testList:绘制箱线图的输入数据;
whis:四分位间距的倍数,用来确定箱须包含数据的范围的大小;
widths:设置箱体的宽度;
sym:离散值的标记样式;
labels:绘制每一个数据集的刻度标签;
patch_artist:是否给箱体添加颜色;
'''
函数 boxplot()
参数含义:
testList:绘制箱线图的输入数据;
whis:四分位间距的倍数,用来确定箱须包含数据的范围的大小;
widths:设置箱体的宽度;
sym:离散值的标记样式;
labels:绘制每一个数据集的刻度标签;
patch_artist:是否给箱体添加颜色;
vert #箱线图是否纵向显示
showfliers #箱线图的离群值是否显示
'''
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["font.sans-serif"] = ["fangsong"]
plt.rcParams["axes.unicode_minus"] = False
testA = np.random.randn(5000)
testB = np.random.randn(5000)
testList = [testA,testB]
labels = ["随机数生成器AlphaRM","随机数生成器BetaRM"]#绘制每一个数据集的刻度标签
whis = 1.6 #四分位间距的倍数
width = 0.35 #设置箱体的宽度
bplot = plt.boxplot(testList,#绘制箱线图的输入数据
whis = whis,#四分位间距的倍数,用来确定箱须包含数据的范围的大小
widths = width,#设置箱体的宽度
sym = "o",#离散值的标记样式
labels = labels,#绘制每一个数据集的刻度标签
patch_artist = True)#是否给箱体添加颜色
for patch,color in zip(bplot["boxes"],colors):
patch.set_facecolor(color)
plt.ylabel("随机数值")
plt.title("生成器抗干扰能力的稳定性比较")
plt.grid(axis = "y",ls = ":",lw = 1,color = "gray",alpha = 0.4)
plt.show()
#测试 notch = True,#变成V型凹痕箱体
'''
vert #箱线图是否纵向显示
showfliers #箱线图的离群值是否显示
'''
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
plt.rcParams["font.sans-serif"] = ["fangsong"]
plt.rcParams["axes.unicode_minus"] = False
testA = np.random.randn(5000)
testB = np.random.randn(5000)
testList = [testA,testB]
labels = ["随机数生成器AlphaRM","随机数生成器BetaRM"]#绘制每一个数据集的刻度标签
whis = 1.6 #四分位间距的倍数
width = 0.35 #设置箱体的宽度
bplot = plt.boxplot(testList,#绘制箱线图的输入数据
whis = whis,#四分位间距的倍数,用来确定箱须包含数据的范围的大小
widths = width,#设置箱体的宽度
sym = "o",#离散值的标记样式
labels = labels,#绘制每一个数据集的刻度标签
notch = True,#变成V型凹痕箱体
vert = False,#箱线图是否纵向显示
showfliers = False,#箱线图的离群值是否显示
patch_artist = True)#是否给箱体添加颜色
#用键'boxes' 来调出值"bplot["boxes"]" ,再用拉链函数 zip() 生成元组列表,使用 for循环 填充颜色
for patch,color in zip(bplot["boxes"],colors):
patch.set_facecolor(color)
plt.ylabel("随机数值")
plt.title("生成器抗干扰能力的稳定性比较")
plt.grid(axis = "y",ls = ":",lw = 1,color = "gray",alpha = 0.4)
plt.show()
9.3 箱体,箱须,离群值的含义和计算方法
#计算四分位数
import numpy as np
data = [10,1,25,11,24,13,54,4,42,-3]
pc = np.array(data)
np.percentile(pc,np.arange(0,100,25))
运行结果: array([-3. , 5.5 , 12. , 24.75])
#确定四分位数的范围[Q1-whis*IQR,Q3-whis*IQR] 其中 IQR = Q3 - Q1
#q1-whis*(q3-q1) < value < q3+whis*(q3-q1)
Q1 = 5.5#第一四分位数
Q3 = 24.75#第三四分位数
whis = 1.5
madian = 12#中位数
IQR = Q3-Q1
MIN = Q1 - whis * IQR
MAX = Q3 + whis * IQR
print("IQR =",IQR)
print("MIN =",round(MIN))
print("MAX =",round(MAX))
运行结果: IQR = 19.25 MIN = -23 MAX = 54
9.4 案例1---水平方向的箱线图
boxplot(vert = False)
# boxplot(vert = False)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["fangsong"]
mpl.rcParams["axes.unicode_minus"] = False
x = np.random.randn(1000)
plt.boxplot(x,vert = False)
plt.xlabel("随机数值")
plt.yticks([1],["随机数生成器AlphaRM"],rotation = 90)
plt.title("随机数生成器抗干扰能力的稳定性")
# plt.grid(True),用于显示点状网格线,它有助于辅助构图
plt.grid(axis = "x",
ls = ":",
lw = 1,
color = "gray",
alpha = 0.4)
plt.show()
9.5 案例2---不绘制离群值的水平方向的箱线图
boxplot(vert = False,showfliers = False)
# boxplot(vert = False,showfliers = False)
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["fangsong"]
mpl.rcParams["axes.unicode_minus"] = False
x = np.random.randn(100)
plt.boxplot(x,vert = False,showfliers = False)
plt.xlabel("随机数值")
plt.yticks([1],["随机数生成器AlphaRM"],rotation = 90)
plt.title("随机数生成器抗干扰能力的稳定性")
plt.grid(axis = "x",
ls = ":",
lw = 1,
color = "gray",
alpha = 0.4)
plt.show()
![](https://i-blog.csdnimg.cn/blog_migrate/1278210c92b1d92c0f4af4e9440e1695.png)
10. 误差棒图 可以给实验结果增加观测结果的误差以表示客观存在的测量偏差
函数 errorbar()
参数含义:
x,y:数据点的位置;
yerr:单一数值的标记样式和数据点标记的连接线样式;
fmt:数据点的标记样式和数据点标记的连接线样式;
ecolor:误差棒的线条颜色;
elinewidth:误差棒的线条粗细;
ms:数据点的大小;
mfc:数据点的标记颜色;
mec:数据点的标记边缘颜色;
capthick:误差棒边界横杠的厚度;
capsize:误差棒边界横杠的大小;
'''
误差棒图样式:1.水平误差棒图 2.垂直误差棒图 3.对称误差棒图 4.非对称误差棒图
函数 errorbar()
参数含义:
x,y:数据点的位置;
yerr:单一数值的标记样式和数据点标记的连接线样式;
fmt:数据点的标记样式和数据点标记的连接线样式;
ecolor:误差棒的线条颜色;
elinewidth:误差棒的线条粗细;
ms:数据点的大小;
mfc:数据点的标记颜色;
mec:数据点的标记边缘颜色;
capthick:误差棒边界横杠的厚度;
capsize:误差棒边界横杠的大小;
'''
#P68 例3.10.2 绘制原理
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["fangsong"]
mpl.rcParams["axes.unicode_minus"] = False
x = np.linspace(0.1,0.6,10)
y = np.exp(x)
error = 0.05 + 0.15 * x
lower_error = error
upper_error = 0.3 * error
error_limit = [lower_error,upper_error]
plt.errorbar(x,y,#数据点的位置
yerr = error_limit,#单一数值的标记样式和数据点标记的连接线样式
fmt = ":o",#数据点的标记样式和数据点标记的连接线样式
ecolor = "y",#误差棒的线条颜色
elinewidth = 4,#误差棒的线条粗细
ms = 5,#数据点的大小
mfc = "c",#数据点的标记颜色
mec = "r",#数据点的标记边缘颜色
capthick = 1,#误差棒边界横杠的厚度
capsize = 2#误差棒边界横杠的大小
)
plt.xlim = (0,0.7)
plt.show()
#测试 误差棒图
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["fangsong"]
mpl.rcParams["axes.unicode_minus"] = False
x = np.linspace(0.1,0.6,10)
y = np.exp(x)
error = 0.05 + 0.15 * x
lower_error = error
upper_error = 0.3 * error
error_limit = [lower_error,upper_error]
plt.errorbar(x,y,#数据点的位置
yerr = error_limit,#单一数值的标记样式和数据点标记的连接线样式
fmt = ":o",#数据点的标记样式和数据点标记的连接线样式
ecolor = "y",#误差棒的线条颜色
elinewidth = 4,#误差棒的线条粗细
ms = 5,#数据点的大小
mfc = "c",#数据点的标记颜色
mec = "r",#数据点的标记边缘颜色
capthick = 1,#误差棒边界横杠的厚度
capsize = 2#误差棒边界横杠的大小
)
plt.xlim = (0,0.7)
plt.show()
10.1 案例1---带误差棒的柱状图
#案例1---带误差棒的柱状图
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["lisu"]
mpl.rcParams["axes.unicode_minus"] = False
x = np.arange(5)
y = [100,68,79,91,82]
std_err = [7,2,6,10,5]
error_attri = dict(elinewidth = 2,ecolor = "black",capsize = 3)
plt.bar(x,y,
color = "c",
width = 0.6,
align = "center",
yerr = std_err,
error_kw = error_attri,
tick_label = ["园区1","园区2","园区3","园区4","园区5"]
)
plt.xlabel("芒果种植区")
plt.ylabel("收割量")
plt.title("不同芒果种植区的单次收割量")
plt.show()
10.2 案例2---带误差棒的条形图
#案例2---带误差棒的条形图
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["lisu"]
mpl.rcParams["axes.unicode_minus"] = False
x = np.arange(5)
y = [1200,2400,1800,2200,1600]
str_err = [150,100,180,130,80]
bar_width = 0.6
colors = ["#e41a1c","#377eb8","#4daf4a","#984ea3","#ff7f00"]
plt.barh(x,y,
bar_width,
color = colors,
align = "center",
xerr = str_err,
tick_label = ["家庭","小说","心理","科技","儿童"]
)
plt.xlabel("订购数量")
plt.ylabel("图书种类")
plt.title("大型图书展销会的不同图书种类的采购情况")
plt.grid(True,axis = "x",ls = ":",color = "gray",alpha = 0.2)
# plt.xlim(0,2600) #元组对象不可调用
plt.show()
10.3 案例3---带误差棒的多数据并列柱状图
#案例3---带误差棒的多数据并列柱状图
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["lisu"]
mpl.rcParams["axes.unicode_minus"] = False
x = np.arange(5)
y1 = [100,68,79,91,82]
y2 = [120,75,70,78,85]
std_err1 = [7,2,6,10,5]
std_err2 = [5,1,4,8,9]
error_attri = dict(elinewidth = 2,ecolor = "black",capsize = 3)
bar_width = 0.4
tick_label = ["园区1","园区2","园区3","园区4","园区5"]
plt.bar(x,y1,
bar_width,
color = "#87ceeb",
align = "center",
yerr = std_err1,
error_kw = error_attri,
label = "2010"
)
plt.bar(x+bar_width,y2,
bar_width,
color = "#cd5c5c",
align = "center",
yerr = std_err2,
error_kw = error_attri,
label = "2013"
)
plt.xlabel("芒果种植区")
plt.ylabel("收割量")
plt.xticks(x + bar_width/2,tick_label)
plt.title("不同年份的芒果种植区的单次收割量")
plt.grid(True,axis = "y",ls = ":",color = "gray",alpha = 0.2)
plt.legend()
plt.show()
10.4 案例4---带误差棒的堆积柱状图
#P74 例3.10.6 案例4---带误差棒的堆积柱状图
import matplotlib as mpl
import matplotlib.pyplot as plt
import numpy as np
mpl.rcParams["font.sans-serif"] = ["lisu"]
mpl.rcParams["axes.unicode_minus"] = False
x = np.arange(5)
y1 = [1200,2400,1800,2200,1600]
y2 = [1050,2100,1300,1600,1340]
std_err1 = [150,100,180,130,80]
std_err2 = [120,110,170,150,120]
bar_width = 0.6
tick_label = ["家庭","小说","心理","科技","儿童"]
plt.bar(x,y1,
bar_width,
color = "#6495ed",
align = "center",
yerr = std_err1,
label = "地区1",
error_kw = error_attri
)
plt.bar(x,y2,
bar_width,
bottom = y1,#bottom的值应该是[第0个类别~(当前类别-1)]的数据总和
color = "#ffa500",
align = "center",
label = "地区2",
error_kw = error_attri
)
plt.xlabel("图书种类")
plt.ylabel("订购数量")
plt.title("不同地区大型图书展销会的图书采购情况")
plt.grid(True,axis = "y",ls = ":",color = "gray",alpha = 0.2)
plt.xticks(x,tick_label)
plt.legend()
plt.show()