Alison's New App is now available on iOS and Android! Study Reminders Support
Text Version

We will email you at these times to remind you to study.
• Monday

Tuesday

Wednesday

Thursday

Friday

Saturday

Sunday

00:00 Speaker 1: Hello everybody and welcome to Part 3 of Section 4. In this tutorial we're gonna be talking about the slightly more complex subplot to grid. I say it's more complex mostly just because it comes with more options and it's a bit more customizable, but, at least to me, it makes a lot more sense and it's actually my preferred method for subplots. So we'll keep everything kind of the same as we've been doing. Also, let's do... I guess we can leave... We can leave this function here, that's no problem, and we can just redefine these axes, basically, and I'm gonna get rid of at least AX... Well, we'll just do this, and then let's do this. Let's just have three for now. So with our stock chart, kind of what we're looking for is to possibly have, maybe, a smaller subplot on top, a larger middle subplot, and a smaller subplot on the very bottom. People usually use these for indicators and stuff like that. So that's kind of what we're looking for.01:02 S1: So with subplot to grid, the way it works is, like before, this was our basic subplot. Subplot to grid, you basically... You start off and right out of the gate, you specify the grid and you can specify different grid size. So let's say you start off... We're gonna start off with a 6x1, so let's use that. So we're gonna say it's a 6x1, but you could have a 6x2 or if you needed to have two you could do that. But anyway, so we're gonna use a 6x1, though. And so, right out of the gate you've got six slots on the top. So let's just... Let's add these here. 1 and then makes us to 3, 4... Wow, these are way too small, but we'll fix it. 4, and then 5. So 1, 2, 3, 4, 5 and 6. Okay.02:03 S1: So this is our 6x1 grid. Obviously our bottom is a bit too large, so we can just bring that up a bit. So we've go this 6x1 chart and that's what subplot to grid, we set 6x1. You can set your other axes to be different measurements, but it generally doesn't make any sense to do that, because it's not necessary in subplot to grid. So what's gonna happen in subplot to grid is you're gonna... Say you're gonna specify an axis, first you specify the height and basically the height and width of the entire chart and then you specify the start point for... So basically you can think of it as an X and Y. So we have... If you had more columns it'd be one thing, but we're gonna start, say AX1 at zero, zero. So that would be up here, at the top corner, this would be zero, zero. And then you specify the rows band, so how many rows do you want this subplot to span down? And then you have a column span and that's how many columns over do you want it span.03:05 S1: Now, for us, column span is gonna be 1 for everyone, because we only have one column, but you can envision maybe scenarios where you might have multiple columns. So for this one, we want it to basically just occupy this first spot, right here. So to do that... And let's just do this, actually. So we want it to occupy... I really thought this was gonna have a fill, solid fill. Thank you.[chuckle]03:31 S1: We want it to occupy here. So this would be start point 0, 0. Row span would be 1, or you could do 2 or something like that. But we'll do 1. And then column span 1. And then you might have another chart, or figure, or subplot rather, and it would be here. Okay? And what this one would be, would it be it's start point would be not zero, zero, it would be at 1, 0, and then row span would be 1, 2, 3, 4. Column span would be 1. And then, again, finally, you'd have your last one, your last subplot would be maybe down here and this would be start point of... This would be 0, 1, 2, 3, 4. Start point of 5, and then 5, 0, basically, 'cause there's no columns. And then it would span, really, one row and one column.04:28 S1: So let's try to make a chart that does just that. So we're gonna move this over here; we're done with that. And now we need to define AX1, 2 and 3, to view exactly what we just wanted. So first of all, you're gonna say AX1 equals, and we do plt.subplot2grid, literally the number 2 grid. And then the first element here, you can even see here, the things that it gives us. So the first one is gonna be the shape. So that's the entire figure's shape. Then you've got location. This is the start point. Then row span, column span. So the shape of this subplot, or at least the shape of this grid, is a 6x1. Okay? Then our start point will be 0, 0, 'cause we want it to start at the very, very top. Row span will be equal to 1. Col span will be equal to 1. So that's axis 1. So then, axis 2 would be, basically the exact same thing. So AX2 is equal to plt.subplot2grid. Again, the first parameter, the shape, 6x1. Nothing changed there. Then we have the start point. This time the start point's gonna change. This one starts at 1, 0. And then, the row span. So how many rows is this gonna span, is actually 4 now. And then col span will be equal to 1.05:57 S1: Finally, we have AX3, that'll be our final axes. Plt.subplot2grid, where... What's the shape of the entire figure? It's still 6x1. Now the start point, this one starts at 5, 0. It's gonna row span 1 and it's gonna span columns... Oops, put that in parenthesis. Row span equals 1 and then col span equals 1. Okay. So, we can save that now and let's go ahead and run it. And then here you go, you can see that what we got is kind of what we were searching for and that was basically like this. So, we've got a chart on top, a big chart in the middle, and a little chart on the bottom again. So, there's our chart there. So, those are the two major types of subplots. Again, I prefer subplot to grid because you can make the grid as many as you wanted, right? You can make it 6x1 if you want, but if you're envisioning that you're gonna have maybe two ele... The bottom one will have kind of two points to it, then that's fine. Then you can change this grid to be a 6x2. And then you're not counting which plot number is this and that's the one that occupied. You don't have to deal with that. Also, subplot to grid gives you a lot more customization within here, since you're giving it the size, you're giving it the starting point, how many rows, how many columns.07:24 S1: It just seems to be a little more intuitive to me, but everyone is different. So, you might find that subplot to grid is non-intuitive and the basic subplot from before was more intuitive to you. So, use whichever you want to use. We're gonna continue on with subplot to grid, but you could try following along with the other subplot if you wanted. So in the next tutorial, what we're gonna do is, we're gonna kind of incorporate what we just learned now, back into our open high-low closed chart. And then, we're gonna kind of start doing some more advanced customization with that open high-low closed chart. So, that's what you guys have to look forward too. So, stay tuned for that. Thanks for watching.
00:00 Speaker 1: Hello, everybody, and welcome to part four of section four of our Data Visualization with Python and Matplotlib Tutorial Series. In this part, what we're gonna be talking about is incorporating what we've learned with subplot to grid to our open-high-low-close candlestick chart, and then we're gonna be moving on from there with some more advanced customization of Matplotlib. So what we're gonna do is we're gonna use the code from part 16 from section three, and that would be this code here. I'm just gonna CTRL A, CTRL C for copy, CTRL A, CTRL V to paste. So I save and run that real quick, and we bring up that chart, say we'll plot Exxon, we get this chart here. So we're using one of our created styles here. And we've got the last price here and stuff like that. So what we're gonna be doing now is close that, and let's go ahead and add a little bit more to that closed price, first of all. So in that ax1.annotate for that last price, I just changed five to eight there, see if that fixes it a little bit.
01:08 S1: That's a little better. We'll just leave it out there for now. Anyways, so we're gonna be adding some more subplots here and talking about the nuances and more advanced customization. So first of all, what we wanna do is, let's say we wanna have... At least we wanna have maybe a chart on top, and then maybe a chart in the middle. So what a lot of people will do is they'll have a graph, you've got the price in the middle, and then maybe they've got an indicator graph on top, and another indicator graph on the bottom. Even if you're not familiar with the indicators, that's totally fine. This isn't a stock or finance tutorial or anything like that. We're just gonna use them as an example, that's it. So if you're not familiar with finance, that's totally fine. We're just using it as an example since it uses date data, and other stuff that's useful and more challenging with graphing. So we're gonna close out of this. And first of all, we're gonna come up here to where we're defining our axes. And we've already started using subplot to grid, but we're gonna change the grid to that 6 x 1 we were talking about.
02:12 S1: We'll change that grid. It's still gonna start at 0,0, but ax1, this is gonna be the top one. So we're gonna say it's a rowspan that is equal to one, and then we're gonna say it's a column span that is also equal to one. And then we're gonna add a second axes. So we're gonna say now, ax2=plt.subplot2grid. Again, it's gonna be a 6 x 1, and this time, it's gonna start at 1,0. And then rowspan will make this one four, and then colspan will be equal to one. And we could add the third subplot. We'll go ahead and do that, too. So first of all, if you recall before, whenever you're trying to add labels and stuff like that, if we leave this here, it's gonna pertain to ax2. So you cannot say ax2.ylabel; that's invalid. You have to say plt. So because of that, when you go to define the labels of your axes, you need to put it under basically the chart in reference. We'll leave price here, but we're gonna go ahead and get rid of date because we're not gonna be using date. There's gonna be a graph that's actually under it. And so having date here really is gonna serve no purpose, so I'm just gonna comment that out for now.
03:44 S1: And in fact, let's just delete it. And then we're gonna specify a third axes, so ax3, and that's gonna be equal to plt.subplot2grid, and it will be, again, a 6 x 1. It will be the starting point of 5,0, and the rowspan will be equal to one, the colspan will be also equal to one. And we'll leave basically everything else in place. Now, what we've done is the pricing is now on ax2, not ax or ax1. So what we wanna go ahead and do is we wanna come all the way down to this candlestick and we actually want it to be plotting on ax2 now. We want the grid to be present on ax2. We want these locators and basically the label in ax2, ax2 here and ax2 here. I'm also gonna delete this annotation that's commented out. You feel free to keep a record of how to do it though for yourself. I'm gonna delete that. We'll go ahead and leave this annotated price. I think it's kinda neat to have it on our graph, so we'll leave it there. But it needs to be annotated to, again, ax2 not ax1.
05:04 S1: Now, the rest of this can remain. We have a double usage of the plt Y label. We actually may not even get away with it there, so let's get rid of this one. And let's save and run. Let's plot for Exxon, and let's see what comes up. Yeah, so we have price there, that's fine. So this is our first look at the new creation of our data. So we can see that first of all, we got a lot of bottom space, but that's okay 'cause we're gonna start labeling. And we can see that there's a lot of run-over happening here with numbers and stuff like that. Don't worry about that. That's exactly what we're doing this tutorial for, but we're gonna just have to take things one step at a time. But anyway, we can see that now on our middle chart here, we do have the pricing that's being shown, so that's good. We've got the annotation coming through as we expected, that's good, and all of that. So I'm gonna go and close out of this.
06:01 S1: And I'm going to, instead of using our style, let's try using 538. Just curious which one is the preferable version for me. I just kinda like this one, 538. It really doesn't matter. You can use our style or 538. It really doesn't matter. But let's use 538 'cause that's easy to read anyways. At least that's what I'm gonna use, you can use whichever style you wanna use. It really doesn't matter. So close out of this. And now what we wanna do... Let's see if we wanna make any other changes here before we move on. Let's go ahead, and let's come down to here, [06:36] ____ x2. I think what we'll do is we'll pick up on the next changes that we wanna make in the next tutorial, but there's actually quite a few changes that we need to make. So let's go... Coming here, we've got some of the problems of these numbers are running over each other. We've also got this axes, Y-axes running over this axes, Y-axes. Same thing down here. We've got this X-axes that's just being overrun by this axes, the entire axes and subplot of number three. And same thing happening up here, down here, and all that. So we've got a lot of stuff. Also, these axes are not tied to each other at all, so we'll have to add that part in as well.
07:18 S1: So anyways, got a lot of changes that we need to make as we move forward. So stay tuned for that, and thanks for watching.
00:00 Speaker 1: Hello, everybody, and welcome to part five of our section four of Data Visualization with Python and Matplotlib Tutorial Series. In this video, what we're gonna be doing is creating some more data to be shown on our other axes. So for now, what we're gonna do is just a simple moving average. So we're gonna just write a really quick function. Kind of gonna gear this function just to be a really quick function, rather than explaining how to calculate for a moving average. If you're not familiar with a moving average, don't worry about it too much. Basically, a moving average is just a... It's exactly how it sounds. It's a moving window of average prices for x amount of days for whatever. Actually, it doesn't have to be prices at all. It can be on any data. But what it's gonna do is it's gonna smooth out data. And people use different lengths of moving averages to trade stocks and stuff like that.
00:52 S1: So anyways, I'm gonna go ahead and... We don't need to be printing out this stuff anymore, so I'm gonna comment that out. And now what we wanna do is we wanna define a function that will calculate the moving average for us. So I'm gonna come down here and say define moving_average. And moving average will take the values and the window, and the window is how many days are we doing of this moving average. Then what we're gonna do is weights=NP.repeat. And then we'll do 1.0, the window, and divide it by the window. And then we're gonna say the SMAs for simple moving averages equals NP.convolve, and we wanna do that against the values with the weights, and we only want one valid data there. And then at the end of it, we return SMAs. And not returns, but return SMAs. So that's the function to calculate the moving average. I'll explain again moving average once we can visualize it 'cause I assume a lot of people probably know what a moving average is, and a lot of people don't. It's not really a tutorial, again, on finance. We just wanna create some sample data to work with. And this is kind of a real example of what people actually might use for... In this setting, at least. But it's quick to calculate, so good.
02:14 S1: Now, what we wanna go ahead and do is we'll come on down here. Let's also change this to... We're using three months right now. Let's go ahead and do six months. And then, what we do is we're gonna come down, let's come down to before... Basically, after we've grabbed the date data, we'll calculate these moving averages here. So we're gonna say... Well, let's go to the very, very, very, very top of our script, and let's define a couple of constants. So we're gonna say ma1. So this will be the first moving average. That will be 10, and then we're gonna say ma2=30. So this will be a 10-day moving average, and this will be a 30-day moving average. And then let's come on down. After we've grabbed this data, we'll just come down here, and then what we're gonna say is we're gonna say ma1 is equal to something, and then ma2 is equal to something. We'll just use the same one, but we'll just undercase it. So ma1=moving_average. So referencing that function we just made. What is the data we wanna feed through? We wanna feed through closeP. And then we wanna feed, the window is gonna be ma1. And then we do the exact same thing for ma2. So ma2=moving_average, and we feed through the closeP with a period of ma2. So 10 and 30. So this is gonna create and return the moving average just of prices, but we still have to match it up to date.
03:50 S1: So the quickest and dirtiest and easiest way that people might think to do this is, well, we have the moving averages, so we should be able to just say date ma1, date ma2. But this may not work out for us, so let's go ahead and go down. And like I was saying before, generally, you want to keep all your definitions, things that you're doing to axes together. So you define axes up here at the top, that's fine. You define those separately from everything else. But then once it comes time to actually plot and modify the plots, you do it in chunks. So this is the ax2 chunk. And I would recommend you do things in order. So ax3 probably ought to come down here, and this is where the ax3 chunk will be. So let's say ax3.plot, and then date, and then ma1. Let's just think that we might be able to do that.
04:44 S1: And then let's go ahead and plot the ma2. So ax3.plot, and again, datema2. But what happens if we actually try to do this? And let's close this original one out. And then let's run it so we don't have the data from the previous tutorials. We'll plot XOM, currently pulls. And then we get this error. We've actually talked about this error once before in the tutorial, but it says, "Value error: X and Y must have the same first dimension." And by first dimension, what they mean is length. So when you see this error, the first thing you ought to do is be like, "Hmm." If you don't know what's going on... Well, let's try this. Okay, X and Y, for some reason, for ax3.plotdatema1, this isn't working, and it gave us that error that tells us that they're not the same length. So step one might be to do this: Print, len, update, and then print, len of ma1. So what's going on here? So let's save and run that again. Let's come over here. We'll plot Exxon Mobil again. And then we get the same error as before, but we get a little more information out.
05:51 S1: So this is the length of the date list basically, and this is the length of the moving average list. Well, it turns out this one's longer than this one. Why is that? Well, with moving average, if you have let's say a 10-day moving average, can you actually get a 10-day moving average on day number five? No, 'cause you didn't have 10 days ago to average. So this can happen for a lot of reasons, and this is just an example with moving averages. So then, what might we do to solve for this little problem? Well, the easiest thing to do, if you know what is causing this problem... We know the root of the problem is that with moving averages, what I've just explained on day, let's say on day 15, we can calculate the ma1, but we can't calculate the ma2. So what we need to do is we use moving average two, basically number 30, as the start point of our lists, basically. But we can't really start at... We can't be like, "Okay, here's what we'll do: We'll start at part 30 of day, and then not start at that part." That can get messy sometimes. So usually, what you do is you start at the length of what you know to be the full data set, and then you start at the length of the full data set from the largest number of basically invalid data that you might have, and then go onward. So in our case, the largest value of invalid data would be 30 points.
07:28 S1: So coming down to where we're defining these moving averages, what we might do here to make sure both of these are equal in length is we might say the starting point is equal to the length of a full data set. So date, we'll just use arbitrarily. So the length of date, ma2-1, and then onward. So this will be a pretty big number. It should be over 100. So last we can print start, for example. So let's save and run that real quick. XOM, and you get 95, 124. You get 115, but keep in mind 115 is the length of ma1, which was 10. But we have 20 more points for ma30. So if we do the math, 115 minus 20, wouldn't you know it, is 95. So cool. So now, we've got this starting point. Well, the way that we can handle this from here on out with our graphs is where we're plotting date, we can plot date, and then slice date to be starting at the negative start point. So this starts us from basically -95, and that basically just means we just start from 95 points from the last point onward. And then we can do the exact same thing here, -start: , and then we do the exact same thing again here, -start: , and then we can do the same thing again here, -start: .
09:05 S1: So now let's graph this, and we'll do Exon Mobil again, and let's pull up the chart. And there you go. Now we have actual moving averages here. So again, moving average basically is a slow moving average of the actual price. It's a way to smooth out price. So we can see this red line is the larger moving average, the blue line is a little quicker. And you can see, if you were to draw a line through these prices, it would be a pretty smooth up and then down and then over. And that's the same thing, that's basically what this blue line is doing. And then the red line is a little longer term.
09:41 S1: We can talk a little bit more about what people do with these later on, but we've got the chart at least there. Now, of course, we need something on the top as well. So in the next tutorial, we'll create another really simple volatility indicator that goes up here, and it'll be even more basic than moving average, actually. So anyways, stay tuned for that. Thanks for watching.