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 判断一个进程是否存在
Apr 09 Python
把MySQL表结构映射为Python中的对象的教程
Apr 07 Python
python查看微信好友是否删除自己
Dec 19 Python
Python实现模拟登录网易邮箱的方法示例
Jul 05 Python
Linux下python制作名片示例
Jul 20 Python
浅述python中深浅拷贝原理
Sep 18 Python
Pandas读写CSV文件的方法示例
Mar 27 Python
Django项目主urls导入应用中views的红线问题解决
Aug 10 Python
pandas按行按列遍历Dataframe的几种方式
Oct 23 Python
python lambda函数及三个常用的高阶函数
Feb 05 Python
Python函数默认参数常见问题及解决方案
Mar 26 Python
Pytest测试框架基本使用方法详解
Nov 25 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
WAMP环境中扩展oracle函数库(oci)
2015/06/26 PHP
php使用gd2绘制基本图形示例(直线、圆、正方形)
2017/02/15 PHP
php实现的统计字数函数定义与使用示例
2017/07/26 PHP
JavaScript 数组循环引起的思考
2010/01/01 Javascript
轻量级 JS ToolTip提示效果
2010/07/20 Javascript
js控制href内容的连接内容的变化示例
2014/04/30 Javascript
js实现window.open不被拦截的解决方法汇总
2014/10/30 Javascript
javascript中cookie对象用法实例分析
2015/01/30 Javascript
js仿QQ邮箱收件人选择与搜索功能
2017/02/10 Javascript
vue.js删除动态绑定的radio的指定项
2017/06/02 Javascript
Node.js v8.0.0正式发布!看看带来了哪些主要新特性
2017/06/02 Javascript
详解基于vue的服务端渲染框架NUXT
2018/06/20 Javascript
对vue中v-if的常见使用方法详解
2018/09/28 Javascript
JS实现的全选、全不选及反选功能【案例】
2019/02/19 Javascript
微信小程序文章详情页跳转案例详解
2019/07/09 Javascript
微信小程序 扭蛋抽奖机css3动画实现详解
2019/07/19 Javascript
[01:38]DOTA2辉夜杯 欢乐的观众现场采访
2015/12/26 DOTA
python批量导出导入MySQL用户的方法
2013/11/15 Python
使用Python3制作TCP端口扫描器
2017/04/17 Python
在Python中执行系统命令的方法示例详解
2017/09/14 Python
Django 跨域请求处理的示例代码
2018/05/02 Python
十分钟搞定pandas(入门教程)
2019/06/21 Python
python中PS 图像调整算法原理之亮度调整
2019/06/28 Python
讲解Python3中NumPy数组寻找特定元素下标的两种方法
2019/08/04 Python
CSS3轻松实现清新 Loading 效果的简单实例
2016/06/06 HTML / CSS
纽约通行卡:The New York Pass(免费游览纽约90多个景点)
2017/07/29 全球购物
瑞典手机壳品牌:Richmond & Finch
2018/04/28 全球购物
外企C语言笔试题
2013/11/10 面试题
如何查找和删除数据库中的重复数据
2014/11/05 面试题
家长给老师的道歉信
2014/01/13 职场文书
单位实习工作证明怎么写
2014/11/02 职场文书
中学生勤俭节约倡议书
2015/04/29 职场文书
歌咏比赛口号大全
2015/12/25 职场文书
2016年教师学习廉政准则心得体会
2016/01/20 职场文书
MySQL 使用自定义变量进行查询优化
2021/05/14 MySQL
Python深度学习之Pytorch初步使用
2021/05/20 Python