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中使用HTMLParser解析HTML的教程
Apr 29 Python
简单介绍Python中的round()方法
May 15 Python
Python中在脚本中引用其他文件函数的实现方法
Jun 23 Python
python并发编程之线程实例解析
Dec 27 Python
Python实现的求解最大公约数算法示例
May 03 Python
python+POP3实现批量下载邮件附件
Jun 19 Python
python logging模块的使用总结
Jul 09 Python
用python实现英文字母和相应序数转换的方法
Sep 18 Python
Python压缩模块zipfile实现原理及用法解析
Aug 14 Python
Python Selenium破解滑块验证码最新版(GEETEST95%以上通过率)
Jan 29 Python
python3判断IP地址的方法
Mar 04 Python
通过Python把学姐照片做成拼图游戏
Feb 15 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
用Socket发送电子邮件(利用需要验证的SMTP服务器)
2006/10/09 PHP
laravel安装zend opcache加速器教程
2015/03/02 PHP
WordPres对前端页面调试时的两个PHP函数使用小技巧
2015/12/22 PHP
PHP动态地创建属性和方法, 对象的复制, 对象的比较,加载指定的文件,自动加载类文件,命名空间
2016/05/06 PHP
基于JQuery的asp.net树实现代码
2010/11/30 Javascript
jQuery效果 slideToggle() 方法(在隐藏和显示之间切换)
2011/06/28 Javascript
jQuery EasyUI API 中文文档 - NumberBox数字框
2011/10/13 Javascript
JavaScript中的作用域链和闭包
2012/06/30 Javascript
点击显示指定元素隐藏其他同辈元素的方法
2014/02/19 Javascript
jquery实现弹出div,始终显示在屏幕正中间的简单实例
2014/03/08 Javascript
JavaScript-RegExp对象只能使用一次问题解决方法
2014/06/23 Javascript
理解JavaScript中Promise的使用
2016/01/18 Javascript
JS正则表达式比较常见用法
2016/01/26 Javascript
JavaScript中的prototype原型学习指南
2016/05/09 Javascript
Javascript中的对象和原型(二)
2016/08/12 Javascript
vue2.0 与 bootstrap datetimepicker的结合使用实例
2017/05/22 Javascript
Node.js v8.0.0正式发布!看看带来了哪些主要新特性
2017/06/02 Javascript
Vue.js实例方法之生命周期详解
2017/07/03 Javascript
JS实现微信摇一摇原理解析
2017/07/22 Javascript
Node.JS使用Sequelize操作MySQL的示例代码
2017/10/09 Javascript
bootstrap 通过加减按钮实现输入框组功能
2017/11/15 Javascript
React/Redux应用使用Async/Await的方法
2017/11/16 Javascript
利用jquery如何从json中读取数据追加到html中
2017/12/01 jQuery
vue如何进行动画的封装
2018/09/26 Javascript
vue远程加载sfc组件思路详解
2019/12/25 Javascript
[01:51]开启你的城市传奇 完美世界城市挑战赛开始报名
2018/10/09 DOTA
用Python生成器实现微线程编程的教程
2015/04/13 Python
Python使用redis pool的一种单例实现方式
2016/04/16 Python
Scrapy爬虫实例讲解_校花网
2017/10/23 Python
Python学习笔记之For循环用法详解
2019/08/14 Python
python pandas.DataFrame.loc函数使用详解
2020/03/26 Python
Python代码覆盖率统计工具coverage.py用法详解
2020/11/25 Python
HTML5 MiranaVideo播放器 (代码开源)
2010/06/11 HTML / CSS
关于爱国的演讲稿
2014/05/07 职场文书
幼师辞职信范文
2015/02/27 职场文书
市场营销计划书
2019/04/24 职场文书