matplotlib 实践(3) 绘制统计图形

2019-07-09  本文已影响0人  银色尘埃010

第三章 绘制统计图形

3.1 柱状图

柱状图是描述统计中使用频率非常高的一种统计图形。他有垂直和水平样式两种可视化效果。  

3.1.1 应用场景

定性数据的可视化场景,或者是离散数据的分布展示

3.1.2 绘制原理

bar() 函数的使用  
参数介绍:
import numpy as np
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]
#creat bar
plt.bar(x,y,align="center",tick_label=["A","B","C","D","E"],alpha=0.8,color='g')
plt.xlabel("Test difficult")
plt.ylabel("Examination number")

plt.grid(True,axis="y",ls=":",color="r",alpha=0.5)

plt.show()
output_2_0.png

3.2 条形图

将柱状图中的柱体形状由垂直变成水平,柱状图就变成了条形图,
x = [1,2,3,4,5]
y = [6,10,4,5,1]
#creat bar
plt.barh(x,y,align="center",tick_label=["A","B","C","D","E"],alpha=0.8,color='c')

plt.xlabel("Test difficult")
plt.ylabel("Examination number")
plt.grid(True,axis="x",ls=":",color="r",alpha=0.5)
plt.show()
output_4_0.png

3.3 堆积图

简单而言就是,将若干统计图形堆叠起来,自然是一种组合式图形。结合前面讲过的柱状图和条形图的绘制方法,具体讲解堆积柱状图和推挤条形图的实现方法

3.3.1 堆积柱状图

将bar() 中的参数bottom取值为y,y1=[2,6,3,8,5]代表另外一台试卷的份数,函数bar(x,y1,bottom=y,color="r")
# 代码实现
x = [1,2,3,4,5]
y = [6,10,4,5,1]
y1 = [2,6,3,8,5]
#creat bar
plt.bar(x,y,align="center",tick_label=["A","B","C","D","E"],alpha=0.8,color='g',label="class A")
plt.bar(x,y1,align="center",bottom=y,color="r",label="class B")
plt.xlabel("Test difficult")
plt.ylabel("Examination number")

