Pie charts and doughnut charts are two popular types of data visualization tools that are widely used to show the proportional distribution of categories within the whole. Both charts can serve as powerful communication aids, allowing viewers to quickly grasp the significance of each component and how it relates to the overall picture.

While pie charts and doughnut charts share many similarities, they also have unique characteristics that make them suitable for different analytical scenarios. In this article, you will learn how to create a pie chart or a doughnut chart in PowerPoint with Python using Spire.Presentation for Python.

Install Spire.Presentation for Python

This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.Presentation

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows

Create a Pie Chart in PowerPoint with Python

Pie charts are designed to resemble a circle, which is divided into sections or "slices", with each slice representing a portion of the whole.

With Spire.Prensetion for Python, you can add a pie chart to a presentation slide using the ISlide.Shapes.AppendChartInit(type: ChartType, rectangle: RectangleF, init: bool) method and specify the chart type as Pie. The following are the detailed steps.

  • Create a Presentation instance.
  • Get the first slide using Prenstion.Slides[] property.
  • Add a pie chart at a specified location on the side using ISlide.Shapes.AppendChartInit(type: ChartType, rectangle RectangleF, init bool).
  • Set and format the chart title.
  • Define some data and append the data to the chart sheet as chart data using IChart.ChartData property.
  • Set series labels, category labels, series values and other attributes using the properties of the IChart class.
  • Set to show label value and percentage value.
  • Save the result file using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation instance
presentation = Presentation()

# Add a pie chart at a specified location on the first slide
rect = RectangleF.FromLTRB (40, 100, 590, 420)
chart = presentation.Slides[0].Shapes.AppendChartInit (ChartType.Pie, rect, False)

# Set and format chart title
chart.ChartTitle.TextProperties.Text = "Sales by Quarter"
chart.ChartTitle.TextProperties.IsCentered = True
chart.ChartTitle.Height = 30
chart.HasTitle = True

# Define some data
quarters = ["1st Qtr", "2nd Qtr", "3rd Qtr", "4th Qtr"]
sales = [210, 320, 180, 460]

# Append data to ChartData, which represents a data table where the chart data is stored
chart.ChartData[0,0].Text = "Quarters"
chart.ChartData[0,1].Text = "Sales"
i = 0
while i < len(quarters):
    chart.ChartData[i + 1,0].Text = quarters[i]
    chart.ChartData[i + 1,1].NumberValue = sales[i]
    i += 1

# Set series labels and category labels
chart.Series.SeriesLabel = chart.ChartData["B1","B1"]
chart.Categories.CategoryLabels = chart.ChartData["A2","A5"]

# Set values for series
chart.Series[0].Values = chart.ChartData["B2","B5"]

# Add data points to series
for i, unusedItem in enumerate(chart.Series[0].Values):
    cdp = ChartDataPoint(chart.Series[0])
    cdp.Index = i
    chart.Series[0].DataPoints.Add(cdp)

# Fill each data point with a different color
chart.Series[0].DataPoints[0].Fill.FillType = FillFormatType.Solid
chart.Series[0].DataPoints[0].Fill.SolidColor.Color = Color.get_RosyBrown()
chart.Series[0].DataPoints[1].Fill.FillType = FillFormatType.Solid
chart.Series[0].DataPoints[1].Fill.SolidColor.Color = Color.get_LightBlue()
chart.Series[0].DataPoints[2].Fill.FillType = FillFormatType.Solid
chart.Series[0].DataPoints[2].Fill.SolidColor.Color = Color.get_LightPink()
chart.Series[0].DataPoints[3].Fill.FillType = FillFormatType.Solid
chart.Series[0].DataPoints[3].Fill.SolidColor.Color = Color.get_MediumPurple()

# Set the data labels to display label value and percentage value
chart.Series[0].DataLabels.LabelValueVisible = True
chart.Series[0].DataLabels.PercentValueVisible = True

