0% found this document useful (0 votes)
12 views

Python - Plotting Doughnut Charts in An Excel Sheet Using The XlsxWriter Module

Uploaded by

harshit.content
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
12 views

Python - Plotting Doughnut Charts in An Excel Sheet Using The XlsxWriter Module

Uploaded by

harshit.content
Copyright
© © All Rights Reserved
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 5

Python - Plotting Doughnut charts in an excel sheet using the XlsxWriter

module

Python is a high-level programming language which was developed by Guido


van Rossum, an all-purpose programming language Python, which was first
made available in 1991. It is employed for a variety of purposes, including
system scripting, software development, and server-side web development.
Python comes with a lot of flexibility and adaptability. Its use in a variety of
domains, including AI, ML, computer vision, data science, etc., can be
attributed to this. One such area is data visualization, and Python offers a huge
selection of packages designed just for it.
In this tutorial, we'll make use of the Python XlsxWriter module, which allows
us to work with Excel files. The mechanism to produce and modify data in an
Excel sheet is provided by this module. Without needing to access the file in
Excel or another spreadsheet program, we'll demonstrate how to utilize this
module to build a Doughnut chart on an Excel sheet.
Like a pie chart, a doughnut chart depicts how different sections relate to the
total while allowing for the inclusion of several data series. In a doughnut
chart, each data series you display results in the addition of a ring to the chart.
The first data series is shown in the chart's centre.
Problem Statement:
Plotting Doughnut charts in an excel sheet using the XlsxWriter module in
python
Solution:
Since the XlsxWriter library is needed to solve this issue, we will first install it
using the command
"pip install XlsxWriter."
Then, after importing the library, we'll make a blank excel file, add some data
to it, and use that information to make the plot.
Algorithm
 Importing the XlsxWriter Python library will be our first step.
 Using the library's Workbook function, we'll create a blank Excel file.
 The next step is to create an empty sheet so that we may add data to it
using the add worksheet function.
 Then, using the write row and write column functions, we will build data
objects and enter them into the spreadsheet.
 Now, we'll use the workbook's add chart technique to add a chart to the
worksheet.
 It will be converted to a doughnut chart using the keyword-type
doughnut.
 The series we want to display will then be added using the add series
function of the chart object.
 We will now enter the chart into the sheet using the insert chart
technique.
 We'll finish the program by utilizing the workbook's close method to
shut the sheet.

Example:

import xlsxwriter
wb = xlsxwriter.wb('chart_doughnut1.xlsx')

ws = wb.add_ws()

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

headings = ['Category', 'Values']


data = [
['Glazed', 'Chocolate', 'Cream'],
[50, 35, 15],
]
ws.write_row('A1', headings, bold)

ws.write_column('A2', data[0])
ws.write_column('B2', data[1])

chart1 = wb.add_chart({'type': 'doughnut'})

chart1.add_series({
'name': 'Doughnut sales data',
'categories': ['Sheet1', 1, 0, 3, 0],
'values': ['Sheet1', 1, 1, 3, 1],
})

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

chart1.set_style(10)

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

wb.close()
Output:
Explanation:
The XlsxWriter library is imported first in the example above, which loads all
the classes and functions required to plot the chart. The Workbook method
from the library is then used to create a blank workbook in the system. This
approach is used to build a workbook object. This object is now connected to
the system's real file. To create a new Excel sheet, we utilize the add
worksheet function of this object. Following that, we apply a special formatting
style—in this example, bold—to the heading, which will be utilized for the
headers. The data is then created into list objects, which are used as
parameters for the write row and write columns functions to enter the data
into the worksheet.
used to create a chart object of the Workbook, and the keyword parameter
type is used to specify that it should be a doughnut chart. The range of data to
take into account for charting was then supplied by the addition of series. We
also have the default color that each category uses when doing this. The plot's
title was then specified using the set title function. We add the plot to the
sheet, then use the Workbook object's Close function to close it to terminate
the programme.
Since this is a commonly used library, all of its methods have been tuned for
optimal performance to increase efficiency in the least amount of time.
Conclusion:
In this post, we learned how to utilize the XlsxWriter module, which is popular
for allowing users to write and edit data on Excel files using Python. This library
offers methods for arithmetic and logical operations as well as ways to enter
data, prepare data, and create plots and graphs. Using this library, our main
aim was to produce a doughnut chart. In this instance, we saw a doughnut
chart in its most straightforward form. In addition to this straightforward chart,
we also have techniques and qualities that may format the chart and alter its
appearance to produce a plot that is much more aesthetically appealing.

You might also like