plt.grid(True,axis="y",ls=":",color="r",alpha=0.5)
plt.legend()
plt.show()
![output_10_0.png](https://img.haomeiwen.com/i14782847/ba0fd88953e54eea.png?imageMogr2/auto-orient/strip%7CimageView2/2/w/1240)

3.3.2 堆积条形图

使用barh(),将参数left的取值设置为列表y 
# 代码实现
x = [1,2,3,4,5]
y = [6,10,4,5,1]
y1 = [2,6,3,8,5]
#creat bar
plt.barh(x,y,align="center",tick_label=["A","B","C","D","E"],alpha=0.8,color='g',label="class A")
plt.barh(x,y1,align="center",left=y,color="r",label="class B")
plt.xlabel("Test difficult")
plt.ylabel("Examination number")

plt.grid(True,axis="x",ls=":",color="r",alpha=0.5)
plt.legend()
plt.show()
output_8_0.png

3.4 分块图

除了堆积图,需要通过借助分块图来对比数据的分布差异。  
分块图可以分为多数据并列柱状图和多数据平行柱状图。

3.4.1 多数据并列柱状图

# 代码实现
# x = [1,2,3,4,5]
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"]
#creat bar
plt.bar(x,y,bar_width,align="center",alpha=0.8,color='g',label="class A")
plt.bar(x+bar_width,y1,bar_width,align="center",color="r",label="class B")
plt.xlabel("Test difficult")
plt.ylabel("Examination number")

plt.grid(True,axis="y",ls=":",color="r",alpha=0.5)
plt.xticks(x+bar_width/2,tick_label)
plt.legend()
plt.show()
output_10_0.png
# 代码实现
# x = [1,2,3,4,5]
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"]
#creat bar
plt.barh(x,y,bar_width,align="center",alpha=0.8,color='g',label="class A")
plt.barh(x+bar_width,y1,bar_width,align="center",color="r",label="class B")
plt.xlabel("Test difficult")
plt.ylabel("Examination number")

plt.grid(True,axis="x",ls=":",color="r",alpha=0.5)
plt.yticks(x+bar_width/2,tick_label)
plt.legend()
plt.show()
output_11_0.png

3.5 参数探索

在柱体上绘制装饰线和装饰图,也就是说设置柱体的填充样式。使用关键字hatch ,取值可以有“/”“\\”,“|”“_”,随着符号的数量越多,柱体的几何图形密集程度越高。
x = [1,2,3,4,5]
y = [6,10,4,5,1]
#creat bar
plt.bar(x,y,hatch="/|\\\_",align="center",tick_label=["A","B","C","D","E"],alpha=0.8,color='g')
plt.xlabel("Test difficult")
plt.ylabel("Examination number")

plt.grid(True,axis="y",ls=":",color="r",alpha=0.5)

plt.show()
output_13_0.png

3.6 堆积折线图、间断条形图和阶梯图

这三种图形是在折线图、柱状图和条形图基础上衍生出来的统计图形。

3.6.1 用函数stackplot()绘制堆积折线图

通过绘制不同数据集的折线图而生成的。堆积折线图按照垂直方向上彼此堆积而又不相互覆盖的排列顺序,绘制若干条折线图而形成的组合图形。
通过使用  
plt.stackplot(x,y,y1,y2,labels=labels,colors=colors)
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 = ["g","r","b"]
colors = ['g','r','b']
plt.stackplot(x,y,y1,y2,labels=labels,colors=colors)
plt.legend(loc="upper left")
plt.show()
output_15_0.png

3.6.2 用函数broken_barh() 绘制间断条形图

主要用来可视化定性数据的相同指标在时间维度上的指标值,实现定性数据的相同指标的变化情况的有效直观比较。
plt.broken_barh([(30,100),(180,50),(260,70)],(20,8),facecolors="g")
plt.broken_barh([(60,90),(190,20),(230,30),(280,60)],(10,8),facecolors=("g","y","r","b"))
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(True,lw = 1,color="r")

plt.show()
output_17_0.png

3.6.3 用step() 绘制阶梯图

阶梯图经常使用在时间序列数据的可视化任务中,凸显时序数据的波动周期和规律。   
在使用step() 绘图的时候,关注where参数的选择,有pre 和 post 可以选择,获得的图形的效果不同
x = np.linspace(1,10,10)
y = np.sin(x)
plt.step(x,y,color="r",where = "pre",lw =2)
plt.step(x,y,color = "g",where = "post",lw=2)
plt.xlim(0,11)
plt.xticks(np.arange(1,11,1))
plt.ylim(-1.2,1.2)
plt.show()
output_19_0.png

3.7 直方图

3.7.1 定量数据的分布展示

直方图主要应用在定量数据的可是化场景中,或者是用来进行连续数据的可视化展示。

3.7.2 直方图和柱状图的关系

直方图描述的是连续型数据的分布,柱状图描述的是离散型的分布。直方图描述定量数据,柱状图描述定性数据。
柱状图之间有间隙,但是直方图之间没有间隙。
scoresT = np.random.randint(0,100,100)
bins = range(0,101,10)
plt.hist(scoresT,bins,histtype="bar",rwidth=10)
plt.xlabel("测试成绩")
plt.ylabel("学生人数")
plt.show()
output_21_0.png

堆积直方图

scoresT1 = np.random.randint(0,100,1000)
scoresT2 = np.random.randint(0,100,1000)
x = [scoresT1,scoresT2]
colors = ["y","r"]
labels = ["班级A","班级B"]
bins = range(0,101,10)

plt.hist(x,bins,histtype="bar",rwidth=10,color=colors,
        stacked = True,label=labels)
# plt.hist(x,bins,histtype="bar",rwidth=10,color=colors,
#         stacked = False,label=labels)

plt.xlabel("测试成绩")
plt.ylabel("学生人数")
plt.show()
output_23_0.png

3.7.5 直方图的不同形状

堆积阶梯直方图
scoresT1 = np.random.randint(0,100,100)
scoresT2 = np.random.randint(0,100,100)
x = [scoresT1,scoresT2]
colors = ["y","r"]
labels = ["班级A","班级B"]
bins = range(0,101,10)

plt.hist(x,bins,histtype="stepfilled",rwidth=10,color=colors,
        stacked = True,label=labels)
# plt.hist(x,bins,histtype="bar",rwidth=10,color=colors,
#         stacked = False,label=labels)

plt.xlabel("测试成绩")
plt.ylabel("学生人数")
plt.show()
output_25_0.png

3.8 饼图 pie

3.8.1 定性数据的比例展示

主要应用在定性数据的可视化场景,或者是用来进行离散型数据的比例展示。

3.8.2 绘制原理

labels = ["A难度水平","B难度水平","C难度水平","D难度水平"]
colors = ["g","y","b","r"]
students = [0.35,0.15,0.20,0.30]
explode = (0.1,0.1,0.1,0.1)

plt.pie(students,explode=explode,
       labels=labels,
       autopct="%3.1f%%",
       startangle=45,
       shadow=True,
       colors=colors)
plt.show()
output_27_0.png

3.8.3 非分列式饼图

去掉参数explode即可
labels = ["A难度水平","B难度水平","C难度水平","D难度水平"]
colors = ["g","y","b","r"]
students = [0.35,0.15,0.20,0.30]
explode = (0.1,0.1,0.1,0.1)

plt.pie(students,
       labels=labels,
       autopct="%3.1f%%",
       startangle=45,
       shadow=True,
       colors=colors)
plt.show()
output_29_0.png

3.8.4 案例 —— 内嵌环形饼图

3.9 箱线图

3.10 误差棒图

References

1、《Python数据可视化之matplotlib实践》 刘大成著

上一篇下一篇

猜你喜欢

热点阅读