条形图和之前的折线图的唯一区别就是绘制时是bar((纵向条形图)或者barh(横向条形图)
方法
语法知识
复习一下语法知识:
- 设置字体:
fm = font_manager.FontProperties(fname="字体包存放路径"
) - 设置x提示语
plt.xlabel("x_label_name",fontproperties=fm)
- 设置y提示语
plt.ylabel("y_label_name",fontproperties=fm)
- 设置标题
plt.title("title_name",fontproperties=fm)
- 设置横坐标
plt.xticks(x_value_list,x_ticks_list,fontproperties=fm,ratation=n)
- 设置纵坐标
plt.yticks(y_value_list,y_ticks_list,fontproperties=fm,rotation=n)
- 绘制纵向条形图
plt.bar(x,y,color="color_name",width=n)
- 绘制横向向条形图
plt.barh(x,y,color="color_name",height=n)
- 显示图片
plt.show()
- 设置画布大小
plt.figure(figsize=(m,n),dpi=p
- 设置图例:
plt.legend(loc="upper right",prop = fm)
- 保存图片:
plt.savefig("savepath+figname")
: # 保存图片至savepath并命名为figname - 显示网格:
plt.grid(alpha=n)
其中最重要的是:
绘制纵向条形图
plt.bar(x,y,color="color_name",width=n)
绘制横向向条形图
plt.barh(x,y,color="color_name",height=n)
Demo01:绘制电影票房的纵向条形图
from matplotlib import pyplot as plt from matplotlib import font_manager fm = font_manager.FontProperties(fname = 'F:\HuaDaBoSi\scratch\practice\img\AlimamaDaoLiTi.ttf') plt.figure(figsize=(20,8),dpi=80) movie_name = ["长津湖之水门桥","独行月球","这个杀手不太冷静","人生大事","万里归途","奇迹笨小孩","侏罗纪世界3","熊出没之重返地球","阿凡达:水之道","神探大战"] box_office = [51,34,32,31,25,24,22,19,14,10] plt.bar(range(len(movie_name)),box_office,width=0.6,color="orange") plt.xticks(range(len(movie_name)),movie_name,rotation=45,fontproperties=fm) plt.title("2022年票房排名前十电影",fontproperties=fm) plt.xlabel("电影名",fontproperties=fm) plt.ylabel("票房",fontproperties=fm) plt.grid(alpha=0.3) plt.show() plt.savefig("./images/bar.png")
纵向条形图.png
Demo02:绘制电影票房的横向条形图
from matplotlib import pyplot as plt from matplotlib import font_manager fm = font_manager.FontProperties(fname = 'F:\HuaDaBoSi\scratch\practice\img\AlimamaDaoLiTi.ttf') plt.figure(figsize=(20,8),dpi=80) movie_name = ["长津湖之水门桥","独行月球","这个杀手不太冷静","人生大事","万里归途","奇迹笨小孩","侏罗纪世界3","熊出没之重返地球","阿凡达:水之道","神探大战"] box_office = [51,34,32,31,25,24,22,19,14,10] plt.barh(range(len(movie_name)),box_office,height=0.6,color="green") plt.yticks(range(len(movie_name)),movie_name,fontproperties=fm) plt.title("2022年票房排名前十电影",fontproperties=fm) plt.ylabel("电影名",fontproperties=fm) plt.xlabel("票房",fontproperties=fm) plt.grid(alpha=0.3) plt.show() plt.savefig("./images/barh.png")
横向条形图.png
Demo03:绘制电影票房的纵向条形图
from matplotlib import pyplot as plt from matplotlib import font_manager fm = font_manager.FontProperties(fname = 'F:\HuaDaBoSi\scratch\practice\img\AlimamaDaoLiTi.ttf') movie_name = ["长津湖之水门桥","独行月球","这个杀手不太冷静"] box_office_1 = [51,34,32] box_office_2 = [31,25,24] box_office_3 = [22,19,14] box_width = 0.2 list_1 = list(range(len(movie_name))) list_2 = list(i+box_width for i in list_1) list_3 = list(i+box_width*2 for i in list_1) plt.bar(list_1,box_office_1,color="orange",width=box_width,label="6月1号") plt.bar(list_2,box_office_2,color="green",width=box_width,label="6月2号") plt.bar(list_3,box_office_3,color="blue",width=box_width,label="6月3号") plt.xticks(list_2,movie_name,fontproperties = fm) plt.legend(loc="upper right",prop=fm) plt.show() plt.savefig('./images/multi_bar.png')