# Save the result file 
presentation.SaveToFile("CreatePieChart.pptx", FileFormat.Pptx2016)
presentation.Dispose()

Python: Create a Pie Chart or a Doughnut Chart in PowerPoint

Create a Doughnut Chart in PowerPoint with Python

Doughnut charts are very similar to pie charts, with the primary difference being the presence of a "hole" in the center. This hole can be used to display additional information or to maintain a cleaner look.

To add a donut chart to a presentation slide, you can specify the ChartType parameter of ISlide.Shapes.AppendChartInit() method as Doughnut. The following are the detailed steps.

  • Create a Presentation instance.
  • Get the first slide using Prenstion.Slides[] property.
  • Add a doughnut chart at a specified location on the side using ISlide.Shapes.AppendChartInit(type: ChartType, rectangle: RectangleF, init: bool).
  • Define some data and append the data to the chart sheet as chart data using IChart.ChartData property.
  • Set series labels, category labels, series values and other attributes using the properties of the IChart class.
  • Set to show label value and percentage value.
  • Save the result file using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation instance
presentation = Presentation()

# Add a doughnut chart at a specified location on the first slide
rect = RectangleF.FromLTRB (80, 100, 630, 420)
chart = presentation.Slides[0].Shapes.AppendChartInit(ChartType.Doughnut, rect, False)

# Set and format chart title
chart.ChartTitle.TextProperties.Text = "Annual Report"
chart.ChartTitle.TextProperties.IsCentered = True
chart.ChartTitle.Height = 30

# Define some data
years = ["Year 2020", "Year 2021", "Year 2022", "Year 2023"]
sales = [16500, 28000, 43200, 60000]

# Append data to ChartData, which represents a data table where the chart data is stored
chart.ChartData[0,0].Text = "Quarters"
chart.ChartData[0,1].Text = "Sales"
i = 0
while i < len(years):
    chart.ChartData[i + 1,0].Text = years[i]
    chart.ChartData[i + 1,1].NumberValue = sales[i]
    i += 1

# Set series labels and category labels
chart.Series.SeriesLabel = chart.ChartData["B1","B1"]
chart.Categories.CategoryLabels = chart.ChartData["A2","A5"]

# Set values for series
chart.Series[0].Values = chart.ChartData["B2","B5"]

# Add data points to series
for i, item in enumerate(chart.Series[0].Values):
    cdp = ChartDataPoint(chart.Series[0])
    cdp.Index = i
    chart.Series[0].DataPoints.Add(cdp)

# Fill each data point with a different color
chart.Series[0].DataPoints[0].Fill.FillType = FillFormatType.Solid
chart.Series[0].DataPoints[0].Fill.SolidColor.Color = Color.get_LightBlue()
chart.Series[0].DataPoints[1].Fill.FillType = FillFormatType.Solid
chart.Series[0].DataPoints[1].Fill.SolidColor.Color = Color.get_MediumPurple()
chart.Series[0].DataPoints[2].Fill.FillType = FillFormatType.Solid
chart.Series[0].DataPoints[2].Fill.SolidColor.Color = Color.get_DarkGray()
chart.Series[0].DataPoints[3].Fill.FillType = FillFormatType.Solid
chart.Series[0].DataPoints[3].Fill.SolidColor.Color = Color.get_DarkOrange()

# Set the data labels to display label value and percentage value
chart.Series[0].DataLabels.LabelValueVisible = True
chart.Series[0].DataLabels.PercentValueVisible = True

# Set the hole size of the doughnut chart
chart.Series[0].DoughnutHoleSize = 50

# Save the result file
presentation.SaveToFile("DoughnutChart.pptx", FileFormat.Pptx2016)
presentation.Dispose()

Python: Create a Pie Chart or a Doughnut Chart in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Chart
Sunday, 04 February 2024 06:12

Python: Create Column Charts in PowerPoint

A column chart in PowerPoint is a graphical representation of data that uses bars or columns to show comparisons between categories. It is commonly used to display financial data, statistics, and other quantitative information. Each column represents a category, and the height of the column corresponds to the value associated with that category. Column charts are easy to create and customize within PowerPoint, allowing users to quickly visualize their data.

