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

二,数据读取部分:

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

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

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读excel中数据画图_python读取excel数据并且画图的实现示例相关推荐

  1. 如何用python处理excel中时间重合_python读取Excel文件中的时间,并计算时间间隔...

    以此Excel文件为例: 1.python读取Excel文件中的时间 import xlrd #读取Excel文件 workbook = xlrd.open_workbook('C:/Users/12 ...

  2. python 获取csv的列数_Python读取excel的两种方法

    excel是什么 Microsoft Excel是一个使用非常广泛的电子表格程序.它的用户友好性和吸引人的功能使其成为数据科学中常用的工具.对于excel来说,整个excel文件称为工作簿,工作簿中的 ...

  3. python取excel单元格数值_python读取excel文件单元格中的数据返回类型

    python读取excel表格内不同类型的数据 不同类型数据对应值: #coding=utf-8 import os import xlrd from datetime import datetime ...

  4. python excel画图_python读取excel数据并且画图

    一,要读取的数据的格式: 二,数据读取部分: # 1930 workbook=xlrd.open_workbook('1930.xlsx') sheet= workbook.sheet_by_inde ...

  5. python 读excel中的sheet_python实现读取excel文件中所有sheet操作示例

    本文实例讲述了python实现读取excel文件中所有sheet操作.分享给大家供大家参考,具体如下: 表格是这样的 实现把此文件所有sheet中 标识为1 的行,取出来,存入一个字典.所有行组成一个 ...

  6. python excel写入一列_python读取excel指定列数据并写入到新的excel方法

    如下所示: #encoding=utf-8 import xlrd from xlwt import * #------------------读数据------------------------- ...

  7. echart 多柱图只显示部分数据标签_python|读取Excel生成pyecharts动态分析图

    导入数据 #导入所需的库文件import numpy as npimport pandas as pdfrom pyecharts import Bar#导入excel数据data=pd.DataFr ...

  8. python获取指定单元格内容_python读取excel表格指定位置的内容

    今天是第一次写博客,对之前学以致用的内容做一些总结,以备日后忘了或者可以给别人提供一些帮助.话不多说,开始写内容. python读取excel表格指定位置的内容 需求:现在有一个excel表格,里面有 ...

  9. python读单行文本求平均值_Python读取文本文件中的数字来计算平均值

    我有一个有很多数字的文件,我想用python把它读入一个列表列表中.然后计算它的平均值或总和或其他什么.在 这是我的文本文件:1,2,5349,2398,19,3450 10,95,201,5943, ...

最新文章

  1. 垃圾回收器的基本原理是什么?垃圾回收器可以马上回收内存吗?有什么办法主动通知虚拟机进行垃圾回收?...
  2. 私钥设置_私钥忘了怎么办,还能找回来吗?能
  3. c++成员函数的重载、覆盖、隐藏区别
  4. 【Ubuntu】安装中文输入法、终端不支持中文的解决方法
  5. hexo 搜索_Hexo--本地搜索localsearch之url路径问题
  6. maven2创建或者导入eclipse工程 设置M2_REPO消除错误
  7. 动态规划——Poj 1159 Palindrome
  8. halcon算子盘点:Chapter 18:元组Tuple
  9. 用小括号表达一个长字符串
  10. 光伏发电项目将全面摸底复核
  11. 您的API是什么情况?
  12. java http 下载文件_JAVA通过HttpURLConnection 上传和下载文件的方法
  13. ylb:SQL 视图(View)基础
  14. Mysql对数据库操作的简单命令
  15. 中国南方航空新开广州直飞宿务航线
  16. 用C++写一个简单的表白小程序
  17. 计算机网络安全文明知识竞赛,网络安全知识竞赛
  18. 3.Open3D教程——点云数据操作
  19. Linux(centos7) 安装配置gitlab-runner
  20. 学会享受努力奋斗的过程

热门文章

  1. Mybatis Plus ${ew.sqlSegment} 与 ${ew.customSqlSegment}的使用区别
  2. Shade4PointLights
  3. verilog练习:hdlbits网站上的做题笔记(5)
  4. nas 微型计算机,NETGEAR无线路由器和NAS试用
  5. POJ前面的题目算法思路【转】
  6. Lua 最全的快速入门教程
  7. 英语单词记忆 词源法-思维导图(09)词源bar/bal/ban-41
  8. Python爬虫第十课:Scrapy框架(1)
  9. yogabook安卓版root方法
  10. 云服务器搭建开发环境