1 函数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]
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()
plt.bar(x,y,align='center',
color="b",tick_label=['A',"B","C","D","E"],alpha=0.6)
plt.bar(柱状图的柱体标签值,柱状图中的柱体高度,柱体对齐方式,
柱体颜色,刻度标签值,柱体透明度)
2 函数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]
y=[6,10,4,5,1]
plt.barh(x,y,align='center',color="c",tick_label=['A',"B","C","D","E"])
plt.xlabel('试卷份数')
plt.ylabel('测试难度')
plt.grid(True,axis="x",ls=":",color="r",alpha=0.3)
# axis :取值both,x或y,指绘制哪个方向的网格
plt.show()
3 堆积柱状图
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,y1,align='center',bottom=y,color='#8da0cd',label='班级B')
# bottom=y 基于y值画y1的图
plt.xlabel('测试难度')
plt.ylabel('试卷份数')
plt.legend() # 图例
plt.show()
4 堆积条形图
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='