python使用xlsxwriter繪圖

lsq_008發表於2016-08-30
#coding:utf-8
import xlsxwriter


workbook = xlsxwriter.Workbook('chart_line.xlsx')
worksheet = workbook.add_worksheet()
bold = workbook.add_format({'bold': 1})


# Add the worksheet data to be plotted.
headings = ['time','value']
data = [['A', 'B', 'C','D'],[10, 20, 50,20]]
worksheet.write_row('A1',headings,bold)
worksheet.write_column('A2', data[0])
worksheet.write_column('B2', data[1])


#折線圖
chart1 = workbook.add_chart({'type': 'line'})


# Configure the first series.
chart1.add_series({
    'name':['Sheet1',0,1],
    'categories':['Sheet1',1,0,4,0],
    'values':['Sheet1',1,1,4,1],
    })
worksheet.insert_chart('D1',chart1,{'x_offset': -10 ,'y_offset': 0})


#柱狀圖
chart2 = workbook.add_chart({'type': 'column'})
chart2.add_series({
    'name':       '=Sheet1!$B$1',
    'categories': '=Sheet1!$A$2:$A$5',
    'values':     '=Sheet1!$B$2:$B$5',
})


worksheet.insert_chart('D2', chart2, {'x_offset': 500, 'y_offset': 0})


#餅狀圖
chart3 = workbook.add_chart({'type': 'pie'})
chart3.add_series({
    'name':       '=Sheet1!$B$1',
    'categories': '=Sheet1!$A$2:$A$5',
    'values':     '=Sheet1!$B$2:$B$5',
})


worksheet.insert_chart('D3', chart3, {'x_offset': -10, 'y_offset': 250})
workbook.close()

來自 “ ITPUB部落格 ” ,連結:http://blog.itpub.net/10972173/viewspace-2124186/,如需轉載,請註明出處,否則將追究法律責任。

相關文章