Locked lesson.
About this lesson
In this lesson, we'll look at Bar Charts - Vertical, Horizontal, Stacked, and 3D - in a little more detail.
Exercise files
Download this lesson’s related exercise files.
31 - Bar Charts - Vertical, Horizontal, Stacked, and 3D.docx60.5 KB 31 - Bar Charts - Vertical, Horizontal, Stacked, and 3D SOLUTION.docx
56.7 KB
Quick reference
Bar Charts - Vertical, Horizontal, Stacked, and 3D
In this video we'll look at Bar Charts in a little more detail.
When to use
Use this whenever you need to use a Bar chart in your spreadsheet.
Instructions
# Import the charts
from openpyxl.chart import Reference, Series, BarChart3D
# Create a Chart instance
chart = BarChart3D()
# Give Axis titles
chart.y_axis.title = "Y Axis Title"
chart.x_axis.title = "X Axis Title"
# Change the shape of the bars
chart.shape = "pyramid"
Shapes include: pyramid, pyramidToMax, cylinder, box, coneToMax, cone
# Set style
chart.style = 5
# Change chart positioning
chart.type = 'bar' # default is col
# Make stacked
chart.grouping = 'stacked'
chart.overlap = 100
The rest stays the same from the last video.
Hints & tips
- chart = BarChart3D()
- chart.y_axis.title = "Y Axis Title"
- chart.x_axis.title = "X Axis Title"
- chart.shape = "pyramid"
- pyramid, pyramidToMax, cylinder, box, coneToMax, cone
- chart.style = 5
- chart.type = 'bar' #default is col
- 00:04 Okay, in this video I want to go through some of these graphs in more detail, and
- 00:07 in this video we're going to look at the bar chart.
- 00:09 So I've changed our chart to BarChart3D here.
- 00:13 And let's just come down here, and here we have our chart title.
- 00:17 You can do a lot of different things by chart.something and so we're going to look
- 00:21 at some of those in this video and do some other things as well.
- 00:24 So the first thing we can do is add a label for our x or y-axis.
- 00:29 So we can go chart.y_axis.title, and we can set this equal to anything we want.
- 00:35 Let's just call this Y Axis Title.
- 00:40 And we can do the same thing for the x-axis or
- 00:43 just copy and paste and change this to X Axis Title.
- 00:47 So let's go ahead and save this and head over to our terminal and
- 00:51 run this guy again.
- 00:53 And now we can open our company_salaries file.
- 00:57 And we see now we have a y-axis and an x-axis title.
- 01:00 So that will work with most of the graphs, that's not really a bar chart thing,
- 01:04 but it's definitely useful.
- 01:05 So let's go ahead and close this.
- 01:07 We can also change the shape of the items in the graph.
- 01:11 We have a bar, we can change those different shapes, so
- 01:16 we can go chart.shape, and we could set this equal to, for instance, pyramid.
- 01:23 Let's save this head back over here and run.
- 01:27 And if we open our file,
- 01:29 we see we have these sort of pyramiding things instead of square blocks.
- 01:34 So there are a bunch of these different styles you can do.
- 01:37 Let me just head over here and paste in, so there's pyramid,
- 01:42 pyramidToMax, cylinder, box, coneToMax and cone.
- 01:47 So if you're curious we could try cylinder, save this, run it.
- 01:54 Open this guy again, and you see now they're sort of rounded.
- 01:58 So, play around with these if you want, you can check them each, I'm not going to
- 02:01 go through each one because that would just take forever and you can try that on
- 02:05 your own if you're interested in the different styles that are available.
- 02:08 Speaking of styles, we can also set a specific style.
- 02:12 So, this one's a little weird, so let's go chart.style.
- 02:17 And let's set this equal to say 5.
- 02:20 So let's save this and run it.
- 02:23 And if we open this guy again, you see now these are different colored, right?
- 02:28 You can get crazy with this.
- 02:29 So let's close this and change this to for instance 47.
- 02:35 Just pick a number, right?
- 02:37 So let's run this again and open this.
- 02:42 You can see now it's black and blue.
- 02:43 So there's not a really good source of documentation for those different numbers.
- 02:48 So I can't really tell you what number 23 does or what number 13 does, but
- 02:52 if you want to go through there and look at all those, feel free to do that.
- 02:56 I wish there was better documentation on that.
- 02:58 There isn't, but play around with it and maybe you'll find a style that you like.
- 03:02 So, okay, that style.
- 03:05 Up until now, the graph has been vertical up and down.
- 03:08 We can change that as well.
- 03:10 So let's go chart.type, and we can set this equal to bar.
- 03:17 Now, the default is col and col is vertical up and
- 03:21 down if we want horizontal left to right, its bar so we can save this.
- 03:27 And let's change let's get rid of this style or change it to 1 I guess.
- 03:32 And let's run this again.
- 03:36 And open this guy.
- 03:38 And you can see now it's sort of horizontal, left to right,
- 03:41 the bars are going this way instead of up and down.
- 03:44 So that's interesting, and notice the style 1 I guess is black and white, so
- 03:48 that's kind of weird.
- 03:50 So okay, so like I said, by default, it's called short for column.
- 03:54 So now, we can also do stacked.
- 03:58 And to do that we need to actually change our original data.
- 04:00 So let's open our company_salaries file,
- 04:05 and let's just add some more data, so 123456,
- 04:11 12345, 12345, 112345.
- 04:16 I'm just making up data here obviously 89123.
- 04:19 So, okay, so let's save this now.
- 04:21 Now we've got this third column.
- 04:23 So let's come back here and where we defined our data,
- 04:28 we have main column 2, we can go max_column 3,
- 04:33 to include that third column we just created.
- 04:37 Then we can come down here, and we can go chart.grouping,
- 04:43 and set this equal to stacked.
- 04:46 And we also have to say chart.overlap, and set that equal to a 100.
- 04:51 And if we save this and come back here, and run this guy one more time,
- 04:56 and then open this, we see there now stacked this second row of
- 05:01 data here is stacked in these lighter colors and we're still in style 1.
- 05:08 So you can change it to a different color, if you want to take that style thing off
- 05:11 completely to get to the default, but that's cool.
- 05:14 So those are bar charts and a little bit more detail,
- 05:17 a lot of stuff you could play around with test out different things, and that's fun.
- 05:21 So, in the next video we'll look at area charts and a little more detail.
Lesson notes are only available for subscribers.