00:01 Speaker 1: Hello, everybody, and welcome to part two of section six, all about three-dimensional graphing in Matplotlib. So, in this tutorial, we're basically gonna have a quick introduction to the 3D plotting capability of Matplotlib, but a lot of this should look really familiar to you from previous sections here. So, to get started, we need to bring in the three-dimensional axis to make this possible. So, what we do is from "mpl_toolkits.mplot3d," we wanna import axis 3D. So, as you might guess, there's a few subtle differences between a two-dimensional graph and a three-dimensional graph, mainly, the third dimension, but also, in order to interact with and really get a good feel for three-dimensional graphs, it's important that we can kind of change our perspective of the three-dimensional graph.
01:00 S1: So, these graphs need to be able to be click and dragged in the kind of the same way that our other graphs were able to be clicked and dragged and moved around, only this needs to be able to rotate on a three-dimensional axis, as well. So, with that in mind, we have to have the three-dimensional axis. Now, the next thing is pretty typical, we have to import "matplotlib.pieplot" as PLT. Now, that's pretty common across all of the plots so I shouldn't need to explain that one by now. Then, we're gonna go ahead and we'll do "fig=plt.figure," we've done that one before. And now, let's go ahead and add an axis, so we'll just say "ax1= fig.add_subplot," and this will be a one-by-one-by-one, and then we'll say the projection is equal to 3D.
01:53 S1: Now, what this does is basically notifies Matplotlib that we're going to throw three dimensions at it; otherwise, it's going to make this figure and add the subplot as if it is a two-dimensional graph, when it is, indeed, a three-dimensional graph. So now, we need some coordinates. So, we need some Xs, we need some Ys, shortly, Ys, and then, we need some Z. So, for X, we'll have one, two, three, four, five six, seven, eight, nine and a 10. And then, for Y and Z, we'll just add some random numbers here. I'm really not doing anything specific, just add random numbers. You can copy me if you'd like, but it's not necessary.
02:41 S1: So, I'll just make sure they're the same length; otherwise, we'll get errors like we've seen in the past. And then, we're ready to go "ax.plot" and we're gonna "plot_wireframes." So, this will be the first one that we do, then we plot X, Y, and Z. Now, wireframe is just a three-dimensional representation of a line, basically. So, you'll see what I mean here in a moment, but let's go ahead and do "plt.show" and bring this thing up. Whoops, this should be "ax1.plot wireframe," try again. Okay, so here is our three-dimensional graph, we can make it big and all of that. And as you can see, we've got this line and it's kinda zig-zagging around and we really don't get a feel for the three-dimensional aspect until we start moving it around. And then, we can see that this is, indeed, a three-dimensional graph that can be interacted with. The axes kind of automatically flip around as necessary and all of that, so that's pretty useful.
03:43 S1: Now, just like any other graph, because those axes flip around, sometimes, it's useful to have your labels on your axes. So, this is where our labels, our definition of a subplot really comes into play. So, the way that we can do this is with set labels. So, we can say "ax1.set_xlabel" and we can call this whatever we want, but we'll just cal it the X axis. Then, we can say "ax1.set_ylabel" and we can set this to be the Y axis. And then, finally, we can "ax1.set_zlabel" and this is equal to the Z axis. So now, we can save and run that, and now, we have our axis, and we even have labels to the axis. So, as we click and drag to spin this chart around, the axis are popping all over the place, we can see them quite well. And you'll also see how it kinda of angles the label with the axis pretty well. There's some spots where it looks kinda funky, but it does a pretty good job of it, actually.
04:53 S1: So then, the next question, "Can we zoom in?" No. Every time you basically click on this, you're going to be zooming it around. But what you can do, is you can take... Let's click home real quick. Home still works and if you wanted to zoom in, for example, you would right click, and you can zoom like this; it's not gonna give you any more axis, but it's gonna allow you to either zoom in our out as needed. So now, you can look at a pretty small one on the chart or right click and zoom in quite a bit. And now, it's basically off the charts, but anyway. And then, you can always use the back and the forth and the home and all of that to get yourself back oriented to where you wanna be.
05:38 S1: So anyway, that's it with the really basic introduction to the three-dimensional plotting with Matplotlib. We've got a lot more plots to cover, so that's what you guys have to look forward to, so stay tuned to the next video.
00:01 Speaker 1: What is going on, everyone? Welcome to the third part of section six, all about 3D visualization with Python and Matplotlib. What we're gonna talk about in this tutorial is the representation of a 3D scatter plot. So previously, we were drawing a line and connecting the dots, but maybe, you wanna have a scatter plot instead. So, what we're gonna do is let's have... We can use the exact same coordinates as we have before; and really, the only thing we have to change is just AX1, and instead of plot wire frame, we can actually just scatter. And we can do X, Y, Z and that's totally fine. Let's go ahead and graph that up first. And we can see that it's a scatter plot. We've got the dots. One thing to note is the dots that are closest to you are the darkest in color, and the ones furthest away have an alpha applied to them. So, as they get closer to you, they get darker and further away is lighter and more... Actually, I suppose less alpha is applied to them. So, that's pretty nifty. And so, that's a real simple example of a scatter plot.
01:12 S1: Now, like we've shown before, what we can do is, you can have things like you can add color. So, we can say that color equals R for red, and then, we can add a marker, and we can have... Let's try a star marker for now. So now, they're red stars instead of dots. And there are other things apply, basically. So, you've got the further ones away get kind of faded out a little bit, less alpha; and the ones that are close are basically full. Now, the next thing that we might wanna do is like... With scatter plots, a lot of times, you're identifying groups. So, one thing that you might have is... Let's go with the O marker for now. And maybe, you wanna have another scatter. So, what we could do, is we could take this group here, take that. That's fine, copy, paste, and let's change all these to negative for the X. Negative, negative, negative, negative, negative, negative, negative, and a negative. Okay, the other ones we can actually just... I don't know, leave those there. [chuckle] I don't know. Actually, let's change one more row to negatives. Negative here, here, here, here, here, here, and here. And then, let's call this, "x2y2z2". And then, we'll do an "ax1.scatter" and then, we'll do "x2y2z2", and then we'll say the color equals green, and marker can be that O again, that's really fine. So, we can save and run that now.
02:51 S1: And now, you can kinda see there's two major groups to this scatter plot. You've got one group here on the left-hand side and one group on the right side, and as we move it around, we can see what defines these as different. So, if we're just comparing them like this, they kind of overlap each other; and if we're comparing, maybe, on the X-axis, there's not too much difference. We compare on the Y-axis, not too much difference. And then, if we compare, say, look on the Z-axis, that's usually where the major... Let's see if we can pull up the Z, having a really hard time here. Z would be like this, and then, pull it around. Nope, it's just not possible y'all. [chuckle]
03:31 S1: Anyway, so you can kinda compare the groups here and see what are the fundamental differences between these groups. So, we can compare, at least, with the Z-axis here, something like that. And you can see that that's not really making much change, but you can see the difference in the overlap, basically, from... 'Cause on the Z-axis, you've got points kind of all the... Between 10 and two, we can see that we have points all along that kind of... Or 10 and zero, I suppose. The negative two shows there just simply because it's just... It shows a little bit more than what we have plots on. But then... So, we can see here that there's really no difference, then we can compare across the Y-axis, and say, "Well, it's pretty obvious here that the reds are in the positives, and the greens are in the negatives", and then, the same thing is true for this X-axis here where you've got, again, the negatives over here and the positives are over here.
04:22 S1: So anyways, that's how you can use a scatter plot, and then, plot, maybe, different bodies of information, and show them visually, and color them differently, and all of that. Let's close out of this. And the next question, of course, might be something like, "Can we do labels?" So, label equals, and we'll just call this red, and then, we could add a label down here, "label=green," and then, our question, really, is, "Can we get away with "plt.legend"? Save and run that, and look at us, we can even add a legend and everything still works. So, it tells us, "Hey, these dots are the red group and these dots are the green group, and all of that." So, anyways, that's an example of scatter plotting with Matplotlib in 3D, as well as, scatter plotting with multiple groups of data, let's say, and you wanna show the difference.
05:14 S1: So, in the next tutorial, we're gonna be talking about 3D bar charts, so stay tuned for that.
00:00 Speaker 1: What is going on, everyone? And welcome to the fourth part of Section 6, in this part we're gonna be talking about 3D bar charts. Now, 3D bar charts are kind of interesting with 3D plotting because you have to consider quite a few things. So remember before with our bar charts in two dimensions, we actually had to supply three dimensions basically, because you had the X and Y. Actually, even more. So you had the X point, it was a start point, and then you had the Y, which was basically how tall was the bar, but then you actually had another parameter that was what was the width of the bar?
00:40 S1: Now, with 3D bars, you actually have something quite similar to this. So back by popular demand is my Paint application, where I can draw awesome bars. So in 2D you've got basically the X here and then this was the Y, and then you had to give the width, and then we had a bar. But a 3D bar has basically this depth to it, width of that depth, and then also the height of that depth, basically. So with 3D bars, what you have is, you've got your X, your Y, and your Z because we have to have... It's 3D, right? So you have the starting point, which has to be not only like before, we had an X and then a Y. So you've got this starting point XYZ, and then you have to give the height, so that would account for this, and then the width, which accounts for this, and then the depth, which would account for that, and then the rest gets connected. So you actually have to have six pieces of data that you feed into this three-dimensional bar chart. So with that, let's get started.
01:50 S1: We've got this data and actually, we can delete basically all of this, and we'll leave this X, Y, and Z. We'll make zeros, so we'll just do 00000... Whoops 000000 all the way through. And in fact, if you did download NumPy before, like I was suggesting, you're gonna keep seeing NumPy being used, might as well have it. So import NumPy as NP. And what you can do with NumPy, which is pretty nifty, is instead of whenever you find yourself wanting to do something like this, you can use NumPy and you can say NP.0s, and then you specify how many, so 10. And that makes basically this NumPy array of 10 zeros. So it's kind of nifty to have that as your ability.
02:40 S1: Now, the next thing we need is these DVARs, which are I think of them as depth, and these can either be a list, so you can change them per bar, or they can be what's called a scaler, and this would just be something that's applied to all of the bars. So what we have is DX equals something, DY shall equal something, and DZ equals something. So first, let's just make them scaler and we'll say one, one, and one. So these bars will be basically one deep and one wide. So let's go ahead and render that and see what happens to us. We didn't get anything. Hold on. Let me close out of this. You know what we've done? [chuckle] We haven't actually plotted anything. So we have to actually plot the bar.
03:34 S1: So let's do AX1.bar3D, and that's how we plot a 3D bar. Now we have to give the X position, the Y position, and the Z position, so we just do XYZ. Easy enough. And then we do the dimensions, so we'll do DX, DY, DZ. And then that's really all we have to do, so let's try to re re-run that now. And there you go. We've got our bars here, just 3D bars. And because of our labels, we can at least sort of see what's going here.
04:08 S1: So the X axis was the one that was 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, so sure enough, we can kind of line that up and see, yep, that's true. And then the other stuff was the stuff that varied. Now, because we're setting D as all ones, so it's one wide and one long basically, we're not really getting much. Also, the Z axis, we're just really one tall, although we're starting at Z=0, too, for the bars. Now, let's go ahead and close this, and let's make this a little more interesting.
04:37 S1: Let's take our data, and instead of scalers, we still want basically the X and the Y because keep in mind, where is... Let's load up this one more time. Where is the Z axes on this chart? Well, typically, unless you're moving stuff around, X is on the bottom, Y is kind of part of the bottom, and Z is the height. Now, this is just kind of the norm. Obviously, we can move this graph around. We can even flip it upside down if we wanted, although flipping it upside down really doesn't really change the fact of what Z's doing. But anyway, that's just kind of the typical situation.
05:10 S1: So let's close out of this, and we're gonna keep X and Y as equal to one. So just like we did NumPy zeros 10, we can do MP.1s like that. And we'll say we want those to be 10 long, and then we'll do again MP.1s, and again, we'll make that 10 long. And then DZ, we will make a list basically. We'll just do 1, 2, 3, 4, 5, 6, 7, 8, 9, and 10. So this is the variable element. Otherwise, X and Y, X and Y, again just kind of... Those are what's affecting the width of that bar, so how wide and how long is that bar, but how tall that bar is, is being determined by DZ. So let's go ahead and run that real quick. And sure enough, now we have bars of differing heights, so our bar chart looks a little more interesting to us when you flip it upside down for the ultimate effect. [chuckle] Anyway, so those are 3D bar charts.
06:13 S1: And so a 3D bar chart's quite interesting because you actually have quite a few dimensions; you don't only have three dimensions. You've got the starting X, Y, Z; you've got height, width, and length, as well. So you actually get quite a bit at your disposal. It's not a true, say, six-dimensional display, and because the height and width might get in the way of other things, but as long as you know where the starting point should've been, you actually can show more than just a simple, let's say, three data points lining everything up for us. [chuckle] Anyway, so that's it with 3D bar charts.
06:49 S1: In the next tutorial, we're gonna show just a quick, simple example of a... Or it's not really a simple example, but just a quick show of the wireframe again, because you can actually do a lot of really cool stuff with the wireframe, but you have to have cool data to start with. So, I'll show you guys the sample data, and then we'll actually do something cool with the wireframe with that. So anyways, that's what you guys have to look forward to, so stay tuned for that. Thanks for watching.
Log in to save your progress and obtain a certificate in Alison’s free Data Visualization with Python and Matplotlib online course
Sign up to save your progress and obtain a certificate in Alison’s free Data Visualization with Python and Matplotlib online course
Please enter you email address and we will mail you a link to reset your password.