Locked lesson.
About this lesson
In this lesson, we'll look at Projected Pie charts, which are pie charts with some chunks pulled out and projected to the side.
Exercise files
Download this lesson’s related exercise files.
37 - Projected Pie Charts.docx61.6 KB 37 - Projected Pie Charts SOLUTION.docx
58.6 KB
Quick reference
Projected Pie Charts
In this video we'll look at Projected Pie charts, which are pie charts with some chunks pulled out and projected to the side.
When to use
Use this whenever you'd like a pie chart with a little more interesting visuals.
Instructions
# Import the charts
from openpyxl.chart import Reference, ProjectedPieChart
# Create a Chart instance
chart = ProjectedPieChart()
# Set The Chart Type
chart.type = "pie" # can be pie or bar
chart.splitType = "pos" # can be pos or val
Hints & tips
- from openpyxl.chart import Reference, ProjectedPieChart
- chart = ProjectedPieChart()
- chart.type = "pie" # can be pie or bar
- chart.splitType = "pos" # can be pos or val
- 00:04 Okay, in this video I want to look at projected pie charts.
- 00:07 And projected pie charts are just like pie charts, except they have a chunk of
- 00:12 the pie chart sort of pulled out and set aside and shown graphically.
- 00:16 So we've got a file called projected.py, and
- 00:18 it's basically our same starter code that we've been working on.
- 00:23 And we're going to open a spreadsheet called pies and
- 00:25 we're going to save it as pies2.
- 00:26 And before we get started, let's take a look at that data really quickly.
- 00:30 So I've just got a very basic set of data here.
- 00:32 We've got pizza, we've got toppings, pepperoni, mushroom, onion, and cheese,
- 00:37 and amount sold, let's say, right?
- 00:39 So very basic data.
- 00:41 So up here we need to import a few things.
- 00:43 So to use a projected pie chart we need to import ProjectedPieChart,
- 00:48 we also need Reference.
- 00:51 And that's really all we need.
- 00:52 So we can come down here to our chart, and
- 00:54 this is going to be our projected pie chart.
- 00:57 And all of this stuff is the same as basically all of the charts we've done so
- 01:01 far, right?
- 01:02 We just set our min columns and rows.
- 01:04 So if we pull this data back up again, and let me minimize this, we can see
- 01:09 we're starting out with our labels column 1, that's this column right here.
- 01:14 Rows 2 through 5, right, so 2 through 5.
- 01:18 These are basically the labels for our legend, right?
- 01:22 And we have our data starting at column 2, with row 1 through 5,
- 01:25 which is just like this.
- 01:27 And then again, we add our chart data like before, and we set our labels like before.
- 01:34 So now we need to define a few things that are unique for this specific chart.
- 01:37 So we can go chart.type, and let's set this equal to pie.
- 01:42 Now this can be pie or bar, and we'll look at both of these.
- 01:47 So then we need a chart.splitType, and notice the capitalization.
- 01:52 We'll set that equal to pos.
- 01:56 Now, this could be pos, this could be pos or val,
- 02:01 and it's almost always going to be pos.
- 02:05 And we could go chart.title, and let's set this equal to, I don't know, Pizza Sold.
- 02:10 And then finally, we can come down here like always and
- 02:13 tell our program where we want this to go.
- 02:16 I'm going to put it on A10, and we're going to save it as pies2.xlsx.
- 02:21 So okay, let's go ahead and save this.
- 02:22 Now this is going to be projected.py, so let's head over here and run it.
- 02:27 Okay, and now we can open our pies2 file, and when we do,
- 02:30 we see our projected pie chart.
- 02:32 And you could see the biggest thing here is pepperoni, this is 50.
- 02:37 All the rest of these guys you can see are sort of pulled out of here and
- 02:42 projected to the side.
- 02:44 And that's our projected pie chart, very cool.
- 02:47 So there are a couple of things we can change here.
- 02:49 So let's close this.
- 02:51 Like I mentioned up here, we can have pie or bar.
- 02:55 So this deals with the projection.
- 02:57 So if we make this a bar, it's going to projected out not as a pie but as a bar.
- 03:01 So let's go ahead and save this and run this again and see what this looks like,
- 03:05 so let's go pies2.
- 03:06 And you can see now the projection is a bar.
- 03:10 So it's really just a matter of preference which you would actually prefer.
- 03:13 So that's all for this video and that's all for the charts and graphs section.
- 03:16 In the next section we're going to start to look at tables.
Lesson notes are only available for subscribers.