一、堆积图
1、堆积柱状图
如果将函数bar()中的参数bottom的取值设定为列表y、列表y1代表另一个数,函数bar(x,y1,bottom=y,color="r")就会输出堆积柱状图
代码示例:
import matplotlib.pyplot as plt
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="title_A")
plt.bar(x,y1,align="center",color="#8da0cb",tick_label=["A","B","C","D","E"],label="title_B")
plt.legend()
plt.show()
图像输出:
2、堆积条状图
如果将函数barh()中的参数left的取值设定为列表y,列表y1代表另一数据,函数bar(x,y1,left=y,color="r")
代码示例:
import matplotlib.pyplot as plt
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="title_A")
plt.barh(x,y1,align="center",left=y,color="#8da0cb",tick_label=["A","B","C","D","E"],label="title_B")
plt.legend()
plt.show()
图像输出:
二、分块图
1、多数据并列柱状图
代码示例:
import matplotlib.pyplot as plt
import numpy as np
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,align="center",color="c",width=bar_width,label="title_A",alpha=0.5)
plt.bar(x+bar_width,y1,align="center",color="b",width=bar_width,label="title_B",alpha=0.5)
plt.xticks(x+bar_width/2,tick_label)
plt.legend()
plt.show()
输出图像:
2、多数据平行条状图
代码示例:
import matplotlib.pyplot as plt
import numpy as np
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,align="center",color="c",label="title_A",alpha=0.5)
plt.barh(x+bar_width,y1,bar_width,align="center",color="b",label="title_B",alpha=0.5)
plt.yticks(x+bar_width/2,tick_label)
plt.legend()
plt.show()
输出图像:
三、堆积折线图、间断条形图和阶梯图
1、函数stackplot() —— 绘制堆积折线图
代码示例:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(0,5,1)
y = [0,4,3,5,6]
y1 = [1,3,4,2,7]
y2 = [3,4,1,6,5]
labels = ["Blue","Brown","Green"]
colors = ["#8da0cb","#fc8d62","#66c2a5"]
plt.stackplot(x,y,y1,y2,labels=labels,colors=colors)
plt.legend(loc="upper left")
plt.show()
图像输出:
2、函数broken_barh() —— 绘制间断条形图
代码示例:
import matplotlib.pyplot as plt
import numpy as np
plt.broken_barh([(30,100),(180,50),(260,70)],(20,8),facecolors="#1f78b4")
plt.broken_barh([(60,90),(190,520),(230,30),(280,60)],(10,8),facecolors=("#7fc97f","#beaed4","#fdc086","#ffff99"))
plt.xlim(0,360)
plt.ylim(5,35)
plt.xticks(np.arange(0,361,60))
plt.yticks([15,25],["A","B"])
plt.grid(ls="-",lw=1,color="gray")
plt.show()
图像输出:
3、函数step() —— 绘制阶梯图
参数说明:
where:默认参数为“pre”,
参数值“pre”表示x轴上的每个数据点对应y轴上的数值向左侧绘制水平线直到x轴上的此数据点的左侧相邻数据点为止,
也就是说,x轴上的相邻数据点的取值是按照左开右闭区间进行选取的。
除了可以取“pre”,还可以取“post”,“post”指的是x轴上的相邻数据点的取值按照左闭右开的数据点选取,
然后用对应的y轴上的数据向右绘制水平线直到x轴上的数据点右侧相邻数据点为止。
代码示例:
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()
图像输出:
四、直方图
1、hist()
hist(x,bins=bins,color="b",histtype="bar",label="score",rwidth=10)
参数说明:
x:连续型数据的输入值
bins:用于确定柱体的个数或是柱体边缘的范围
color:柱体的颜色
histtype:柱体类型
label:图例内容
rwidth:柱体宽度
2、堆积直方图
代码实例:
import matplotlib.pyplot as plt
import numpy as np
x1 = np.random.randint(0,100,100)
x2 = np.random.randint(0,100,100)
x = [x1,x2]
colors = ["#8dd3c7","#bebada"]
labels = ["A","B"]
bins = range(0,101,10)
plt.hist(x,bins=bins,color=colors,histtype="bar",rwidth=10,stacked=True,label=labels,edgecolor = 'k')
plt.legend(loc="upper left")
plt.show()
堆积图像输出:
对比:不堆积并列(即:stacked=False)效果图输出 :
3、直方图与阶梯图结合对比
代码实例:
import matplotlib.pyplot as plt
import numpy as np
x1 = np.random.randint(0,100,100)
x2 = np.random.randint(0,100,100)
x = [x1,x2]
colors = ["#8dd3c7","#bebada"]
labels = ["A","B"]
bins = range(0,101,10)
plt.hist(x,bins=bins,color=colors,histtype="stepfilled",rwidth=10,stacked=True,label=labels,edgecolor = 'k')
plt.legend(loc="upper right")
plt.show()
图像输出:
五、饼图
1、分列式饼图
代码实现:
import matplotlib.pyplot as plt
labels = ["A","B","C","D"]
nums = [0.35,0.15,0.2,0.3]
colors = ["#377eb8","#4daf4a","#984ea3","#ff7f00"]
explode = (0.1,0.1,0.1,0.1)
plt.pie(nums,explode=explode,labels=labels,autopct="%3.1f%%",startangle=45,shadow=True,colors=colors)
plt.show()
图像输出:
2、非分裂式饼图
代码示例:
import matplotlib.pyplot as plt
labels = ["A","B","C","D"]
nums = [0.35,0.15,0.2,0.3]
colors = ["#377eb8","#4daf4a","#984ea3","#ff7f00"]
plt.pie(nums,labels=labels,autopct="%3.1f%%",startangle=45,colors=colors,pctdistance=0.7)
plt.show()
图像输出:
3、内嵌环形饼图
代码示例:
import matplotlib.pyplot as plt
labels = ["A","B","C","D","E"]
nums1 = [40,15,20,10,15]
nums2 = [30,25,15,20,10]
colors = ["#e41a1c","#377eb8","#4daf4a","#984ea3","#ff7f00"]
w1, t1, a1 = plt.pie(nums1,autopct="%3.1f%%",radius=1,pctdistance=0.85,colors=colors,textprops=dict(color="w"),wedgeprops=dict(width=0.3,edgecolor="w"))
w2, t2, a2 = plt.pie(nums2,autopct="%3.1f%%",radius=0.7,pctdistance=0.75,colors=colors,textprops=dict(color="w"),wedgeprops=dict(width=0.3,edgecolor="w"))
plt.legend(nums1,labels,fontsize=12,bbox_to_anchor=(0.91,0,0.3,1))
plt.setp(a1,size=15,weight="bold")
plt.setp(a2,size=15,weight="bold")
plt.setp(t1,size=12)
plt.show()
图像输出:
六、箱线图
plt.boxplot(testList,whis=whis,widths=width,sym="o",labels=labels,patch_artist=True)
参数说明:
testList:绘制箱线图的输入数据
whis:四分位间距的倍数,用来确定箱须包含数据的范围大小
widths:设置箱体的宽度
sym:离群值的标记样式
labels:绘制每一个数据集的刻度标签
patch_artist:是否给箱体添加颜色
代码实例:
import matplotlib.pyplot as plt
import numpy as np
t1 = np.random.randn(5000)
t2 = np.random.randn(5000)
testList = [t1,t2]
labels = ["A","B"]
colors = ["#1b9e77","#d95f02"]
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.grid(axis="y",ls=":",lw=1,color="gray",alpha=0.4)
plt.show()
图像输出:
2、水平方向箱线图
代码示例:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(1000)
plt.boxplot(x,vert=False)
plt.grid(axis="x",ls=":",lw=1,color="gray",alpha=0.4)
plt.show()
图像输出:
3、没有离群值的水平箱线图
代码示例:
import matplotlib.pyplot as plt
import numpy as np
x = np.random.randn(1000)
plt.boxplot(x,vert=False,showfliers=False)
plt.grid(axis="x",ls=":",lw=1,color="gray",alpha=0.4)
plt.show()
图像输出:
七、误差棒图
1、定量数据的误差范围
代码示例:
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1,0.6,10)
y = np.exp(x)
err = 0.05 + 0.15 * x
low_err = err
upp_err = err * 0.3
err_lim = [low_err,upp_err]
plt.errorbar(x,y,yerr=err_lim,fmt=":o",ecolor="y",elinewidth=4,ms=5,mfc='r',capthick=1,capsize=2)
plt.xlim(0,0.7)
plt.show()
图像输出:
参数说明:
x,y : 数据点的位置
yerr:单一数值的非对称形式误差范围 //误差范围非对称形式,而且数据点下方的误差范围大于数据点上方的误差范围,xerr类似
fmt:数据点标记样式和数据点标记的连接线样式
ecolor:误差棒的线条颜色
elinewidth:误差棒的线条粗细
ms:数据点的大小
mfc:数据点的标记颜色
mec:数据点的标记边沿颜色
capthick:误差棒边界横杠的厚度
capsize:误差棒边界横杠的大小
2、带误差棒的柱状图
代码实例:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y = [100,68,79,91,82]
err = [7,2,6,10,5]
err_att = dict(elinewidth=2,ecolor="black",casize=3)
plt.bar(x,y,color="c",width=0.6,align="center",yerr=err,error_kw=err_att,tick_label=["A","B","C","D","E"])
plt.grid(True,axis="y",ls=":",color="gray",alpha=0.2)
plt.show()
图像输出:
3、带误差棒的条形图
代码实例:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y = [1200,2400,1800,2200,1600]
err = [150,100,180,130,100]
bar_width = 0.6
colors = ["#377eb8","#4daf4a","#984ea3","#ff7f00","#e41a1c"]
plt.barh(x,y,bar_width,color=colors,align="center",xerr=err,tick_label=["A","B","C","D","E"])
plt.grid(True,axis="x",ls=":",color="gray",alpha=0.2)
plt.show()
图像输出:
4、带误差棒的多数据并列柱状图
代码实例:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y1 = [100,68,79,91,82]
y2 = [120,75,70,78,85]
err1 = [7,2,6,10,5]
err2 = [5,1,4,8,9]
err_att = dict(elinewidth=2,ecolor="black",capize=3)
bar_width = 0.4
tick_label=["A","B","C","D","E"]
plt.bar(x,y1,bar_width,color="#87ceeb",align="center",yerr=err1,error_kw=err_att,label="NO.1")
plt.bar(x+bar_width,y2,bar_width,color="#cd5c5c",align="center",yerr=err2,error_kw=err_att,label="NO.2")
plt.xticks(x+bar_width/2,tick_label)
plt.grid(True,axis="y",ls=":",color="gray",alpha=0.2)
plt.legend()
plt.show()
图像输出:
5、带误差棒的堆积柱状图
代码实例:
import matplotlib.pyplot as plt
import numpy as np
x = np.arange(5)
y1 = [1200,2400,1800,2200,1600]
y2 = [1050,2100,1300,1600,1340]
err1 = [150,100,180,130,80]
err2 = [120,110,170,150,120]
err_att = dict(elinewidth=2,ecolor="black",capize=0)
bar_width = 0.6
tick_label=["A","B","C","D","E"]
plt.bar(x,y1,bar_width,color="#6495ed",align="center",yerr=err1,error_kw=err_att,label="NO.1")
plt.bar(x,y2,bar_width,color="#ffa500",align="center",yerr=err2,error_kw=err_att,label="NO.2")
plt.xticks(x,tick_label)
plt.grid(True,axis="y",ls=":",color="gray",alpha=0.2)
plt.legend()
plt.show()
图像输出: