python读取excel数据并且画图的实现示例


Posted in Python onFebruary 08, 2021

一,要读取的数据的格式:

python读取excel数据并且画图的实现示例

二,数据读取部分:

b站视频参考:https://www.bilibili.com/video/BV14C4y1W7Nj?t=148

# 1930
workbook=xlrd.open_workbook('1930.xlsx')
sheet= workbook.sheet_by_index(0)
A1=[]
B1=[]
# sheet.cell_value(i,0):第i行的第0个元素
for i in range(1,sheet.nrows):
 A1.append(sheet.cell_value(i,0))
 B1.append(sheet.cell_value(i,1))
 
if len(A1)!=len(B1):
 print("False")
drawBar(A1,B1,1930)

三,画图函数

1. def drawBar(Music_genre,singer_num,year)

参数介绍

参数名 参数含义
Music_genre 音乐流派名称list
singer_num 音乐流派对应音乐家数量list
year 读的文件的年份(因为源代码是从1840到2020的)
def drawBar(Music_genre,singer_num,year):
 arr_len=len(Music_genre)
 # 由循环得到一个字典,key是音乐流派,value是这个音乐流派对应的音乐家的数量
 i=0
 dict_music_singer={}
 while i<arr_len:
 dict_music_singer[Music_genre[i]]=singer_num[i]
 i=i+1
 
	# 注释1
 pyplot.bar(x=0, bottom=range(arr_len), height=0.3, width=singer_num, orientation="horizontal")
 # 注释2
 pyplot.yticks(range(arr_len),Music_genre)
 # 加title,展示图像
 pyplot.title(year)
 pyplot.show()
 
 ...
 ...
 drawBar(A1,B1,1930)

注释1:

"""
 水平条形图,需要修改以下属性
 orientation="horizontal"
"""
import numpy as np
import matplotlib.pyplot as plt
 
# 数据
N = 5
x = [20, 10, 30, 25, 15]
y = [0,1,2,3,4]
 
# 绘图 x= 起始位置, bottom= 水平条的底部(左侧), y轴, height 水平条的宽度, width 水平条的长度
p1 = plt.bar(x=0, bottom=y, height=0.5, width=x, orientation="horizontal")
pyplot.bar(range(arr_len),singer_num,align='center')
pyplot.bar(x=0, bottom=range(arr_len), height=0.5, width=singer_num, orientation="horizontal")
# 展示图形
plt.show()

python读取excel数据并且画图的实现示例

注释2:plt.xticks的第一个参数和plt.plot的第一个参数一样,第二个参数是和第一个参数相同长度的list此例中用来代替横坐标

import matplotlib.pyplot as plt
x = [1, 2, 3, 4]
y = [1, 4, 9, 6]
labels = ['Frogs', 'Hogs', 'Bogs', 'Slogs']
 
plt.plot(x, y)
# You can specify a rotation for the tick labels in degrees or with keywords.
plt.xticks(x, labels, rotation='vertical')
# Pad margins so that markers don't get clipped by the axes
plt.margins(0.2)
# Tweak spacing to prevent clipping of tick-labels
plt.subplots_adjust(bottom=0.15)
plt.show()

python读取excel数据并且画图的实现示例

1.1 效果:

python读取excel数据并且画图的实现示例

1.2 完整代码

import pandas as pd
import numpy as np 
import xlrd
from matplotlib import pyplot
def drawBar(Music_genre,singer_num,year):
 arr_len=len(Music_genre)
 
 i=0
 dict_music_singer={}
 while i<arr_len:
 dict_music_singer[Music_genre[i]]=singer_num[i]
 i=i+1
 #pyplot.bar(range(arr_len),singer_num,align='center')
 pyplot.bar(x=0, bottom=range(arr_len), height=0.3, width=singer_num, orientation="horizontal")
 pyplot.yticks(range(arr_len),Music_genre)
 pyplot.title(year)
 pyplot.show()
 
 
# 1930
workbook=xlrd.open_workbook('1930.xlsx')
sheet= workbook.sheet_by_index(0)
A1=[]
B1=[]
for i in range(1,sheet.nrows):
 A1.append(sheet.cell_value(i,0))
 B1.append(sheet.cell_value(i,1))
 
