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中 __get__和__getattr__和__getattribute__的区别
Jun 16 Python
Python生成短uuid的方法实例详解
May 29 Python
python去除文件中重复的行实例
Jun 29 Python
python实现彩色图转换成灰度图
Jan 15 Python
spark dataframe 将一列展开,把该列所有值都变成新列的方法
Jan 29 Python
详解用Python实现自动化监控远程服务器
May 18 Python
Python进程间通信Queue消息队列用法分析
May 22 Python
python3 mmh3安装及使用方法
Oct 09 Python
Win10下python 2.7与python 3.7双环境安装教程图解
Oct 12 Python
信号生成及DFT的python实现方式
Feb 25 Python
python3.7中安装paddleocr及paddlepaddle包的多种方法
Nov 27 Python
Python基于百度AI实现抓取表情包
Jun 27 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实现时间轴函数代码
2011/10/08 PHP
PHP实现的QQ空间g_tk加密算法
2015/07/09 PHP
PHP简单计算两个时间差的方法示例
2017/06/20 PHP
索趣科技的答案
2007/02/07 Javascript
javascript写的日历类(基于pj)
2010/12/28 Javascript
为Javascript中的String对象添加去除左右空格的方法(示例代码)
2013/11/30 Javascript
ExtJs中gridpanel分组后组名排序实例代码
2013/12/02 Javascript
一个不错的js html页面倒计时可精确到秒
2014/10/22 Javascript
js字符串完全替换函数分享
2014/12/03 Javascript
浅谈js中的延迟执行和定时执行
2016/05/31 Javascript
jQuery仿写百度百科的目录树
2017/01/03 Javascript
Angular实现模版驱动表单的自定义校验功能(密码确认为例)
2018/05/17 Javascript
vue指令v-html使用过滤器filters功能实例
2019/10/25 Javascript
Element Alert警告的具体使用方法
2020/07/27 Javascript
Python访问MySQL封装的常用类实例
2014/11/11 Python
Fiddler如何抓取手机APP数据包
2016/01/22 Python
Python实现微信消息防撤回功能的实例代码
2019/04/29 Python
Django使用中间件解决前后端同源策略问题
2019/09/02 Python
python实现tail -f 功能
2020/01/17 Python
如何在Python对Excel进行读取
2020/06/04 Python
python正则表达式 匹配反斜杠的操作方法
2020/08/07 Python
Python selenium实现断言3种方法解析
2020/09/08 Python
python如何对链表操作
2020/10/10 Python
Html5 postMessage实现跨域消息传递
2016/03/11 HTML / CSS
后勤岗位职责
2013/11/26 职场文书
学生实习介绍信
2014/01/15 职场文书
毕业典礼演讲稿
2014/05/13 职场文书
消防安全宣传标语
2014/06/07 职场文书
环保口号大全
2014/06/12 职场文书
实验室的标语
2014/06/20 职场文书
机械电子工程专业求职信
2014/06/22 职场文书
财务会计求职信范文
2015/03/20 职场文书
2015年大学社团工作总结
2015/04/09 职场文书
法律意见书范文
2015/06/04 职场文书
感恩老师主题班会
2015/08/12 职场文书
2016高考寄语集锦
2015/12/04 职场文书