日期:2024.03.14
内容:将matplotlib的常用方法做一个记录,方便后续查找。
from matplotlib import pyplot as plt # 设置画布大小,横坐标想分开,就要使用到子图 plt.figure(figsize=(20,8),dpi = 300) # 全局设置中文字体 plt.rcParams['font.sans-serif'] = ['Simhei'] # 绘制条形图 name = ["战狼2","速度与激情8","功夫瑜伽","西游伏妖篇","变形金刚5:\n最后的骑士" ,"!爸爸","加勒比海盗5:死无对证","金刚:岛","极限特工:终极回归","生化危终章","乘风破浪","神偷奶爸3","智取威虎山","大闹天","金刚狼3:殊死一战","蜘英雄归来","悟空传","银河护卫队2","情圣","新木乃伊"] data = [56.01,26.94,17.53,16,49,15.45,12.96,11.8,11.61,11.28,11.12,10.49,10.3,8.75,7.55,6.99,6.88,6.86,6.58,6.23] # 一般条形图 plt.bar(range(len(name)),data,width = 0.3,label = "电影数据分布图") # 使用xticks可以将name中的内容作为横坐标 x_ticks = list(range(len(name))) x_label = name plt.xticks(x_ticks,x_label,rotation = 90,fontsize = 8) # 展示图例 plt.legend() # 展示数据 plt.show()
# 横着的条形图 from matplotlib import pyplot as plt # 设置画布大小,横坐标想分开,就要使用到子图 plt.figure(figsize=(20,8),dpi = 300) # 全局设置中文字体 plt.rcParams['font.sans-serif'] = ['Simhei'] # plt.barh()绘制横条形图的方法 plt.barh(range(len(name)),data,height = 0.5,color = "red") y_ticks = list(range(len(name))) y_label = name plt.yticks(y_ticks,y_label,fontsize = 8) # 添加网格 plt.grid(0.3) plt.show()