if len(A1)!=len(B1):
 print("False")
drawBar(A1,B1,1930)
 
 
 
# 1940
workbook=xlrd.open_workbook('1940.xlsx')
sheet= workbook.sheet_by_index(0)
A2=[]
B2=[]
for i in range(1,sheet.nrows):
 A2.append(sheet.cell_value(i,0))
 B2.append(sheet.cell_value(i,1))
 
if len(A2)!=len(B2):
 print("False")
drawBar(A2,B2,1940)
 
 
 
# 
workbook=xlrd.open_workbook('1950.xlsx')
sheet= workbook.sheet_by_index(0)
A3=[]
B3=[]
for i in range(1,sheet.nrows):
 A3.append(sheet.cell_value(i,0))
 B3.append(sheet.cell_value(i,1))
 
if len(A3)!=len(B3):
 print("False")
drawBar(A3,B3,1950)
 
 
 
# 6
workbook=xlrd.open_workbook('1960.xlsx')
sheet= workbook.sheet_by_index(0)
A4=[]
B4=[]
for i in range(1,sheet.nrows):
 A4.append(sheet.cell_value(i,0))
 B4.append(sheet.cell_value(i,1))
 
if len(A4)!=len(B4):
 print("False")
drawBar(A4,B4,1960)
 
 
 
 
# 
workbook=xlrd.open_workbook('1970.xlsx')
sheet= workbook.sheet_by_index(0)
A5=[]
B5=[]
for i in range(1,sheet.nrows):
 A5.append(sheet.cell_value(i,0))
 B5.append(sheet.cell_value(i,1))
 
if len(A5)!=len(B5):
 print("False")
drawBar(A5,B5,1970)
 
 
 
 
# 
workbook=xlrd.open_workbook('1980.xlsx')
sheet= workbook.sheet_by_index(0)
A6=[]
B6=[]
for i in range(1,sheet.nrows):
 A6.append(sheet.cell_value(i,0))
 B6.append(sheet.cell_value(i,1))
 
if len(A6)!=len(B6):
 print("False")
drawBar(A6,B6,1980)
 
 
 
 
# 9
workbook=xlrd.open_workbook('1990.xlsx')
sheet= workbook.sheet_by_index(0)
A7=[]
B7=[]
for i in range(1,sheet.nrows):
 A7.append(sheet.cell_value(i,0))
 B7.append(sheet.cell_value(i,1))
 
if len(A7)!=len(B7):
 print("False")
drawBar(A7,B7,1990)
 
 
 
 
# 2000
workbook=xlrd.open_workbook('2000.xlsx')
sheet= workbook.sheet_by_index(0)
A8=[]
B8=[]
for i in range(1,sheet.nrows):
 A8.append(sheet.cell_value(i,0))
 B8.append(sheet.cell_value(i,1))
 
if len(A8)!=len(B8):
 print("False")
drawBar(A8,B8,2000)
 
 
 
 
# 
workbook=xlrd.open_workbook('2010.xlsx')
sheet= workbook.sheet_by_index(0)
A9=[]
B9=[]
for i in range(1,sheet.nrows):
 A9.append(sheet.cell_value(i,0))
 B9.append(sheet.cell_value(i,1))
 
if len(A9)!=len(B9):
 print("False")
drawBar(A9,B9,2010)
 
 
 
 
# # 
# workbook=xlrd.open_workbook('2020.xlsx')
# sheet= workbook.sheet_by_index(0)
# A2=[]
# B2=[]
# for i in range(1,sheet.nrows):
# A2.append(sheet.cell_value(i,0))
# B2.append(sheet.cell_value(i,1))
 
# if len(A2)!=len(B2):
# print("False")
# drawBar(A2,B2,2020)

以上就是python读取excel数据并且画图的实现示例的详细内容,更多关于python读取excel数据并且画图的资料请关注三水点靠木其它相关文章!

