pythonmatplotlib画图流程(python3使用matplotlib绘制条形图)
类别:脚本大全 浏览量:1755
时间:2021-11-05 14:06:02 pythonmatplotlib画图流程
python3使用matplotlib绘制条形图本文实例为大家分享了python3使用matplotlib绘制条形图的具体代码,供大家参考,具体内容如下
绘制竖状条形图
代码
|
from matplotlib import pyplot as plt from matplotlib import font_manager my_font = font_manager.fontproperties(fname = "/usr/share/fonts/truetype/arphic/ukai.ttc" ) a = [ "战狼2" , "速度与激情8" , "功夫瑜伽" , "西游伏妖篇" , "变形金刚5:最后的骑士" , "摔跤吧!爸爸" , "加勒比海盗5:死无对证" , "金刚:骷髅岛" , "极限特工:终极回归" , "生化危机6:终章" , "乘风破浪" , "神偷奶爸3" , "智取威虎山" , "大闹天竺" , "金刚狼3:殊死一战" , "蜘蛛侠:英雄归来" , "悟空传" , "银河护卫队2" , "情圣" , "新木乃伊" , ] b = [ 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 , 7.32 , 6.99 , 6.88 , 6.86 , 6.58 , 6.23 ] plt.figure(figsize = ( 15 , 7 )) # 绘制条形图 plt.bar( range ( len (a)),b,width = 0.3 ) # 对应x轴与字符串 plt.xticks( range ( len (a)),a,fontproperties = my_font,rotation = 90 ) plt.savefig( "./bar1.jpg" alt="pythonmatplotlib画图流程(python3使用matplotlib绘制条形图)" border="0" /> plt.show() |
效果图
绘制横状条形图
代码
|
# 绘制横着的条形图 from matplotlib import pyplot as plt from matplotlib import font_manager my_font = font_manager.fontproperties(fname = "/usr/share/fonts/truetype/arphic/ukai.ttc" ) a = [ "战狼2" , "速度与激情8" , "功夫瑜伽" , "西游伏妖篇" , "变形金刚5:最后的骑士" , "摔跤吧!爸爸" , "加勒比海盗5:死无对证" , "金刚:骷髅岛" , "极限特工:终极回归" , "生化危机6:终章" , "乘风破浪" , "神偷奶爸3" , "智取威虎山" , "大闹天竺" , "金刚狼3:殊死一战" , "蜘蛛侠:英雄归来" , "悟空传" , "银河护卫队2" , "情圣" , "新木乃伊" , ] b = [ 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 , 7.32 , 6.99 , 6.88 , 6.86 , 6.58 , 6.23 ] plt.figure(figsize = ( 15 , 7 )) # 绘制条形图 plt.barh( range ( len (a)), b, height = 0.3 ,color = 'orange' ) # 对应x轴与字符串 plt.yticks( range ( len (a)), a, fontproperties = my_font, rotation = 0 ) # 添加网格 alpha参数是设置网格的透明度的 plt.grid(alpha = 0.3 ) # 保存图片 plt.savefig( "./bar1.jpg" alt="pythonmatplotlib画图流程(python3使用matplotlib绘制条形图)" border="0" /> plt.show() |
效果图
需要注意的是横着的和竖着的条形图的区别在与横着的使用的是barh()方法,同时要注意它传参数的顺序是:
|
def barh(y, width, height = 0.8 , left = none, * , align = 'center' , * * kwargs): |
绘制多次条形图
代码
|
from matplotlib import pyplot as plt from matplotlib import font_manager myfont = font_manager.fontproperties(fname = "/usr/share/fonts/truetype/arphic/ukai.ttc" ) a = [ "猩球崛起3:终极之战" , "敦刻尔克" , "蜘蛛侠:英雄归来" , "战狼2" ] b_16 = [ 15746 , 312 , 4497 , 319 ] b_15 = [ 12357 , 156 , 2045 , 168 ] b_14 = [ 2358 , 399 , 2358 , 362 ] bar_width = 0.25 x_14 = list ( range ( len (a))) x_15 = list (i + bar_width for i in x_14) x_16 = list (i + bar_width for i in x_15) # 设置图形大小 plt.figure(figsize = ( 20 , 8 ),dpi = 80 ) plt.bar( range ( len (a)),b_14,width = bar_width,label = "9月14日" ) plt.bar(x_15,b_15,width = bar_width,label = "9月15日" ) plt.bar(x_16,b_16,width = bar_width,label = "9月16日" ) # 设置图例 plt.legend(prop = myfont) # 设置x轴刻度 plt.xticks(x_15,a,fontproperties = myfont) plt.savefig( "./mutiy.jpg" alt="pythonmatplotlib画图流程(python3使用matplotlib绘制条形图)" border="0" /> plt.show() |
效果图
以上就是本文的全部内容,希望对大家的学习有所帮助,也希望大家多多支持开心学习网。
原文链接:https://blog.csdn.net/qq_41009846/article/details/85017125
您可能感兴趣
- python strip用法(Python3.5内置模块之shelve模块、xml模块、configparser模块、hashlib、hmac模块用法分析)
- python函数基本使用(Python3中exp函数用法分析)
- linux下python安装pip(在Docker中的ubuntu中安装Python3和Pip的问题)
- python语言提供的3种基本数据类型(详解Python3 基本数据类型)
- python获取天气源(Python3爬虫之自动查询天气并实现语音播报)
- python3.9.6生成的注册表(厉害了,Python也能操作注册表)
- python的基本函数及用法(Python3.6.x中内置函数总结及讲解)
- python中字符串常用函数或方法(Python3.5字符串常用操作实例详解)
- python3.7手册中文版(Python3.4解释器用法简单示例)
- python3.5 tkinter教程(解决python3.5 正常安装 却不能直接使用Tkinter包的问题)
- 怎么用python实现链表(Python3实现的判断回文链表算法示例)
- python在mysql创建数据库(python3对接mysql数据库实例详解)
- python数字形式转换详解(Python3.5实现的罗马数字转换成整数功能示例)
- python序列定义(详解Python3序列赋值、序列解包)
- pythonselenium接口自动测试(python3+selenium自动化测试框架详解)
- python与php(解决Python3 被PHP程序调用执行返回乱码的问题)
- 吉林神秘传染链跨省 传染源尚未找到,舒兰 封城(吉林神秘传染链跨省)
- 吉林舒兰 封城 聚集性疫情传播链已延至沈阳,有一个细节让人忧心(吉林舒兰封城)
- 1天密接者猛增77人,患者轨迹透露危险信号 吉林市全面封闭管理(1天密接者猛增77人)
- 吉林舒兰 封城 15人确诊 276人隔离,出现跨省传播(吉林舒兰封城)
- 四月新番CP人气榜公布,《剃须》两度上榜,沙优不是女朋友(四月新番CP人气榜公布)
- 2019年外媒秋季新番动画角色CP排行榜,桐人和爱丽丝落榜(2019年外媒秋季新番动画角色CP排行榜)
热门推荐
- javascript四种数组(javascript数组includes、reduce的基本使用)
- jQuery对象和DOM的相互转换
- 使用docker在linux环境中部署springboot包的教程(使用docker在linux环境中部署springboot包的教程)
- JavaScript instanceof 的用法
- php 数据库查询并写入(PHP实现单条sql执行多个数据的insert语句方法)
- 服务器虚拟化需要哪些技术(认识云服务器的虚拟化实现途径)
- thinkphp从模板自定义标签(Thinkphp5.0框架视图view的循环标签用法示例)
- python怎样看字符unicode编码(Python3中编码与解码之Unicode与bytes的讲解)
- docker如何简化部署(Docker使用Portainer搭建可视化界面的方法)
- dedecms默认模板目录(DEDECMS 5.7 将data目录迁移后,网站地图无法打开和更新的解决方法)
排行榜
- 1
- 2
- 3
- 4
- 5
- 6
- 7
- 8
- 9