Python - Plotting Doughnut Charts in An Excel Sheet Using The XlsxWriter Module
Python - Plotting Doughnut Charts in An Excel Sheet Using The XlsxWriter Module
module
Example:
import xlsxwriter
wb = xlsxwriter.wb('chart_doughnut1.xlsx')
ws = wb.add_ws()
ws.write_column('A2', data[0])
ws.write_column('B2', data[1])
chart1.add_series({
'name': 'Doughnut sales data',
'categories': ['Sheet1', 1, 0, 3, 0],
'values': ['Sheet1', 1, 1, 3, 1],
})
chart1.set_style(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.