參數(shù)名 | 參數(shù)含義 |
---|---|
Music_genre | 音樂(lè)流派名稱list |
singer_num | 音樂(lè)流派對(duì)應(yīng)音樂(lè)家數(shù)量list |
year | 讀的文件的年份(因?yàn)樵创a是從1840到2020的) |
def drawBar(Music_genre,singer_num,year): arr_len=len(Music_genre) # 由循環(huán)得到一個(gè)字典,key是音樂(lè)流派,value是這個(gè)音樂(lè)流派對(duì)應(yīng)的音樂(lè)家的數(shù)量 i=0 dict_music_singer={} while iarr_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 # 數(shù)據(jù) N = 5 x = [20, 10, 30, 25, 15] y = [0,1,2,3,4] # 繪圖 x= 起始位置, bottom= 水平條的底部(左側(cè)), y軸, height 水平條的寬度, width 水平條的長(zhǎng)度 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的第一個(gè)參數(shù)和plt.plot的第一個(gè)參數(shù)一樣,第二個(gè)參數(shù)是和第一個(gè)參數(shù)相同長(zhǎng)度的list此例中用來(lái)代替橫坐標(biāo)
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()
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 iarr_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數(shù)據(jù)并且畫(huà)圖的實(shí)現(xiàn)示例的詳細(xì)內(nèi)容,更多關(guān)于python讀取excel數(shù)據(jù)并且畫(huà)圖的資料請(qǐng)關(guān)注腳本之家其它相關(guān)文章!
標(biāo)簽:郴州 海南 烏蘭察布 平頂山 大慶 烏蘭察布 合肥 哈爾濱
巨人網(wǎng)絡(luò)通訊聲明:本文標(biāo)題《python讀取excel數(shù)據(jù)并且畫(huà)圖的實(shí)現(xiàn)示例》,本文關(guān)鍵詞 python,讀取,excel,數(shù)據(jù),并且,;如發(fā)現(xiàn)本文內(nèi)容存在版權(quán)問(wèn)題,煩請(qǐng)?zhí)峁┫嚓P(guān)信息告之我們,我們將及時(shí)溝通與處理。本站內(nèi)容系統(tǒng)采集于網(wǎng)絡(luò),涉及言論、版權(quán)與本站無(wú)關(guān)。