首页
/
每日頭條
/
職場
/
python讀取excel數據并繪制圖表
python讀取excel數據并繪制圖表
更新时间:2026-02-01 14:03:15
安裝XlsxWriter模塊:

pip install XlsxWriter

python讀取excel數據并繪制圖表(一日一技:Python使用XlsxWriter模塊在Excel工作表中繪制柱形圖)1

pip install XlsxWriter顯示已經安裝好,所以不用重複安裝

代碼1:繪制簡單的柱形圖。

要在Excel工作表上繪制簡單的柱形圖,請使用add_chart()方法和工作簿對象的“ column”關鍵字參數類型。

上代碼:

# import xlsxwriter module import xlsxwriter # Workbook() takes one, non-optional, argument # which is the filename that we want to create. workbook = xlsxwriter.Workbook('chart_column.xlsx') # The workbook object is then used to add new # worksheet via the add_worksheet() method. worksheet = workbook.add_worksheet() # Create a new Format object to formats cells # in worksheets using add_format() method . # here we create bold format object . bold = workbook.add_format({'bold': 1}) # create a data list . headings = ['Number', 'Batch 1', 'Batch 2'] data = [ [2, 3, 4, 5, 6, 7], [80, 80, 100, 60, 50, 100], [60, 50, 60, 20, 10, 20], ] # Write a row of data starting from 'A1' # with bold format . worksheet.write_row('A1', headings, bold) # Write a column of data starting from # 'A2', 'B2', 'C2' respectively . worksheet.write_column('A2', data[0]) worksheet.write_column('B2', data[1]) worksheet.write_column('C2', data[2]) # Create a chart object that can be added # to a worksheet using add_chart() method. # here we create a column chart object . chart1 = workbook.add_chart({'type': 'column'}) # Add a data series to a chart # using add_series method. # Configure the first series. # = Sheet1 !$A$1 is equivalent to ['Sheet1', 0, 0]. # note : spaces is not inserted in b / w # = and Sheet1, Sheet1 and ! # if space is inserted it throws warning. chart1.add_series({ 'name': '= Sheet1 !$B$1', 'categories': '= Sheet1 !$A$2:$A$7', 'values': '= Sheet1 !$B$2:$B$7', }) # Configure a second series. # Note use of alternative syntax to define ranges. # [sheetname, first_row, first_col, last_row, last_col]. chart1.add_series({ 'name': ['Sheet1', 0, 2], 'categories': ['Sheet1', 1, 0, 6, 0], 'values': ['Sheet1', 1, 2, 6, 2], }) # Add a chart title chart1.set_title ({'name': 'Results of data analysis'}) # Add x-axis label chart1.set_x_axis({'name': 'Test number'}) # Add y-axis label chart1.set_y_axis({'name': 'Data length (mm)'}) # Set an Excel chart style. chart1.set_style(11) # add chart to the worksheet # the top-left corner of a chart # is anchored to cell E2 . worksheet.insert_chart('E2', chart1) # Finally, close the Excel file # via the close() method. workbook.close()


輸出為:

python讀取excel數據并繪制圖表(一日一技:Python使用XlsxWriter模塊在Excel工作表中繪制柱形圖)2


代碼2:繪制堆積柱形圖.

要在Excel工作表上繪制堆積柱形圖,請使用add_chart()方法,其類型為工作簿對象的“ column”和子類型“ stacked”關鍵字參數。

上代碼演示:

