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 :
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 :
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:
Similar Reads
- Python | Plotting bar charts in excel sheet using XlsxWriter module Prerequisite: Create and Write on an excel file.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 type of Bar charts using realtime data. Charts are composed 6 min read
- Python | Plotting Line charts in excel sheet using XlsxWriter module 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 Line charts using realtime data. Charts are composed of at least one 3 min read
- Python | Plotting Area charts in excel sheet using XlsxWriter module Prerequisite: Create and write on an excel file 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 charts using realtime data. Charts are composed of at least 6 min read
- Python | Plotting column charts in excel sheet using XlsxWriter module Prerequisite: Create and Write on an excel file.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 type of Column charts using realtime data. Charts are compo 7 min read
- Python | Plotting Stock charts in excel sheet using XlsxWriter module 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 Stock charts using realtime data. Charts are composed of at least on 3 min read
- Python | Plotting Radar charts in excel sheet using XlsxWriter module Prerequisite:Create and Write on excel file 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 type of Radar charts using realtime data. Charts are composed o 6 min read
- Python | Plotting scatter charts in excel sheet using XlsxWriter module Prerequisite: Create and Write on an excel file. 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 type of Scatter charts using realtime data. Charts are com 10 min read
- Python | Plotting Doughnut charts in excel sheet using XlsxWriter module 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 variety of doughnut charts using realtime data. Charts are 7 min read
- Python | Plotting Combined charts in excel sheet using XlsxWriter module Prerequisite: Create and Write on an excel sheetXlsxWriter 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 Combined charts using real-time data. Charts are composed of at leas 5 min read
- Python | Plotting charts in excel sheet using openpyxl module | Set – 2 Prerequisite: Python | Plotting charts in excel sheet using openpyxl module | Set – 1 Openpyxl is a Python library using which one can perform multiple operations on excel files like reading, writing, arithmetic operations and plotting graphs. Charts are composed of at least one series of one or mor 6 min read