In this article, you will learn how to programmatically create column charts in a PowerPoint document using Spire.Presentation for Python.

Install Spire.Presentation for Python

This scenario requires Spire.Presentation for Python and plum-dispatch v1.7.4. They can be easily installed in your Windows through the following pip command.

pip install Spire.Presentation

If you are unsure how to install, please refer to this tutorial: How to Install Spire.Presentation for Python on Windows

Create a Clustered Column Chart in PowerPoint in Python

A clustered column chart is a type of bar graph where the bars (or columns) are grouped together in clusters or segments, with each cluster representing a category and the height of the columns within the cluster reflecting the value of a data point for that category.

To add a clustered column chart in PowerPoint using Spire.Prensetion for Python, you can use the ISlide.Shapes.AppendChartInit(type: ChartType, rectangle RectangleF, init bool) method and specify the chart type as ColumnClustered. This method returns an object of IChart class, which you can use to set the chart data, title, series labels, category labels, series values and other attributes.

The following are the steps to create a clustered column chart in PowerPoint in Python.

  • Create a Presentation object.
  • Get the first slide using Prenstion.Slides[] property.
  • Add a clustered column chart to the side using ISlide.Shapes.AppendChartInit(type: ChartType, rectangle RectangleF, init bool).
  • Add text and numbers to the chart sheet as chart data using IChart.ChartData property.
  • Set series labels, category labels, series values and other attributes using the properties of the IChart class.
  • Save the document to a PowerPoint file using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation object
presentation = Presentation()

# Set slide size type
presentation.SlideSize.Type = SlideSizeType.Screen16x9

# Get the first slide
slide = presentation.Slides[0]

# Add clustered column chart
rect = RectangleF.FromLTRB(40, 80, 700, 450)
chart = slide.Shapes.AppendChartInit(ChartType.ColumnClustered, rect, False)

# Set chart title
chart.ChartTitle.TextProperties.Text = "Soda Soft Drink Sales"
chart.ChartTitle.TextProperties.IsCentered = True
chart.ChartTitle.Height = 25
chart.HasTitle = True

# Insert text to chart as series labels
chart.ChartData[0,0].Text = "Product"
chart.ChartData[0,1].Text = "Store A"
chart.ChartData[0,2].Text = "Store B"

# Insert text to chart as category labels
chart.ChartData[1,0].Text = "Diet Coke"
chart.ChartData[2,0].Text = "Mountain Dew"
chart.ChartData[3,0].Text = "Diet Pesi"
chart.ChartData[4,0].Text = "Cherry Coke"

# Insert numbers to chart as values of series
Series1 = [35000, 46000, 28000, 52000]
Series2 = [41000, 32000, 36000, 40000]
i = 0
while i < len(Series1):
    chart.ChartData[i + 1,1].NumberValue = Series1[i]
    chart.ChartData[i + 1,2].NumberValue = Series2[i]
    i += 1

# Set series labels
chart.Series.SeriesLabel = chart.ChartData["B1","C1"]

# Set category labels
chart.Categories.CategoryLabels = chart.ChartData["A2","A5"]

# Set values for series
chart.Series[0].Values = chart.ChartData["B2","B5"]
chart.Series[1].Values = chart.ChartData["C2","C5"]

# Set gap width
chart.GapWidth = 350

# Set overlap
chart.OverLap = -50

# Set fill color of each series
chart.Series[0].Fill.FillType = FillFormatType.Solid
chart.Series[0].Fill.SolidColor.Color = Color.get_CadetBlue()
chart.Series[1].Fill.FillType = FillFormatType.Solid
chart.Series[1].Fill.SolidColor.Color = Color.get_LightBlue()

# Add data labels
for i in range(len(Series1)):
    chart.Series[0].DataLabels.Add()
    chart.Series[1].DataLabels.Add()

