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 02 Python
Python基础教程之浅拷贝和深拷贝实例详解
Jul 15 Python
Python 将RGB图像转换为Pytho灰度图像的实例
Nov 14 Python
Python温度转换实例分析
Jan 17 Python
python3爬取各类天气信息
Feb 24 Python
深入浅析Python获取对象信息的函数type()、isinstance()、dir()
Sep 17 Python
Python如何调用JS文件中的函数
Aug 16 Python
python3 使用traceback定位异常实例
Mar 09 Python
Python关键字及可变参数*args,**kw原理解析
Apr 04 Python
Python实现手势识别
Oct 21 Python
python 逐步回归算法
Apr 06 Python
Python实现打乒乓小游戏
Sep 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
与文件上传有关的php配置参数总结
2013/06/14 PHP
php数组添加与删除单元的常用函数实例分析
2015/02/16 PHP
php array_walk 对数组中的每个元素应用用户自定义函数详解
2016/11/18 PHP
php计数排序算法的实现代码(附四个实例代码)
2020/03/31 PHP
JavaScript CSS修改学习第一章 查找位置
2010/02/19 Javascript
js弹出模式对话框,并接收回传值的方法
2013/03/12 Javascript
JS Jquery 遍历,筛选页面元素 自动完成(实现代码)
2013/07/08 Javascript
Javascript 多物体运动的实现
2014/12/24 Javascript
JS+CSS实现鼠标滑过时动态翻滚的导航条效果
2015/09/24 Javascript
基于vue-video-player自定义播放器的方法
2018/03/21 Javascript
Node.js的进程管理的深入理解
2019/01/09 Javascript
原生js通过一行代码实现简易轮播图
2019/06/05 Javascript
JS相册图片抖动放大展示效果的示例代码
2021/01/29 Javascript
Python操作MySQL模拟银行转账
2018/03/12 Python
解决Python pandas plot输出图形中显示中文乱码问题
2018/12/12 Python
Python整数与Numpy数据溢出问题解决
2019/09/11 Python
使用Python爬虫库requests发送表单数据和JSON数据
2020/01/25 Python
canvas简单连线动画的实现代码
2020/02/04 HTML / CSS
马克华菲官方商城:Mark Fairwhale
2016/09/04 全球购物
WebSphere面试题:在WebSphere里面如何部署一个应用
2015/08/02 面试题
信息专业本科生个人的自我评价
2013/10/28 职场文书
安全生产宣传标语
2014/06/06 职场文书
小学生读书活动总结
2014/06/30 职场文书
新闻发布会活动策划方案
2014/09/15 职场文书
乡党政领导班子群众路线教育实践活动个人对照检查材料
2014/09/20 职场文书
机关干部四风问题自查报告及整改措施
2014/10/26 职场文书
2014年质量管理工作总结
2014/12/01 职场文书
小班上学期幼儿评语
2014/12/30 职场文书
党校毕业个人总结
2015/02/28 职场文书
2015年员工试用期工作总结
2015/05/28 职场文书
党员转正意见怎么写
2015/06/03 职场文书
农村婚礼司仪主持词
2015/06/29 职场文书
Java数组与堆栈相关知识总结
2021/06/29 Java/Android
python turtle绘图命令及案例
2021/11/23 Python
《Estab Life》4月6日播出 正式PV、主视觉图公开
2022/03/20 日漫
MySQL外键约束(Foreign Key)案例详解
2022/06/28 MySQL