python画条形图的具体代码


Posted in Python onApril 20, 2022

本文实例为大家分享了python画条形图的具体代码,供大家参考,具体内容如下

在做毕设的过程中有些数据用表格来展现,会很难看出数据之间的差别,凸显不出数据的特点,所以想制作一个条形图,这里特地记录下,已备以后用到。

import numpy as np
import matplotlib.pyplot as plt
import matplotlib
#指定默认字体
matplotlib.rcParams['font.sans-serif'] = ['FangSong']
matplotlib.rcParams['font.family']='sans-serif'
 
c101= (8,7,3,7,10,4,11,8,11,10,8,9)
c102= (21,21,24,25,16,17,11,24,24,25,36,33)
c103= (4,4,10,3,9,8,12,2,4,5,2,6)
c104= (1,5,1,3,2,3,3,3,6,5,1,0)
c105= (3,1,0,1,3,3,1,2,0,0,0,0)
c106= (1,2,0,0,1,1,0,0,1,1,0,0)
c107= (1,0,1,0,0,1,0,0,0,0,0,0)
c108= (0,1,0,0,0,2,1,1,0,1,0,0)
c109= (1,0,1,1,0,0,1,1,0,0,0,0)
 
ind = np.arange(0,24,2) # the x locations for the groups
width = 0.2  # the width of the bars
 
fig,ax = plt.subplots()
rects1 = ax.bar(ind + width, c101, width, color='SkyBlue',align='edge', label='101')
rects2 = ax.bar(ind + 2*width, c102, width,color='IndianRed',align='edge', label='102')
rects3 = ax.bar(ind + 3*width, c103, width, color='Cyan',align='edge', label='103')
rects4 = ax.bar(ind + 4*width, c104, width, color='Magenta',align='edge', label='104')
rects5 = ax.bar(ind + 5*width, c105, width, color='Purple',align='edge', label='105')
rects6 = ax.bar(ind + 6*width, c106, width, color='Green',align='edge', label='106')
rects7 = ax.bar(ind + 7*width, c107, width, color='Yellow',align='edge', label='107')
rects8 = ax.bar(ind + 8*width, c108, width, color='Blue',align='edge', label='108')
rects9 = ax.bar(ind + 9*width, c109, width, color='Orange',align='edge', label='109')
 
# Add some text for labels, title and custom x-axis tick labels, etc.


#ax.set_title('Scores by group and gender')
plt.xticks(ind,('1班', '2班', '3班', '4班', '5班','6班','7班','8班','9班','10班','11班','12班'))
ax.legend(loc='upper center')
 
plt.show()
fig.savefig('./test77.jpg')

python画条形图的具体代码

竖起来的

%matplotlib notebook
import matplotlib.pyplot as plt
import pandas as pd
import numpy as np
import seaborn as sns
import matplotlib
#指定默认字体
matplotlib.rcParams['font.sans-serif'] = ['FangSong']
matplotlib.rcParams['font.family']='sans-serif'
#解决负号'-'显示为方块的问题
matplotlib.rcParams['axes.unicode_minus'] = False
data = [[8, 21, 4, 1, 3, 1, 1, 0, 1],
        [7,21,4,5,1,2,0,1,0], 
        [3,24,10,1,0,0,1,0,1],
        [7,25,3,3,1,0,0,0,1], 
        [10,16,9,2,3,1,0,0,0], 
        [4,17,8,3,3,1,1,2,0], 
        [11,11,12,3,1,0,0,1,1], 
        [8,24,2,3,2,0,0,1,1], 
        [11,24,4,6,0,1,0,0,0], 
        [10,25,5,5,0,1,0,1,0], 
        [8,36,2,1,0,0,0,0], 
        [9,33,6,0,0,0,0,0]]
df = pd.DataFrame(data,
                 index=['1班','2班','3班','4班','5班','6班','7班','8班','9班','10班','11班','12班'],
                 columns=pd.Index(['101','102','103','104','105','106','107','108','109']),
                 )


df.plot(kind='barh',figsize=(5,8)) 


plt.show()
#fig.savefig('./test2.jpg')

python画条形图的具体代码

以上就是本文的全部内容,希望对大家的学习有所帮助。