# Save the document
presentation.SaveToFile("output/ClusteredColumnChart.pptx", FileFormat.Pptx2019)
presentation.Dispose()

Python: Create Column Charts in PowerPoint

Create a Stacked Column Chart in PowerPoint in Python

A stacked column chart is a variation of the standard column chart where each column represents a category, and the height of the column corresponds to the total value of the category.

To add a stacked column chart in PowerPoint using Spire.Prensetion for Python, you use the ISlide.Shapes.AppendChartInit(type: ChartType, rectangle RectangleF, init bool) method and specify the chart type as ColumnStacked. Then, you can use to set the chart data, title, series labels, category labels, series values and other attributes using the properties of the IChart class.

The following are the steps to create a stacked column chart in PowerPoint in Python.

  • Create a Presentation object.
  • Get the first slide using Prenstion.Slides[] property.
  • Add a stacked column chart to the side using ISlide.Shapes.AppendChartInit(type: ChartType, rectangle RectangleF, init bool).
  • Add text and numbers to the chart sheet as chart data using IChart.ChartData property.
  • Set series labels, category labels, series values and other attributes using the properties of the IChart class.
  • Save the document to a PowerPoint file using Presentation.SaveToFile() method.
  • Python
from spire.presentation.common import *
from spire.presentation import *

# Create a Presentation object
presentation = Presentation()

# Set slide size type
presentation.SlideSize.Type = SlideSizeType.Screen16x9

# Get the first slide
slide = presentation.Slides[0]

# Add a stacked column chart
rect = RectangleF.FromLTRB(40, 80, 700, 450)
chart = slide.Shapes.AppendChartInit(ChartType.ColumnStacked, rect, False)

# Set chart title
chart.ChartTitle.TextProperties.Text = "Soda Soft Drink Sales"
chart.ChartTitle.TextProperties.IsCentered = True
chart.ChartTitle.Height = 25
chart.HasTitle = True

# Insert text to chart as series labels
chart.ChartData[0,0].Text = "Product"
chart.ChartData[0,1].Text = "Store A"
chart.ChartData[0,2].Text = "Store B"

# Insert text to chart as category labels
chart.ChartData[1,0].Text = "Diet Coke"
chart.ChartData[2,0].Text = "Mountain Dew"
chart.ChartData[3,0].Text = "Diet Pesi"
chart.ChartData[4,0].Text = "Cherry Coke"

# Insert numbers to chart as values of series
Series1 = [35000, 46000, 28000, 52000]
Series2 = [41000, 32000, 36000, 40000]
i = 0
while i < len(Series1):
    chart.ChartData[i + 1,1].NumberValue = Series1[i]
    chart.ChartData[i + 1,2].NumberValue = Series2[i]
    i += 1

# Set series labels
chart.Series.SeriesLabel = chart.ChartData["B1","C1"]

# Set category labels
chart.Categories.CategoryLabels = chart.ChartData["A2","A5"]

# Set values for series
chart.Series[0].Values = chart.ChartData["B2","B5"]
chart.Series[1].Values = chart.ChartData["C2","C5"]

# Set gap width
chart.GapWidth = 350

# Set fill color of each series
chart.Series[0].Fill.FillType = FillFormatType.Solid
chart.Series[0].Fill.SolidColor.Color = Color.get_CadetBlue()
chart.Series[1].Fill.FillType = FillFormatType.Solid
chart.Series[1].Fill.SolidColor.Color = Color.get_LightBlue()

# Add data labels
for i in range(len(Series1)):
    chart.Series[0].DataLabels.Add()
    chart.Series[1].DataLabels.Add()

# Save the document
presentation.SaveToFile("output/StackedColumnChart.pptx", FileFormat.Pptx2019)
presentation.Dispose()

Python: Create Column Charts in PowerPoint

Apply for a Temporary License

If you'd like to remove the evaluation message from the generated documents, or to get rid of the function limitations, please request a 30-day trial license for yourself.

Published in Chart