Python 相关文章推荐
详解Python的Django框架中的通用视图
May 04 Python
Python 网页解析HTMLParse的实例详解
Aug 10 Python
python之pandas用法大全
Mar 13 Python
一个简单的python爬虫程序 爬取豆瓣热度Top100以内的电影信息
Apr 17 Python
Python从列表推导到zip()函数的5种技巧总结
Oct 23 Python
python双端队列原理、实现与使用方法分析
Nov 27 Python
OpenCV哈里斯(Harris)角点检测的实现
Jan 15 Python
哈工大自然语言处理工具箱之ltp在windows10下的安装使用教程
May 07 Python
Python通过字典映射函数实现switch
Nov 06 Python
Python根据URL地址下载文件并保存至对应目录的实现
Nov 15 Python
Python基础之数据类型知识汇总
May 18 Python
浅谈Python协程asyncio
Jun 20 Python
Python爬取某平台短视频的方法
Feb 08 #Python
利用Python批量识别电子账单数据的方法
Feb 08 #Python
Python命令行参数argv和argparse该如何使用
Feb 08 #Python
python 实现Requests发送带cookies的请求
Feb 08 #Python
PyCharm2020.3.2安装超详细教程
Feb 08 #Python
python 30行代码实现蚂蚁森林自动偷能量
Feb 08 #Python
如何用Python编写一个电子考勤系统
Feb 08 #Python
You might like
Terran兵种介绍
2020/03/14 星际争霸
yii框架结合charjs实现统计30天数据的方法
2020/04/04 PHP
写出更好的JavaScript之undefined篇(上)
2009/11/22 Javascript
Textbox控件注册回车事件及触发按钮提交事件具体实现
2013/03/04 Javascript
Struts2的s:radio标签使用及用jquery添加change事件
2013/04/08 Javascript
js实现checkbox全选和反选示例
2014/05/01 Javascript
javascript实现验证身份证号的有效性并提示
2015/04/30 Javascript
超详细的JS弹出窗口代码大全
2020/04/18 Javascript
基于slideout.js实现移动端侧边栏滑动特效
2016/11/28 Javascript
微信小程序 获取当前地理位置和经纬度实例代码
2016/12/05 Javascript
JS沙箱模式实例分析
2017/09/04 Javascript
详解JavaScript基础知识(JSON、Function对象、原型、引用类型)
2018/01/16 Javascript
vue中子组件的methods中获取到props中的值方法
2018/08/27 Javascript
前后端如何实现登录token拦截校验详解
2018/09/03 Javascript
ng-events类似ionic中Events的angular全局事件
2018/09/05 Javascript
vue学习笔记之过滤器的基本使用方法实例分析
2020/02/01 Javascript
[01:20:38]完美世界DOTA2联赛 GXR vs IO 第一场 11.07
2020/11/09 DOTA
深入浅析Python中join 和 split详解(推荐)
2016/06/30 Python
Tensorflow 自带可视化Tensorboard使用方法(附项目代码)
2018/02/10 Python
python pandas库的安装和创建
2019/01/10 Python
python 动态生成变量名以及动态获取变量的变量名方法
2019/01/20 Python
Python3使用PySynth制作音乐的方法
2019/09/09 Python
Pytorch之finetune使用详解
2020/01/18 Python
python使用pandas抽样训练数据中某个类别实例
2020/02/28 Python
python3通过qq邮箱发送邮件以及附件
2020/05/20 Python
python爬虫泛滥的解决方法详解
2020/11/25 Python
css3实现一个div设置多张背景图片及background-image属性实例演示
2017/08/10 HTML / CSS
css3翻牌翻数字的示例代码
2020/02/07 HTML / CSS
JD Sports澳洲官网:英国领先的运动鞋和运动时尚零售商
2020/02/15 全球购物
blueseventy官网:铁人三项和比赛泳衣
2021/02/06 全球购物
《手指教学》反思
2014/02/14 职场文书
优秀教师申报材料
2014/12/16 职场文书
员工辞退通知书
2015/04/17 职场文书
2019年度政务公开考核工作总结模板
2019/11/11 职场文书
JS Object构造函数之Object.freeze
2021/04/28 Javascript
使用RedisTemplat实现简单的分布式锁
2021/11/20 Redis