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")
- 00:04 Okay, in this section, we're going to look at charts and graphs.
- 00:07 And in this video, we're going to look at the pie chart.
- 00:08 I think a pie chart is a pretty good place to start.
- 00:10 I think everyone's sort of familiar with a pie chart.
- 00:12 It's just a big circle that's cut into slices that looks like a pie based
- 00:16 on data.
- 00:17 So we've got our same code, basically, that we've been working on up until now,
- 00:21 I just got rid of all this stuff from the last few videos.
- 00:24 But this time we're going to open up our company_salaries.xlsx file.
- 00:28 If you remember, this is the little project we did earlier in the course.
- 00:32 If you don't remember, if we come over here, we can open this guy.
- 00:36 And we just had John, Mary, Steve, Tina, and Bob and
- 00:39 then some randomly created salaries for each of them.
- 00:42 So in this video, we're going to make a pie chart out of that data.
- 00:44 So the first thing we need to do is import the chart functionality from openpyxl.
- 00:50 So let's go from openpyxl.chart.
- 00:54 We want to import PieChart, and also, Reference.
- 01:00 And notice the P and the C are both capitalized in PieChart.
- 01:04 So to create a pie chart, we just come down here and create a variable.
- 01:07 Call it anything you want, but
- 01:08 I'm going to call it chart because that's what it is, a chart, right?
- 01:11 And then we want to create a pie chart instance.
- 01:15 So charts are made up of usually labels and data, so we need to designate those.
- 01:19 So let's create some variables.
- 01:20 Let's call this one labels and let's call this one data, and to do that,
- 01:25 we need to make a reference to our labels and our data.
- 01:28 So that's where we use this Reference guy.
- 01:30 So we can copy this and just paste it in.
- 01:33 And where do we want to put this?
- 01:34 We want to put it in our ws worksheet, our active worksheet,
- 01:37 which is this guy right here.
- 01:39 And now we need to designate the rows and columns for our labels.
- 01:43 So let me just go through here and then we'll talk about this.
- 01:45 So we want min_col=1, we want min_row to equal 2,
- 01:51 and we want max_row to equal 6.
- 01:55 Now, let's look at these guys.
- 01:56 So the minimum column is the the first column, and then we want rows 2 through 6.
- 02:01 So if we look at our data, that's this column, column 1, but
- 02:06 the the data is 2 through 6.
- 02:08 So that's just these names.
- 02:10 That's not this names label at the top.
- 02:12 We leave that out.
- 02:13 For this one, we just want these labels.
- 02:16 And these are the labels that go with the actual data in the future, right?
- 02:21 So that's that.
- 02:22 And we can do basically the same thing down here for our data.
- 02:25 Just copy and paste this, but for this,
- 02:27 we want column 2 because that's where our data is.
- 02:30 And instead of starting at row 2, we want to start at row 1, and
- 02:34 I'll show you why in just a second.
- 02:35 So basically, looking at this again, that's all of this information.
- 02:39 Now, this is a little different because we're including this salaries label at
- 02:43 the top, and I'll show you why in just a second.
- 02:46 So okay, now we need to sort of put all this stuff together.
- 02:49 So let's go chart.add_data.
- 02:54 And then we want to pass in that data variable that we just created, right?
- 02:58 And we want to say titles_from_data=True.
- 03:05 This is what allows us to start with row 1 and
- 03:08 include this salaries label, because telling our program to get
- 03:13 the title from the data tells it to get it from that first row.
- 03:18 It knows how to do that automatically, that's why we can do that.
- 03:21 So okay, that's our data.
- 03:22 We also need to set the category.
- 03:24 So let's go chart.set_categories.
- 03:28 And categories are labels, right?
- 03:29 So we would just pass in our labels that we defined up here.
- 03:34 Okay, now we can set a title for our chart.
- 03:37 So let's go chart.title, and let's set that equal to employee salaries.
- 03:45 Okay, so now we just need to place the chart on the spreadsheet.
- 03:52 And we do that by calling ws or active worksheet, dot add_chart.
- 03:58 And then we just tell this thing, well, what do we want to add?
- 04:01 Well, we want to add this chart,
- 04:02 which is what we've been building out this whole time, right, this chart variable.
- 04:07 So we want to add that.
- 04:08 And now where do we want to put it?
- 04:10 Let's put it in D2.
- 04:12 And if we look at our spreadsheet here, that's right here.
- 04:16 Now, this is the top left corner of our chart will be in D2.
- 04:20 And the whole chart itself will look something like this.
- 04:23 It'll spread out across all these rows and columns, but
- 04:26 the anchor will be the top left corner of D2 right here.
- 04:30 So just keep in mind, just sort of remember, top left, right?
- 04:34 So okay, finally, we want to save this guy to company_salaries2 as
- 04:39 opposed to company_salaries.
- 04:41 We don't want to overwrite the original file, we want to create a new one, and
- 04:45 that should work.
- 04:46 So let's go ahead and save this, And
- 04:50 let's come up here and python format.py, run this guy.
- 04:55 Okay, now we can head back over to our directory here and
- 04:58 there's our company_salaries2.
- 05:01 So let's open this, and boom, we get this pie chart, really nice looking chart.
- 05:06 And it was just that easy.
- 05:07 So if we kind of eyeball this, I could see it looks like this.
- 05:10 And also, if you click on it, it sort of does its thing here.
- 05:13 Everything highlights, sort of interesting, but to me,
- 05:16 the purple area looks like the biggest one, and that according to this is Tina.
- 05:20 So if we come over here and look at Tina, yep, sure enough,
- 05:23 it looks like she has the biggest salary.
- 05:25 And then the next one, at least to my eye, is probably this light blue one,
- 05:29 which is Bob.
- 05:30 And yep, sure enough.
- 05:31 And then there's the red one, Mary, yep.
- 05:34 And then finally, John and Steve.
- 05:38 Yep, that looks about right.
- 05:39 So just eyeballing this, the pie chart looks correct, and just that easy.
- 05:43 So that's how to do pie charts.
- 05:45 In this video,
- 05:46 I just wanted to give you a very quick overview of sort of an intro into graphs.
- 05:50 In the future, we're going to look at pie charts in much greater detail because
- 05:54 there's a lot of stuff you can do more than what we just looked at in this video,
- 05:57 but that's a sort of broad level look at using charts.
- 06:00 Now, there are a lot of other charts that you can use with openpyxl, and
- 06:03 in the next video, we're going to go over most of those very quickly.
- 06:06 And then in videos after that, we'll look at each of those in greater detail.
- 06:09 So that's coming up in the next video.
Lesson notes are only available for subscribers.