Locked lesson.
About this lesson
In this lesson, we'll look at how to add Doughnut Charts.
Exercise files
Download this lesson’s related exercise files.
34 - Doughnut Charts.docx57.3 KB 34 - Doughnut Charts SOLUTION.docx.docx
58.5 KB
Quick reference
Doughnut Charts
In this video we'll look at Doughnut Charts.
When to use
Use this whenever you need to use a Doughnut chart in your spreadsheet.
Instructions
# Import the charts
from openpyxl.chart import Reference, Series, DoughnutChart
from openpyxl.chart.series import DataPoint
# Create a Chart instance
chart = Doughnut()
# Cut out a slice of data
slices = [DataPoint(idx=i) for i in range(5)]
# Define our Slices
chocolate, vanilla, strawberry, blueberry, raspberry = slices
# Chart a Series
chart.series[0].data_points = slices
# Pull out our slice/wedge
chocolate.explosion = 10
Hints & tips
- from openpyxl.chart import Reference, Series, DoughnutChart
- from openpyxl.chart.series import DataPoint
- slices = [DataPoint(idx=i) for i in range(5)]
- chocolate, vanilla, strawberry, blueberry, raspberry = slices
- chart.series[0].data_points = slices
- chocolate.explosion = 10
- 00:04 Okay, in this video we're going to look at doughnut charts.
- 00:06 So I've created a new file called doughnut pie.
- 00:09 And we're going to import our stuff in just a second, but
- 00:12 it's the basic code we've been working with so far.
- 00:14 We're going to import a spreadsheet called doughnut.xlxs.
- 00:17 We're going to save it as doughnut2 down here.
- 00:20 We're going to do some stuff and we'll go through this in a second.
- 00:23 So first let's look at this doughnuts of this doughnut spreadsheet.
- 00:29 So we have flavors of doughnut, right?
- 00:31 And Vanilla, strawberry, blueberry, and raspberry.
- 00:34 We have sales data from 2020 in 2021.
- 00:36 You can see, I don't know, maybe these are in millions,
- 00:40 50 million whatever, a sales of doughnut in our company.
- 00:44 So we're creating a doughnut chart of doughnut sales,
- 00:47 which is a little weird but.
- 00:48 Okay, so this is the data we're going to be working with.
- 00:52 So let's check out our code here.
- 00:54 We're going to use a doughnutChart.
- 00:56 So we need to import that.
- 00:57 We also need a couple other things.
- 01:00 We need Reference as we've used before, and
- 01:02 we need Series which I believe we've used before.
- 01:04 So we also need to import something called DataPoint,
- 01:08 that will handle the points of our data.
- 01:10 So we can go from openpyxl.chart.series.
- 01:18 We want to import DataPoint.
- 01:22 Okay, so as before, we're opening up our doughnut spreadsheet.
- 01:25 We're going to create a doughnutChart instance here.
- 01:29 And then we need to set our labels in our data just like always.
- 01:32 So we have minimum column one, min_row=2, max_row=6.
- 01:37 So from row 2 to 6, and if we look back at our data again,
- 01:42 we see that's from here to here.
- 01:44 So we're going to leave off our top things there.
- 01:49 And then for the actual data, we've got minimum column 2, and
- 01:52 then rows 1 through 6.
- 01:54 So column 2, 1 through 6, we're pulling all of this data.
- 01:59 So normal stuff we've been doing for all of our other charts, defining our data and
- 02:04 our labels just like before we want to add our data as data, titles_from_data=true.
- 02:10 I'm going to set our categories with these labels, just like we've always done.
- 02:14 And we can set a chart title of let's say Doughnut Sold By Category.
- 02:18 And then or Doughnut Sold By Flavor maybe.
- 02:22 And we can set a chart style.
- 02:24 So I'm going to comment this out.
- 02:26 Again, you can play around with these numbers,
- 02:27 do all kinds of different things with them.
- 02:29 So okay, all of this is sort of boilerplate,
- 02:32 what we've done with all of our other graphs and charts so far.
- 02:35 Here's where it gets a little bit different.
- 02:37 First we need to cut out a slice of our data.
- 02:41 So a donut chart is basically a pie chart with a section sort of cut out of it,
- 02:46 just for graphical, you know, coolness, I guess.
- 02:49 So we could do that.
- 02:52 So let's define our slices.
- 02:52 So let's go slices = and
- 02:54 hen we want to call that data point thing that we imported up here, right?
- 03:02 And then we want to pass an idx = i.
- 03:06 Now we need to define a loop.
- 03:08 So let's go for i range of 5.
- 03:12 So why 5?
- 03:14 Well, because if we look back at our data, we've got one, two,
- 03:18 three, four, five different flavors that we need to loop through, right?
- 03:22 So okay, now we need to define our slices.
- 03:28 Now let's comment this.
- 03:29 So we've got chocolate, we've got vanilla,
- 03:35 we've got strawberry, we've got blueberry,
- 03:40 and we've got raspberry.
- 03:43 And want to set those equal to slices.
- 03:49 Which we've defined up here.
- 03:52 So basically we're saying, hey, these are the things we want to loop through in this
- 03:56 loop with and make data points out of each of these things.
- 03:59 So here we need to chart a series of those things.
- 04:02 So we can go chart, which is the name of our chart thing up here, right?
- 04:06 Chart.series and we want to pass in the zeroth item.
- 04:11 And we want to call data_points and set that equal to slices.
- 04:15 So, strictly speaking, this will do it and if we run this,
- 04:19 this is not going to be as interesting but if we save this and run it.
- 04:26 And then open up, our doughnut2 file,
- 04:33 We get this sort of pie chart like a thing and it's got a thing in the middle that
- 04:38 sort of sets it apart from pie chart a little bit, but
- 04:41 there's no wedge that's been pulled out of here to make it look cool.
- 04:45 So we can do that real quick.
- 04:47 By just calling whichever one of these we want to explode out.
- 04:50 So let's go explode wedge and
- 04:54 let's call chocolate.explosion and
- 05:00 set that equal to 10.
- 05:03 And this is a distance how far out do we want to pull our little wedge?
- 05:07 So if we save this and run it and then, open up doughnut2, we see,
- 05:12 now, this wedge has been pulled away, and that's cool.
- 05:16 We can make it go further by changing the distance, right here,
- 05:19 we could change that to 20.
- 05:21 Now, we could add, again, chart.style.
- 05:24 Let's add this chart.style = 26 in here.
- 05:25 Now, if we save this and run it.
- 05:30 And open this guy.
- 05:31 We see okay, now we've got sort of some style.
- 05:32 This has been pulled out a little bit more 20 instead of 10.
- 05:38 We've got some like shadows going on here and it looks kind of cool.
- 05:40 And again, you can play around with those numbers to find one that you like.
- 05:41 But basically that's a donut chart.
- 05:46 So that's all for this video.
- 05:46 In the next video, we'll look at surface charts.
Lesson notes are only available for subscribers.