Locked lesson.
About this lesson
In this lesson, we'll learn how to add Pie Charts to a spreadsheet.
Exercise files
Download this lesson’s related exercise files.
29 - Intro To Charts And Graphs - Pie Charts.docx60.5 KB 29 - Intro To Charts And Graphs - Pie Charts SOLUTION.docx
56.6 KB
Quick reference
Intro To Charts And Graphs - Pie Charts
Pie charts are common chart that you can add to your spreadsheet.
When to use
Use this whenever you want to add a pie chart to your spreadsheet.
Instructions
# import Charts
from openpyxl.chart import PieChart, Reference
# Create a chart variable
chart = PieChart()
# Define your labels and Data
labels = Reference(ws, min_col=1, min_row=2, max_row=6)
data = Reference(ws, min_col=2, min_row=1, max_row=6)
# Put it all together
chart.add_data(data, titles_from_data=True)
chart.set_categories(labels)
# Set a title for our Chart
chart.title = "Our Pie Chart Title"
#Place the chart on the spreadsheet
ws.add_chart(chart, "D3")
Hints & tips
- from openpyxl.chart import PieChart, Reference
- chart = PieChart()
- labels = Reference(ws, min_col=1, min_row=2, max_row=6)
- data = Reference(ws, min_col=2, min_row=1, max_row=6)
- chart.add_data(data, titles_from_data=True)
- chart.set_categories(labels)
- chart.title = "Our Pie Chart Title"
- ws.add_chart(chart, "D3")
Lesson notes are only available for subscribers.