python寫入excel(xlswriter)--組建圖表

來源:互聯網
上載者:User

標籤:div   title   har   pytho   配置   ber   axis   chart   資料   

一、折線圖:

# -*- coding:utf-8 -*-import xlsxwriter# 建立一個excelworkbook = xlsxwriter.Workbook("chart_line.xlsx")# 建立一個sheetworksheet = workbook.add_worksheet()# worksheet = workbook.add_worksheet("bug_analysis")# 自訂樣式,加粗bold = workbook.add_format({‘bold‘: 1})# --------1、準備資料並寫入excel---------------# 向excel中寫入資料,建立表徵圖時要用到headings = [‘Number‘, ‘testA‘, ‘testB‘]data = [    [‘2017-9-1‘, ‘2017-9-2‘, ‘2017-9-3‘, ‘2017-9-4‘, ‘2017-9-5‘, ‘2017-9-6‘],    [10, 40, 50, 20, 10, 50],    [30, 60, 70, 50, 40, 30],]# 寫入表頭worksheet.write_row(‘A1‘, headings, bold)# 寫入資料worksheet.write_column(‘A2‘, data[0])worksheet.write_column(‘B2‘, data[1])worksheet.write_column(‘C2‘, data[2])# --------2、組建圖表並插入到excel---------------# 建立一個柱狀圖(line chart)chart_col = workbook.add_chart({‘type‘: ‘line‘})# 配置第一個系列資料chart_col.add_series({    # 這裡的sheet1是預設的值,因為我們在建立sheet時沒有指定sheet名    # 如果我們建立sheet時設定了sheet名,這裡就要設定成相應的值    ‘name‘: ‘=Sheet1!$B$1‘,    ‘categories‘: ‘=Sheet1!$A$2:$A$7‘,    ‘values‘:   ‘=Sheet1!$B$2:$B$7‘,    ‘line‘: {‘color‘: ‘red‘},})# 配置第二個系列資料chart_col.add_series({    ‘name‘: ‘=Sheet1!$C$1‘,    ‘categories‘:  ‘=Sheet1!$A$2:$A$7‘,    ‘values‘:   ‘=Sheet1!$C$2:$C$7‘,    ‘line‘: {‘color‘: ‘yellow‘},})# 配置第二個系列資料(用了另一種文法)# chart_col.add_series({#     ‘name‘: [‘Sheet1‘, 0, 2],#     ‘categories‘: [‘Sheet1‘, 1, 0, 6, 0],#     ‘values‘: [‘Sheet1‘, 1, 2, 6, 2],#     ‘line‘: {‘color‘: ‘yellow‘},# })# 設定圖表的title 和 x,y軸資訊chart_col.set_title({‘name‘: ‘The xxx site Bug Analysis‘})chart_col.set_x_axis({‘name‘: ‘Test number‘})chart_col.set_y_axis({‘name‘:  ‘Sample length (mm)‘})# 設定圖表的風格chart_col.set_style(1)# 把圖表插入到worksheet並設定位移worksheet.insert_chart(‘A10‘, chart_col, {‘x_offset‘: 25, ‘y_offset‘: 10})workbook.close() 

 

二、柱狀圖:

