python读取excel数据并且画图的实现示例
程序员文章站
2022-04-11 11:36:19
一,要读取的数据的格式:二,数据读取部分:b站视频参考:https://www.bilibili.com/video/bv14c4y1w7nj?t=148# 1930workbook=xlrd...
一,要读取的数据的格式:
二,数据读取部分:
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数据并且画图的资料请关注其它相关文章!