Python 相关文章推荐
Python利用pyHook实现监听用户鼠标与键盘事件
Aug 21 Python
浅谈Python2.6和Python3.0中八进制数字表示的区别
Apr 28 Python
Python简单读取json文件功能示例
Nov 30 Python
对Python3 pyc 文件的使用详解
Feb 16 Python
Python创建字典的八种方式
Feb 27 Python
Django框架用户注销功能实现方法分析
May 28 Python
解决pycharm下os.system执行命令返回有中文乱码的问题
Jul 07 Python
Python BeautifulSoup [解决方法] TypeError: list indices must be integers or slices, not str
Aug 07 Python
opencv调整图像亮度对比度的示例代码
Sep 27 Python
Python操作MySQL数据库实例详解【安装、连接、增删改查等】
Jan 17 Python
Python3 Tensorlfow:增加或者减小矩阵维度的实现
May 22 Python
Python 把两层列表展开平铺成一层(5种实现方式)
Apr 07 Python
Python中的matplotlib绘制百分比堆叠柱状图,并为每一个类别设置不同的填充图案
Apr 20 #Python
Pandas 数据编码的十种方法
Apr 20 #Python
Python读取和写入Excel数据
Python 的演示平台支持 WSGI 接口的应用
Apr 20 #Python
python​格式化字符串
Apr 20 #Python
Python编写冷笑话生成器
Apr 20 #Python
Python Django / Flask如何使用Elasticsearch
Apr 19 #Python
You might like
社区(php&&mysql)二
2006/10/09 PHP
PHP常用代码大全(新手入门必备)
2010/06/29 PHP
php中var_export与var_dump的区别分析
2010/08/21 PHP
Yii框架中 find findAll 查找出制定的字段的方法对比
2014/09/10 PHP
基于Laravel5.4实现多字段登录功能方法示例
2017/08/11 PHP
JS字符串累加Array不一定比字符串累加快(根据电脑配置)
2012/05/14 Javascript
动态读取JSON解析键值对的方法
2014/06/03 Javascript
jQuery实现的一个自定义Placeholder属性插件
2014/08/11 Javascript
javascript中SetInterval与setTimeout的定时器用法
2015/08/24 Javascript
javascript的几种继承方法介绍
2016/03/22 Javascript
JavaScript事件处理的方式(三种)
2016/04/26 Javascript
JS实现JSON.stringify的实例代码讲解
2017/02/07 Javascript
JS实现发送短信验证后按钮倒计时功能(防止刷新倒计时失效)
2017/07/07 Javascript
ReactNative短信验证码倒计时控件的实现代码
2017/07/20 Javascript
JavaScript监听手机物理返回键的两种解决方法
2017/08/14 Javascript
JavaScript实现微信红包算法及问题解决方法
2018/04/26 Javascript
AngularJS模态框模板ngDialog的使用详解
2018/05/11 Javascript
Vue项目添加动态浏览器头部title的方法
2018/07/11 Javascript
在Angular中实现一个级联效果的下拉框的示例代码
2020/05/20 Javascript
[02:16]深扒TI7聊天轮盘语音出处2
2017/05/11 DOTA
Python 制作糗事百科爬虫实例
2016/09/22 Python
Python实现树莓派WiFi断线自动重连的实例代码
2017/03/16 Python
快速了解Python开发中的cookie及简单代码示例
2018/01/17 Python
Python下载网络文本数据到本地内存的四种实现方法示例
2018/02/05 Python
10 分钟快速入门 Python3的教程
2019/01/29 Python
PyQt5 加载图片和文本文件的实例
2019/06/14 Python
python区分不同数据类型的方法
2019/10/14 Python
Python实现字符串中某个字母的替代功能
2019/10/21 Python
Python使用py2neo操作图数据库neo4j的方法详解
2020/01/13 Python
sqlalchemy实现时间列自动更新教程
2020/09/02 Python
Python Selenium库的基本使用教程
2021/01/04 Python
Antler英国官网:购买安特丽行李箱、拉杆箱
2019/08/25 全球购物
革命先烈的英雄事迹材料
2014/02/15 职场文书
餐饮周年庆活动方案
2014/08/14 职场文书
群众路线问题查摆对照检查材料
2014/10/04 职场文书
Mysql文件存储图文详解
2021/06/01 MySQL