# import xlsxwriter module import xlsxwriter # Workbook() takes one, non-optional, argument # which is the filename that we want to create. workbook = xlsxwriter.Workbook('chart_column2.xlsx') # The workbook object is then used to add new # worksheet via the add_worksheet() method. worksheet = workbook.add_worksheet() # Create a new Format object to formats cells # in worksheets using add_format() method . # here we create bold format object . bold = workbook.add_format({'bold': 1}) # create a data list . headings = ['Number', 'Batch 1', 'Batch 2'] data = [ [2, 3, 4, 5, 6, 7], [80, 80, 100, 60, 50, 100], [60, 50, 60, 20, 10, 20], ] # Write a row of data starting from 'A1' # with bold format . worksheet.write_row('A1', headings, bold) # Write a column of data starting from # 'A2', 'B2', 'C2' respectively . worksheet.write_column('A2', data[0]) worksheet.write_column('B2', data[1]) worksheet.write_column('C2', data[2]) # Create a chart object that can be added # to a worksheet using add_chart() method. # here we create a stacked Column chart object . chart1 = workbook.add_chart({'type': 'column', 'subtype': 'stacked'}) # Add a data series to a chart # using add_series method. # Configure the first series. # = Sheet1 !$A$1 is equivalent to ['Sheet1', 0, 0]. chart1.add_series({ 'name': '= Sheet1 !$B$1', 'categories': '= Sheet1 !$A$2:$A$7', 'values': '= Sheet1 !$B$2:$B$7', }) # Configure a second series. # Note use of alternative syntax to define ranges. # [sheetname, first_row, first_col, last_row, last_col]. chart1.add_series({ 'name': ['Sheet1', 0, 2], 'categories': ['Sheet1', 1, 0, 6, 0], 'values': ['Sheet1', 1, 2, 6, 2], }) # Add a chart title chart1.set_title ({'name': 'Results of data analysis'}) # Add x-axis label chart1.set_x_axis({'name': 'Test number'}) # Add y-axis label chart1.set_y_axis({'name': 'Data length (mm)'}) # Set an Excel chart style. chart1.set_style(11) # add chart to the worksheet # the top-left corner of a chart # is anchored to cell E2 . worksheet.insert_chart('E2', chart1) # Finally, close the Excel file # via the close() method. workbook.close()

輸出結果:

python讀取excel數據并繪制圖表(一日一技:Python使用XlsxWriter模塊在Excel工作表中繪制柱形圖)3

代碼3:繪制堆積柱形圖百分比.

若要在Excel工作表上繪制“堆積百分比”圖表,請使用add_chart()方法,其類型為工作簿對象的“列”類型和子類型“ percent_stacked”關鍵字參數。

# import xlsxwriter module import xlsxwriter # Workbook() takes one, non-optional, argument # which is the filename that we want to create. workbook = xlsxwriter.Workbook('chart_column3.xlsx') # The workbook object is then used to add new # worksheet via the add_worksheet() method. worksheet = workbook.add_worksheet() # Create a new Format object to formats cells # in worksheets using add_format() method . # here we create bold format object . bold = workbook.add_format({'bold': 1}) # create a data list . headings = ['Number', 'Batch 1', 'Batch 2'] data = [ [2, 3, 4, 5, 6, 7], [80, 80, 100, 60, 50, 100], [60, 50, 60, 20, 10, 20], ] # Write a row of data starting from 'A1' # with bold format . worksheet.write_row('A1', headings, bold) # Write a column of data starting from # 'A2', 'B2', 'C2' respectively . worksheet.write_column('A2', data[0]) worksheet.write_column('B2', data[1]) worksheet.write_column('C2', data[2]) # Create a chart object that can be added # to a worksheet using add_chart() method. # here we create a percent stacked Column chart object . chart1 = workbook.add_chart({'type': 'column', 'subtype': 'percent_stacked'}) # Add a data series to a chart # using add_series method. # Configure the first series. # = Sheet1 !$A$1 is equivalent to ['Sheet1', 0, 0]. chart1.add_series({ 'name': '= Sheet1 !$B$1', 'categories': '= Sheet1 !$A$2:$A$7', 'values': '= Sheet1 !$B$2:$B$7', }) # Configure a second series. # Note use of alternative syntax to define ranges. # [sheetname, first_row, first_col, last_row, last_col]. chart1.add_series({ 'name': ['Sheet1', 0, 2], 'categories': ['Sheet1', 1, 0, 6, 0], 'values': ['Sheet1', 1, 2, 6, 2], }) # Add a chart title chart1.set_title ({'name': 'Results of data analysis'}) # Add x-axis label chart1.set_x_axis({'name': 'Test number'}) # Add y-axis label chart1.set_y_axis({'name': 'Data length (mm)'}) # Set an Excel chart style. chart1.set_style(11) # add chart to the worksheet # the top-left corner of a chart # is anchored to cell E2 . worksheet.insert_chart('E2', chart1) # Finally, close the Excel file # via the close() method. workbook.close()

