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()

注释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()

1.1 效果:

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数据并且画图的资料请关注我们其它相关文章!

(0)

相关推荐

  • Python matplotlib读取excel数据并用for循环画多个子图subplot操作

    读取excel数据需要用到xlrd模块,在命令行运行下面命令进行安装 pip install xlrd 表格内容大致如下,有若干sheet,每个sheet记录了同一所学校的所有学生成绩,分为语文.数学.英语.综合.总分 考号 姓名 班级 学校 语文 数学 英语 综合 总分 ... ... ... ... 136 136 100 57 429 ... ... ... ... 128 106 70 54 358 ... ... ... ... 110.5 62 92 44 308.5 画多张子图需要

  • python3:excel操作之读取数据并返回字典 + 写入的案例

    excel写入数据,使用openpyxl库 class WriteExcel: def __init__(self,path): self.path = path def write_excel(self, sheet_name, content): """ 在excel指定sheet中的写入指定内容,以追加方式 :return: """ wb = openpyxl.load_workbook(self.path) ws = wb[sheet_n

  • python读取excel数据绘制简单曲线图的完整步骤记录

    python读写excel文件有很多种方法: 用xlrd和xlwt进行excel读写 用openpyxl进行excel读写 用pandas进行excel读写 本文使用xlrd读取excel文件(xls,sxls格式),使用xlwt向excel写入数据 一.xlrd和xlwt的安装 安装很简单,windos+r调出运行窗口,输入cmd,进入命令行窗口,输入以下命令. 安装xlrd: pip install xlrd 安装xlwt: pip install xlwt xlrd的API(applica

  • python 利用openpyxl读取Excel表格中指定的行或列教程

    Worksheet 对象的 rows 属性和 columns 属性得到的是一 Generator 对象,不能用中括号取索引. 可先用列表推导式生成包含每一列中所有单元格的元组的列表,在对列表取索引. Worksheet 的 rows 属性亦可用相同的方法处理. 补充:python之表格数据读取 python 操作excel主要用到xlrd,xlwt这两个库,xlrd,是读取excel表,xlwt是写入表格 1.打开表格 table = xlrd.open("path_to_your_excel&

  • 解决python pandas读取excel中多个不同sheet表格存在的问题

    摘要:不同方法读取excel中的多个不同sheet表格性能比较 # 方法1 def read_excel(path): df=pd.read_excel(path,None) print(df.keys()) # for k,v in df.items(): # print(k) # print(v) # print(type(v)) return df # 方法2 def read_excel1(path): data_xls = pd.ExcelFile(path) print(data_x

  • Python读取pdf表格写入excel的方法

    背景 今天突然想到之前被要求做同性质银行的数据分析.妈耶!十几个银行,每个银行近5年的财务数据,而且财务报表一般都是 pdf 的,我们将 pdf 中表的数据一个个的拷贝到 excel 中,再借助 excel 去进行求和求平均等聚合函数操作,完事了还得把求出来的结果再统一 CV 到另一张表中,进行可视化分析- 当然,那时风流倜傥的 老Amy 还熟练的玩转着 excel ,也是个秀儿~ 今天就思索着,如果当年我会 Python 是不是可以让我成为班级最靓的崽!用技术占领高地,HHH,所以今天我来了,

  • python excel和yaml文件的读取封装

    excel import os import xlrd PATH = lambda p: os.path.abspath( os.path.join(os.path.dirname(__file__), p) ) class ExcelData: def __init__(self, file, sheet="sheet1", title=True): # 判断文件存在不存在 if os.path.isfile(PATH(file)): self.file = PATH(file) s

  • python3 循环读取excel文件并写入json操作

    文件内容: excel内容: 代码: import xlrd import json import operator def read_xlsx(filename): # 打开excel文件 data1 = xlrd.open_workbook(filename) # 读取第一个工作表 table = data1.sheets()[0] # 统计行数 n_rows = table.nrows data = [] # 微信文章属性:wechat_name wechat_id title abstr

  • Python读取Excel一列并计算所有对象出现次数的方法

    第一种方法 import pandas as pd from collections import Counter data = '参赛信息.xlsx' data = pd.read_excel('参赛信息.xlsx') # 导入参赛信息 x_pandas_list = data[u'专业1'] # 专业情况 list = list(x_pandas_list) c = Counter(list) print(c) 输出形式 Counter({'自动化学院': 164, '高分子科学与工程学院'

  • python在CMD界面读取excel所有数据的示例

    代码 import xlrd import os from prettytable import PrettyTable import pandas #创建一个Excel表类 class Excel(object): def __init__(self, path): self.path = path //路径要加上文件名 #读取Excel内全部数据 参数sname是sheet页名字 def read_all_data(self, sname): workbook = xlrd.open_wor

随机推荐