介绍二、数据读取部分:
三、画图函数
1。def挂钩(Music_genre singer_num,年)
这篇文章主要介绍python如何读取excel数据并且画的图,文中介绍的非常详细,具有一定的参考价值,感兴趣的小伙伴们一定要看完!
一、要读取的数据的格式:
二、数据读取部分:
b站视频参考:https://www.bilibili.com/video/BV14C4y1W7Nj?t=148
#, 1930 工作簿=xlrd.open_workbook (& # 39; 1930. xlsx& # 39;) 表=,workbook.sheet_by_index (0) A1=[] B1=[] 0 #,sheet.cell_value(我):第行我的第0个元素 for 小姐:拷贝范围(1,sheet.nrows): ,A1.append (sheet.cell_value(我,0)) ,B1.append (sheet.cell_value(我,1)) , if len (A1) !=len (B1): ,打印(“False") 拉杆(A1, B1, 1930)
三、画图函数
1。def挂钩(Music_genre singer_num,年)
参数介绍
参数名参数含义Music_genre音乐流派名称listsinger_num音乐流派对应音乐家数量listyear读的文件的年份(因为源代码是从1840到2020的)def 牵引(Music_genre, singer_num,年): ,arr_len=len (Music_genre) ,#由循环得到一个字典,关键是音乐流派,价值是这个音乐流派对应的音乐家的数量 i=0 ,dict_music_singer={} ,while i< arr_len: ,dict_music_singer [Music_genre[我]]=singer_num[我] 我=+ 1 , #,注释1 ,pyplot.bar (x=0,底部=范围(arr_len),身高=0.3,=singer_num宽度,取向=癶orizontal") ,#注释2 ,pyplot.yticks(范围(arr_len) Music_genre) ,#加标题,展示图像 ,pyplot.title(年) ,pyplot.show () , ,…… ,…… ,挂钩(A1, B1, 1930)
注释1:
“““ ,水平条形图,需要修改以下属性 ,定位=癶orizontal" “““ import numpy  as np import matplotlib.pyplot  as plt , #,数据 时间=N 5 x =,(20日,10日,30日,25日,15) 时间=y [0、1、2、3、4) , #,绘图,x=,起始位置,,=,底水平条的底部(左侧),,y轴,height 水平条的宽度,,width 水平条的长度 p1 =, plt.bar (x=0,,=y,,身高=0.5,=x,宽度,取向=癶orizontal") pyplot.bar(范围(arr_len)、singer_num对齐=& # 39;中心# 39;) pyplot.bar (x=0,,底部=范围(arr_len),身高=0.5,=singer_num宽度,取向=癶orizontal") #,展示图形 plt.show ()
注释2:plt.xticks的第一个参数和plt。阴谋的第一个参数一样,第二个参数是和第一个参数相同长度的列表此例中用来代替横坐标
import matplotlib.pyplot as plt 时间=x [1,, 2,, 3,, 4] y =,(1, 4, 9日,6] labels =,(& # 39;青蛙# 39;,,& # 39;猪# 39;,,& # 39;沼泽# 39;,,& # 39;跋涉# 39;】 , plt.plot (x, y) #,你还要specify a rotation for 从而tick labels 拷贝degrees 或是with 关键词。 plt.xticks (x,,标签,旋转=& # 39;垂直# 39;) #,Pad margins  so that markers 不要# 39;t get clipped by 从而轴 plt.margins (0.2) #,Tweak spacing 用prevent clipping of tick-labels plt.subplots_adjust(底部=0.15) plt.show ()
1.1效果:
1.2完整代码
import pandas as pd import numpy  as np import xlrd 得到matplotlib import  pyplot def 牵引(Music_genre, singer_num,年): ,arr_len=len (Music_genre) , i=0 ,dict_music_singer={} ,while i< arr_len: ,dict_music_singer [Music_genre[我]]=singer_num[我] 我=+ 1 ,# pyplot.bar(范围(arr_len) singer_num,对齐=& # 39;中心# 39;) ,pyplot.bar (x=0,底部=范围(arr_len),身高=0.3,=singer_num宽度,取向=癶orizontal") ,pyplot.yticks(范围(arr_len) Music_genre) ,pyplot.title(年) ,pyplot.show () , , #,1930 工作簿=xlrd.open_workbook (& # 39; 1930. xlsx& # 39;) 表=,workbook.sheet_by_index (0) A1=[] B1=[] for 小姐:拷贝范围(1,sheet.nrows): ,A1.append (sheet.cell_value(我,0)) ,B1.append (sheet.cell_value(我,1)) , if len (A1) !=len (B1): ,打印(“False") 拉杆(A1, B1, 1930) , , , #,1940 工作簿=xlrd.open_workbook (& # 39; 1940. xlsx& # 39;) 表=,workbook.sheet_by_index (0) A2=[] B2=[] for 小姐:拷贝范围(1,sheet.nrows): ,A2.append (sheet.cell_value(我,0)) ,B2.append (sheet.cell_value(我,1)) , if len (A2) !=len (B2): ,打印(“False") 拉杆(A2, B2, 1940) , , , #, 工作簿=xlrd.open_workbook (& # 39; 1950. xlsx& # 39;) 表=,workbook.sheet_by_index (0) A3=[] B3=[] for 小姐:拷贝范围(1,sheet.nrows): ,A3.append (sheet.cell_value(我,0)) ,B3.append (sheet.cell_value(我,1)) , if len (A3) !=len (B3): null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null null nullpython如何读取excel数据并且画的图