用matplotlib绘制一些大家比较熟悉又经常混淆的统计图形,掌握这些统计图形可以对数据可视化有一个深入理解。
1.函数bar()--用于绘制柱状图
在x轴上绘制定性数据的分布特征
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,7,8]
y=[3,1,4,5,8,9,7,2]
plt.bar(x,y,align="center",color="c",tick_label=["q","a","c","e","r","j","b","p"],hatch="/")
plt.xlabel("箱子编号")
plt.ylabel("箱子重量(kg)")
plt.show()
2.函数barh()--用于绘制条形图
在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,6,7,8]
y=[3,1,4,5,8,9,7,2]
plt.barh(x,y,align="center",color="c",tick_label=["q","a","c","e","r","j","b","p"],hatch="/")
plt.xlabel("箱子编号")
plt.ylabel("箱子重量(kg)")
plt.show()
3.函数hist()--用于绘制直方图
在x轴上绘制定量数据的分布特征
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
#set test scores
boxWeight = np.random.randint(0,10,100)
x = boxWeight
#plot histogram
bins = range(0,11,1)
plt.hist(x,bins=bins,
color="g",
histtype="bar",
rwidth=1,
alpha=0.6)
#set x,y-axis label
plt.xlabel("箱子重量(kg)")
plt.ylabel("销售数量(个)")
plt.show()
4.函数pie()--用于绘制饼图
绘制定性数据的不同类别的百分比
import matplotlib as mpl
import matplotlib.pyplot as plt
mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False
kinds ="简易箱","保温箱","行李箱","密封箱"
colors = ["#e41a1c","#377eb8","#4daf4a","#984ea3"]
soldNums = [0.05,0.45,0.15,0.35]
#pie chart
plt.pie(soldNums,
labels=kinds,
autopct="%3.1f%%",
startangle=60,
colors=colors)
plt.title("不同类型箱子的销售数量占比")
plt.show()
5.函数polar()--用于绘制极线图
在极坐标图上绘制折线图
import matplotlib.pyplot as plt
import numpy as np
barSlices = 12
theta = np.linspace(0.0,2*np.pi,barSlices,endpoint=False)
r = 30*np.random.rand(barSlices)
plt.polar(theta,r, #theta每个标记所在射线与极径的夹角,r每个标记到原点的距离
color="chartreuse",
linewidth=2,
marker="*",
mfc="b",
ms=10)
plt.show()
6.函数scatter()--用于绘制气泡图
二维数据借助气泡图大小展示三维数据
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
a = np.random.randn(100)
b = np.random.randn(100)
#colormap:RdYlBu
plt.scatter(a,b,s=np.power(10*a+20*b,2),#s散点标记的大小
c=np.random.rand(100),#c散点标记的颜色
cmap=mpl.cm.RdYlBu,#将浮点数映射成颜色的颜色映射表
marker='o')
plt.show()
7.函数stem()--用于绘制棉棒图
绘制离散的有序数据
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="-")
linefmt棉棒的样式、markerfmt棉棒末端的样式、basefmt指定基线的样式
plt.show()
8.函数boxplot()--用于绘制箱型图
绘制箱型图
import matplotlib.pyplot as plt
import matplotlib as mpl
import numpy as np
mpl.rcParams["font.sans-serif"]=["SimHei"]
mpl.rcParams["axes.unicode_minus"]=False
x = np.random.randn(1000)
plt.boxplot(x)
plt.xticks([1],["随机数生成器AlphaRM"])
plt.ylabel("随机数值")
plt.title("随机数生成器抗干扰能力的稳定性")
plt.grid(axis="y",ls=":",lw=1,color="gray",alpha=0.4)
plt.show()
9.函数errorbar()--用于绘制误差棒图
绘制y轴方向或是x轴方向的误差范围
import matplotlib.pyplot as plt
import numpy as np
x = np.linspace(0.1,0.6,6)
y = np.exp(x)
plt.errorbar(x,y,fmt="bo:",yerr=0.2,xerr=0.02)
plt.xlim(0,0.7)
plt.show()