輸出結果為:

python讀取excel數據并繪制圖表(一日一技:Python使用XlsxWriter模塊在Excel工作表中繪制柱形圖)4

祝學習愉快!

,
Comments
Welcome to tft每日頭條 comments! Please keep conversations courteous and on-topic. To fosterproductive and respectful conversations, you may see comments from our Community Managers.
Sign up to post
Sort by
Show More Comments
推荐阅读
九型人格3号的優點和缺點(九型人格3号職業規劃)
九型人格3号的優點和缺點(九型人格3号職業規劃)
  今日目錄:3号職業規劃、性格解析完全指南   3号解析,順境與逆境時表現   3号性格與國家   精神通道   3号的關注點   性格傾向   分支性格關注點:   焦點問題:8個   敵意與自戀的問題   性格形成原因:父母取向   與3号打交道的技巧   戀愛中的3号   3号發出的信号:積極、消極、混合、内在   職場中的3号:工作中;領導;員工;...
2026-02-01
大鵬的團隊需要的是哪些人才(大鵬即将被解雇)
大鵬的團隊需要的是哪些人才(大鵬即将被解雇)
     《大赢家》劇情剛開始的五分鐘,大鵬飾演的嚴謹。   長相平凡,戴着黑框眼鏡。一臉嚴肅,為人木讷,似乎在和領導為了什麼而辯論。   嚴謹的意思:形容态度嚴肅謹慎,細緻、周全、完善,追求完美。   以“嚴”為姓氏,故有以“嚴謹”為名的人。   果然是人如其名,眼裡容不得沙子。   整個人在工作上就是呆闆,不知變通的人。   我們知道,如果現在的職場中真...
2026-02-01
關于公務員面試的技巧與方法(公務員面試不要露怯)
關于公務員面試的技巧與方法(公務員面試不要露怯)
  公務員,這個鐵飯碗,有多少人想進入公務員隊伍,而且自從《新公務員法》之後,公務員的績效考核,待遇,補休等一系列的福利待遇更加完善。特别是去年經濟形勢不好,公務員更是成為金飯碗。      但凡行測,申論好的,過了筆試不成問題,但是面試卻沒有這麼簡單,面試考的是一個人的表達能力,心理素質,已經處理突發事情的能力等。筆試過了隻是一腳踏入了公務員的隊伍,但是還...
2026-02-01
禁毒示範學校鞏固材料(毒品預防教育示範校)
禁毒示範學校鞏固材料(毒品預防教育示範校)
     健康人生 綠色無毒   ——XX學校創建“毒品預防教育示範校”工作彙報   自從上級部門倡導開展“毒品預防教育工作”以來,我校始終以學生發展為根本,紮實開展毒品預防教育,做到全校學生接受禁毒知識教育面達100%,開展禁毒宣傳教育面達100%。   我們一直都把毒品預防教育作為學校安全工作、德育和法制教育工作的一項重要内容來抓,圍繞“遠離毒品珍愛生命...
2026-02-01
園區孵化器招商崗位能做嗎(如何做好園區招商工作)
園區孵化器招商崗位能做嗎(如何做好園區招商工作)
     園區招商就是一場企業資源的争奪戰,決定這場戰争勝負的因素很多,其中,招商人員在招商引資工作中起着關鍵的作用,其能力和素質的高低對于園區招商成功與否至關重要。那麼,園區招商人員要如何做好園區招商工作呢?   1、具有敏銳的信息捕捉嗅覺   招商的第一步,就是獲取招商信息,所以,捕捉項目信息至關重要,其次,還要學會對獲取來的信息進行分類和篩選,做到憑感...
2026-02-01
Copyright 2023-2026 - www.tftnews.com All Rights Reserved