Plotting Pie Charts in Excel by using XlsxWriter Module:

The XlsxWriter is a library in Python gengerally used in the field of data science.. This module is similar to openpyxl module and performs functions like creating, writing, arithmetic operations, and plotting of graphs.

Charts comprise of atleast one series of one or more data points. The series are comprised of reference to cell ranges.

To plot charts on an excel sheet, firstly, we need to create chart objects of specific chart type. After the creation of chart object we need to insert data and then add that chart object in the sheet object.

Code 1: Plotting a simple Pie chart

In order to plot a simple Pie chart on an excel sheet, we use the add_chart() method with the type ‘pie’ keyword argument of a workbook object.

Plot Pie Chart in Excel using XlsxWriter Module in Python for Data SciencePlot Pie Chart in Excel using XlsxWriter Module in Python for Data Science

OUTPUT:

Code 2: Plotting pie chart with user defined segment colours

In order to plot pie chart with user defined segment colour on excel sheet, we use add_series() method using points keyword argument of chart object.

Plot Pie Chart in Excel using XlsxWriter Module in Python for Data Science

OUTPUT:

Plot Pie Chart in Excel using XlsxWriter Module in Python for Data Science

Code 3: Plotting Pie chart with rotation of segments

In order to plot a pie chart with rotation of segments, we use set_rotation() method with definite angle argument of chart object.

OUTPUT:

Plot Pie Chart in Excel using XlsxWriter Module in Python for Data Science

To learn more about charts in python for data science, you can check this and this as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.