首页
/
每日頭條
/
職場
/
python讀取excel數據并繪制圖表
python讀取excel數據并繪制圖表
更新时间:2024-12-22 13:19: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
推荐阅读
私湯溫泉價格(大學生和女白領都喜歡來這家溫泉店)
私湯溫泉價格(大學生和女白領都喜歡來這家溫泉店)
  有一家溫泉店不光女白領和大學生愛來,還有議員、政治家、貴婦人、女明星等諸多上流社會人士客戶層。不光如此,這家溫泉店還制霸了所有的旅館雜志。   這家旅館長這樣:         才怪!其實長這樣↓      除了有溫泉和卧室等溫泉旅館基礎設施之外還有餐廳、自動售貨機、乒乓球室、老虎機、甚至還有占蔔室等諸多設施。而且每間卧室附近都有風景,春天的櫻花,夏天的...
2024-12-22
時尚又減齡的圍巾系法一定要學會(時髦有型的8種圍巾系法)
時尚又減齡的圍巾系法一定要學會(時髦有型的8種圍巾系法)
  尤其是像時尚博主Leonie Hanne這種粗棒針的針織款,堆砌會讓人顯得很臃腫,而随意的披挂反而成為搭配增添了亮點。      時尚博主Leonie Hanne   時尚博主Charlotte Bridgeman、和時尚博主Marina the Moss這種亮色的流蘇圍巾也很适合冬天,輕輕松松就能從滿大街的黑白灰中跳脫出來!      時尚博主Char...
2024-12-22
最溫馴的貓貓(比老闆更像老闆)
最溫馴的貓貓(比老闆更像老闆)
  試想一下,你在午後走進一間普普通通的雜貨店,首先映入眼簾的是一隻慵懶、可愛的貓咪,心情是不是會變得很好?Twitter上有一個專門收集「雜貨店店貓」照片的帳号「@Bodega Cats」,萌萌的貓咪們有時坐在貨架上,有時趴在門口,吸引着貓奴來光顧~      ▼「請自行選購商品,本喵先休息一下。」      ▼吃多了水果也會發胖嗎?      ▼「歡迎光...
2024-12-22
dnf瞎子下個版本加強嗎(17個職業平衡性調整)
dnf瞎子下個版本加強嗎(17個職業平衡性調整)
  今日,DNF進行了版本大更新,其中就是大家期待已久的職業平衡性調整,本次的版本更新一共涉及到17個職業,下面台長就來和大家一一盤點,看看對你的職業是否有影響:   1、阿修羅  鬼印珠、無雙波、邪光波動陣、暗天波動眼、雷神之息、天雷 · 波動劍 變更為獨立攻擊。(這樣的變化等于是由百分比職業變更為固傷職業了,有望告别下水道)   心眼 技能增加5%的命中...
2024-12-22
dnf60版本各個副本前置(DNF60版本異界回憶啟示錄)
dnf60版本各個副本前置(DNF60版本異界回憶啟示錄)
  說起DNF是大家已經在熟悉不過的網絡遊戲了,是由韓國NEOPLE開發的,于2008年在國服正式登陸,是一款免費角色扮演的2D闖關遊戲,距離至今已十餘年之久,而我們也與DNF風雨同舟十餘年,現在回憶起已是慢慢的回憶,甚至有時候還熱淚盈眶,懷念當時一起組隊刷圖升級,懷念當年一起組隊刷深淵,還念一起去PK場PK的成年往事;說道回憶各位小夥伴們還記的之前最開始的...
2024-12-22
Copyright 2023-2024 - www.tftnews.com All Rights Reserved