Locked lesson.
About this lesson
We'll discuss how to add Surface Charts to your spreadsheet.
Exercise files
Download this lesson’s related exercise files.
35 - Surface Charts.docx61.5 KB 35 - Surface Charts SOLUTION.docx
58.9 KB
Quick reference
Surface Charts
In this video we'll look at Surface charts.
When to use
Use this whenever you need to create a Surface chart for your spreadsheet.
Instructions
# Import the charts
from openpyxl.chart import Reference, Series, SurfaceChart, SurfaceChart3D
from openpyxl.chart.axis import SeriesAxis
# Create a Chart instance
chart = SurfaceChart()
# Change to wireframe
chart.wireframe = True
Hints & tips
- from openpyxl.chart import Reference, Series, SurfaceChart, SurfaceChart3D
- from openpyxl.chart.axis import SeriesAxis
- chart.wireframe = True
- 00:04 Okay, in this video I want to look at SurfaceCharts.
- 00:07 So I've created a new file.
- 00:08 The last one was getting a little bit cluttered.
- 00:09 So we're going to call this surface.py.
- 00:12 And I've created a spreadsheet called surface, and
- 00:14 we're going to save this as surface2.
- 00:16 So before we get into this, let me just open this surface spreadsheet, and
- 00:19 take a quick look at the data.
- 00:23 So this is just some dummy data.
- 00:25 I've got 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 20, 30, 40, 50, and
- 00:30 then just some random numbers in here.
- 00:32 If you're not familiar with what surface charts are, you could just skip this.
- 00:35 But if you know what surface charts are and you need to use one for
- 00:38 your particular set of data, this is how you do it.
- 00:41 So okay, so just our basic dummy data.
- 00:43 So up here where we import our charts,
- 00:45 I got rid of the long list of charts that we've already looked at because we don't
- 00:49 need all of them every time we run this thing.
- 00:51 We're going to need several things to this SurfaceCharts.
- 00:53 We need SurfaceChart obviously, we probably want SurfaceChart3D,
- 00:58 if we wanted the 3D one, we might as well because that's fun.
- 01:03 We also need reference, we've looked at this before, and we need Series,
- 01:06 I believe we looked at this before.
- 01:08 Now, in addition to that, we also need to import something else,
- 01:13 we need from openpyxl.chart.axis.
- 01:17 We need to import SeriesAxis, and notice the upper case S and the uppercase A.
- 01:24 So this just allows us to make series out of our axis basically.
- 01:28 So okay, down here, we can define our SurfaceCharts.
- 01:31 And for our labels and data is basically the same as before,
- 01:34 we have our min_column for our labels, min_column= 1.
- 01:37 And then for min rows 2 through 10, which is obviously 2 through 10, right?
- 01:44 These rows.
- 01:46 And for our data, we have min_column, max_column, min_row, and max_row.
- 01:50 And our column start at 2, the actual data, and go to 6.
- 01:54 So that would be columns 2 through 6, right?
- 02:02 And min_row through 10,
- 02:04 which is just basically all of our stuff here from row 1 through 10, okay?
- 02:10 Pretty simple.
- 02:11 Then our chart.add_data is the same as all of the things we've done so far.
- 02:16 In this chart.set categories, if we set our labels as the categories just like
- 02:20 we've done for all the other charts.
- 02:22 So we can give this a chart title, I'm going to call this one contour.
- 02:25 And then all we have to do is just add this chart to somewhere on our
- 02:29 spreadsheet.
- 02:30 So I'm going to pick A12.
- 02:31 So let's go ahead and save this and run surface.py, okay?
- 02:36 So then we can open surface2, and we see this cool contoured SurfaceCharts.
- 02:42 Very cool.
- 02:42 So that's the basic SurfaceCharts.
- 02:45 We can close this and head back over here to our code.
- 02:49 And we can change this to a wireframe if we want,
- 02:53 simply by going chart.wireframe = True.
- 02:56 So if we save this and run it, and then open up surface2 again.
- 03:03 We get the same chart, but now it's a wireframe.
- 03:05 So that's kind of cool, right?
- 03:07 So let's close this.
- 03:08 We can also do, let's comment out the wireframe thing.
- 03:12 We can also do a 3D version of this SurfaceChart just by calling 3D, right?
- 03:17 Remember, we imported SurfaceChart3D up here.
- 03:20 So let's go ahead and save this and run it, and let's take a look at surface2.
- 03:26 And now we get this 3D in, that's kind of cool looking, and
- 03:29 we can also make this a wireframe as well.
- 03:31 So let's close this by just uncommenting out this chart.wireframe = True.
- 03:36 Save it, run it, open it and now we get this cool 3D wireframe.
- 03:42 So those are SurfaceCharts, pretty simple.
- 03:44 If you're particular set of data requires a SurfaceChart.
- 03:48 This is how you would do it.
- 03:49 So that's all for this video.
Lesson notes are only available for subscribers.