# -*- coding:utf-8 -*-import xlsxwriter# 建立一個excelworkbook = xlsxwriter.Workbook("chart_column.xlsx")# 建立一個sheetworksheet = workbook.add_worksheet()# worksheet = workbook.add_worksheet("bug_analysis")# 自訂樣式,加粗bold = workbook.add_format({‘bold‘: 1})# --------1、準備資料並寫入excel---------------# 向excel中寫入資料,建立表徵圖時要用到headings = [‘Number‘, ‘testA‘, ‘testB‘]data = [    [‘2017-9-1‘, ‘2017-9-2‘, ‘2017-9-3‘, ‘2017-9-4‘, ‘2017-9-5‘, ‘2017-9-6‘],    [10, 40, 50, 20, 10, 50],    [30, 60, 70, 50, 40, 30],]# 寫入表頭worksheet.write_row(‘A1‘, headings, bold)# 寫入資料worksheet.write_column(‘A2‘, data[0])worksheet.write_column(‘B2‘, data[1])worksheet.write_column(‘C2‘, data[2])# --------2、組建圖表並插入到excel---------------# 建立一個柱狀圖(column chart)chart_col = workbook.add_chart({‘type‘: ‘column‘})# 配置第一個系列資料chart_col.add_series({    # 這裡的sheet1是預設的值,因為我們在建立sheet時沒有指定sheet名    # 如果我們建立sheet時設定了sheet名,這裡就要設定成相應的值    ‘name‘: ‘=Sheet1!$B$1‘,    ‘categories‘: ‘=Sheet1!$A$2:$A$7‘,    ‘values‘:   ‘=Sheet1!$B$2:$B$7‘,    ‘line‘: {‘color‘: ‘red‘},})# 配置第二個系列資料(用了另一種文法)chart_col.add_series({    ‘name‘: ‘=Sheet1!$C$1‘,    ‘categories‘:  ‘=Sheet1!$A$2:$A$7‘,    ‘values‘:   ‘=Sheet1!$C$2:$C$7‘,    ‘line‘: {‘color‘: ‘yellow‘},})# 配置第二個系列資料(用了另一種文法)# chart_col.add_series({#     ‘name‘: [‘Sheet1‘, 0, 2],#     ‘categories‘: [‘Sheet1‘, 1, 0, 6, 0],#     ‘values‘: [‘Sheet1‘, 1, 2, 6, 2],#     ‘line‘: {‘color‘: ‘yellow‘},# })# 設定圖表的title 和 x,y軸資訊chart_col.set_title({‘name‘: ‘The xxx site Bug Analysis‘})chart_col.set_x_axis({‘name‘: ‘Test number‘})chart_col.set_y_axis({‘name‘:  ‘Sample length (mm)‘})# 設定圖表的風格chart_col.set_style(1)# 把圖表插入到worksheet以及位移worksheet.insert_chart(‘A10‘, chart_col, {‘x_offset‘: 25, ‘y_offset‘: 10})workbook.close()

PS:

其實前面兩個圖只變動一點:把 line 個性為 column

chart_col = workbook.add_chart({‘type‘: ‘column‘})

 

三、餅圖:

# -*- coding:utf-8 -*-import xlsxwriter# 建立一個excelworkbook = xlsxwriter.Workbook("chart_pie.xlsx")# 建立一個sheetworksheet = workbook.add_worksheet()# 自訂樣式,加粗bold = workbook.add_format({‘bold‘: 1})# --------1、準備資料並寫入excel---------------# 向excel中寫入資料,建立表徵圖時要用到data = [    [‘closed‘, ‘active‘, ‘reopen‘, ‘NT‘],    [1012, 109, 123, 131],]# 寫入資料worksheet.write_row(‘A1‘, data[0], bold)worksheet.write_row(‘A2‘, data[1])# --------2、組建圖表並插入到excel---------------# 建立一個柱狀圖(pie chart)chart_col = workbook.add_chart({‘type‘: ‘pie‘})# 配置第一個系列資料chart_col.add_series({    ‘name‘: ‘Bug Analysis‘,    ‘categories‘: ‘=Sheet1!$A$1:$D$1‘,    ‘values‘: ‘=Sheet1!$A$2:$D$2‘,    ‘points‘: [        {‘fill‘: {‘color‘: ‘#00CD00‘}},        {‘fill‘: {‘color‘: ‘red‘}},        {‘fill‘: {‘color‘: ‘yellow‘}},        {‘fill‘: {‘color‘: ‘gray‘}},    ],})# 設定圖表的title 和 x,y軸資訊chart_col.set_title({‘name‘: ‘Bug Analysis‘})# 設定圖表的風格chart_col.set_style(10)# 把圖表插入到worksheet以及位移worksheet.insert_chart(‘B10‘, chart_col, {‘x_offset‘: 25, ‘y_offset‘: 10})workbook.close()

 

 

 

 

 

參考資料:

http://xlsxwriter.readthedocs.io/chart_examples.html

http://xlsxwriter.readthedocs.io/chart.html

python寫入excel(xlswriter)--組建圖表

相關文章

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.