(一)函数bar()---------绘制柱状图
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,2,7]
plt.bar(x,y,align="center",color="c",tick_label = ["q","a","c","e","r","j","b","p"],hatch="-")
'''
align---->对齐方式
tick_label------->标签
hatch-------->填充内容
'''
#设置x,y标签
plt.xlabel("箱子编号")
plt.ylabel("箱子质量(kg)")
plt.show()
(二)函数barh()--------绘制条形图
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,2,7]
plt.barh(x,y,align="center",color="c",tick_label = ["q","a","c","e","r","j","b","p"],hatch="/")
'''
align---->对齐方式
tick_label------->标签
hatch-------->填充内容
'''
#设置x,y标签
plt.ylabel("箱子编号")
plt.xlabel("箱子质量(kg)")
plt.show()
(三)函数hist()-------------绘制直方图
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 = boxWeight
bins = range(0,11,1)
plt.hist(x, bins = bins, color = "g", histtype="bar", rwidth = 1,alpha=.6)
plt.xlabel("箱子质量(kg)")
plt.ylabel("销售数量(个)")
plt.show()
(四)函数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"]
soldNumbers = [0.0954, 0.4583, 0.1573, 0.3495]
plt.pie(soldNumbers, labels=kinds, autopct="%3.1f%%", startangle=60, colors=colors)
'''
soldNumbers------>传入数据
labels-------->外围标签
autopct----------->精确度
startangle-------->开始角度
colors----------->对应颜色
'''
plt.title("不同种类箱子的销售数量占比")
plt.show()
(五)函数polar()-----------绘制极线图
import numpy as np
import matplotlib.pyplot as plt
barSlices = 12
theta = np.linspace(0.0, 2*np.pi, barSlices,endpoint=False)#角度
r = 30*np.random.rand(barSlices)#值
plt.polar(theta,r,color="chartreuse",linewidth=5,marker="*",mfc="b",ms=6)
#mfc-------->星的颜色 ms-------->星的大小
plt.show()
(六)函数scatter()------------绘制气泡图,用二维表示三维数据
import numpy as np
import matplotlib.pyplot as plt
import matplotlib as mpl
a = np.random.randn(100)
b = np.random.randn(100)
plt.scatter(a, b, s=np.power(10*a+20*b,2),c=np.random.rand(100),cmap=mpl.cm.RdYlBu,marker="o")
'''
a,b为x,y坐标
s---->对应气泡大小
c----->散点标记颜色
cmap----->讲浮点数映射成颜色的颜色映射表
'''
plt.show()
(七)函数stem()-------绘制棉棒图
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(0.5, 2*np.pi, 20)
y = np.random.randn(20)
plt.stem(x, y, linefmt="-.", markerfmt="o", basefmt="--")
'''
x,y------>对应x,y坐标的值
linefmt------>棉棒样式
markerfmt------>棉棒末端样式
basefmt--------->指定基线样式
'''
plt.show()
(八)函数boxplot()---------绘制箱线图
import matplotlib as mpl
import numpy as np
import matplotlib.pyplot as plt
mpl.rcParams["font.sans-serif"] = ["FangSong"]
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=.4)
plt.show()
(九)函数errorbar()--------绘制误差棒图
import numpy as np
import matplotlib.pyplot as plt
x = np.linspace(.1, .6, 6)
y = np.exp(x)
plt.errorbar(x,y,fmt="bo:", yerr=0.2, xerr=.02)
plt.xlim(0, 0.7)
plt.show()