Python | Plotting Pie charts in excel sheet using XlsxWriter module (original) (raw)

Last Updated : 22 Oct, 2018

Prerequisite: Create and Write on an excel sheet

**XlsxWriter **is a Python library using which one can perform multiple operations on excel files like creating, writing, arithmetic operations and plotting graphs. Let’s see how to plot different types of pie charts using realtime data.

Charts are composed of at least one series of one or more data points. Series themselves are comprised of references to cell ranges.

For plotting the charts on an excel sheet, firstly, create chart object of specific chart type( i.e Pie chart etc.). After creating chart objects, insert data in it and lastly, add that chart object in the sheet object.

Code #1 : Plot the simple Pie Chart.

For plotting the simple Pie chart on an excel sheet, use add_chart() method with type ‘pie’ keyword argument of a workbook object.

import xlsxwriter

workbook = xlsxwriter.Workbook( 'chart_pie.xlsx' )

worksheet = workbook.add_worksheet()

bold = workbook.add_format({ 'bold' : 1 })

headings = [ 'Category' , 'Values' ]

data = [

`` [ 'Apple' , 'Cherry' , 'Pecan' ],

`` [ 60 , 30 , 10 ],

]

worksheet.write_row( 'A1' , headings, bold)

worksheet.write_column( 'A2' , data[ 0 ])

worksheet.write_column( 'B2' , data[ 1 ])

chart1 = workbook.add_chart({ 'type' : 'pie' })

chart1.add_series({

`` 'name' : 'Pie sales data' ,

`` 'categories' : [ 'Sheet1' , 1 , 0 , 3 , 0 ],

`` 'values' : [ 'Sheet1' , 1 , 1 , 3 , 1 ],

})

chart1.set_title({ 'name' : 'Popular Pie Types' })

chart1.set_style( 10 )

worksheet.insert_chart( 'C2' , chart1, { 'x_offset' : 25 , 'y_offset' : 10 })

workbook.close()

Output :
Output 1

Code #2: Plot a Pie chart with user defined segment colours.

For plotting the pie chart with user defined segment colours on an excel sheet, use add_series() method with points keyword argument of a chart object.

import xlsxwriter

workbook = xlsxwriter.Workbook( 'chart_pie_colour.xlsx' )

worksheet = workbook.add_worksheet()

bold = workbook.add_format({ 'bold' : 1 })

headings = [ 'Category' , 'Values' ]

data = [

`` [ 'Apple' , 'Cherry' , 'Pecan' ],

`` [ 60 , 30 , 10 ],

]

worksheet.write_row( 'A1' , headings, bold)

worksheet.write_column( 'A2' , data[ 0 ])

worksheet.write_column( 'B2' , data[ 1 ])

chart2 = workbook.add_chart({ 'type' : 'pie' })

chart2.add_series({

`` 'name' : 'Pie sales data' ,

`` 'categories' : '= Sheet1 !$A$2:$A$4' ,

`` 'values' : '= Sheet1 !$B$2:$B$4' ,

`` 'points' : [

`` { 'fill' : { 'color' : '# 5ABA10' }},

`` { 'fill' : { 'color' : '# FE110E' }},

`` { 'fill' : { 'color' : '# CA5C05' }},

`` ],

})

chart2.set_title({ 'name' : 'Pie Chart with user defined colors' })

worksheet.insert_chart( 'C2' , chart2, { 'x_offset' : 25 , 'y_offset' : 10 })

workbook.close()

Output :
Output 2

Code #3: Plot a Pie chart with rotation of the segments.

For plotting a pie chart with rotation of the segments on an excel sheet, use set_rotation() method with definite angle argument of the chart object.

import xlsxwriter

workbook = xlsxwriter.Workbook( 'chart_pie_rotation.xlsx' )

worksheet = workbook.add_worksheet()

bold = workbook.add_format({ 'bold' : 1 })

headings = [ 'Category' , 'Values' ]

data = [

`` [ 'Apple' , 'Cherry' , 'Pecan' ],

`` [ 60 , 30 , 10 ],

]

worksheet.write_row( 'A1' , headings, bold)

worksheet.write_column( 'A2' , data[ 0 ])

worksheet.write_column( 'B2' , data[ 1 ])

chart3 = workbook.add_chart({ 'type' : 'pie' })

chart3.add_series({

`` 'name' : 'Pie sales data' ,

`` 'categories' : '= Sheet1 !$A$2:$A$4' ,

`` 'values' : '= Sheet1 !$B$2:$B$4' ,

})

chart3.set_title({ 'name' : 'Pie Chart with segment rotation' })

chart3.set_rotation( 90 )

worksheet.insert_chart( 'C2' , chart3, { 'x_offset' : 25 , 'y_offset' : 10 })

workbook.close()

Output:
Output 3

Similar Reads