Alison's New App is now available on iOS and Android! Download Now

    Study Reminders
    Support

    Adjusting Customization Options

    00:00 Speaker 1: Hello everybody, and welcome to part six of our data visualization with Python and matplotlib, a Section Three tutorial. In this tutorial, we're gonna be talking about a few things. First of all, we noticed that our graph is a bit too large in spots and then maybe too small in other spots. So let's go ahead and pull that up. So here's our graph, and we can see that at least on the default load, the dates are a little over the edge. But then also on the left-hand side and the right-hand side, maybe we've got maybe too much space. Although if we had a label, maybe we wouldn't. But anyways, let's at least set up the initial subplot configuration.

    00:41 S1: So for those of you who aren't familiar, usually what I would do... And you should be familiar if you've been following along at least, usually what I'll do is I'll... You click on "configure subplots" and you can play with these sliders to set everything the way that you really want it to be. So maybe with left, this is probably good enough, that's still room for a label there. And then bottom, we need a little more space. So maybe 16 on the bottom, and then we would adjust the right a little bit. Maybe... Yeah 0.94 is probably more than enough over. And then the top, we can adjust that. If we had the title, it probably won't matter much, but let's just for now, we'll go ahead and make it a 0.95. And then the width space and header space, we don't really have to mess with that right now, so we can just leave those.

    01:36 S1: But anyway, so 0.9, 0.16, 0.94 and so on, those will be our values. So what I'll do is, I'm gonna move this, it'll be off the screen, but you can leave yours on the screen, and I'm just coding in full screen. So I'm gonna put on a different screen for me, and we'll just take these values. So we'll scroll on down to the bottom, and just before "plot.show", we're gonna adjust the subplots. So we're gonna say "plt.subplots_adjust" and we're going to adjust... We'll do it in order. And so we have the left, and we chose to do 0... Well let's just do 0.09. And then we had the bottom, was equal to 0.16. Then we had right equal to 0.94, top was equal to 0.95, "wspace", we'll keep that at 0.20... Or actually 0.2, that's fine, we don't need the extra 0 there. And then "hspace" can also be equal to 0.2.

    02:44 S1: So let's go ahead and run that new version now. TSLA. And now we can see the default plot comes up, and it looks a little better as far as showing all of the information that we wanna see. We might... If you make the plot bigger out of the gate, you might want to adjust these sides, but it doesn't really matter at the moment. Okay, so we'll close that. And now what I want us to talk about is what if we wanted to change the timeframe here from 10 years to one day? Well if we change that, let's go ahead and also print the new URL. Let's say even run that real quick, and we'll go ahead and plot Tesla. But we're gonna see we're gonna get an error here. So we plot Tesla, and then we get this big giant nasty error, and it's "unconverted data remains". And we see 428. And what's happened is, let's look at the data now, we'll copy this and we'll paste this new data into here. And we'll see that no longer... Basically everything's the same, except for the fact that this is no longer a date. This is what's called "a Unix timestamp".

    03:56 S1: So we wanna... Maybe we're dealing with data that has a Unix timestamp. So how would we handle data that comes with Unix time instead? So what we'll do is we can close this. And now we need to create some sort of conversion for that. And luckily these timestamps dealing at least with a number is a lot easier than dealing with date data. We don't need a big fancy function like this. Although there is a big fancy function, and that'll work for us in the background. It's just we don't have to code it, it's already done for us. So let's say we're working with that data, okay, so this would be what we would use if... And I'm just gonna copy it. This would be what we would use if we were working with long term data, but let me just do Alt 3 here to comment it all out. And then what we'll do is we'll come down, and now paste... For the converters, we actually don't... We'll just not even use a converter for now, we'll just do this, and then we'll actually run the conversion on our own.

    05:00 S1: So now, after we've run this data, we're gonna apply a function to it and we're gonna say... Well first we need to create it. So we're gonna say "date_con" for date converter, and that's gonna be equal to "np" for NumPy ".vectorize". And we wanna vectorize... And we're gonna use datetime, so we'll have to import that. But for now we'll just do "dt.datetime.fromtimestamp". And so this will return a vectorization for something that will convert from a timestamp to a NumPy data conversion for us. And let's go ahead and go to the very top here, and go "import datetime as dt". Coming back down, this is where we're working here. So we've got the date conversion now and now we actually need to apply that. So after we've got date, now we've got the date conversion.

    05:55 S1: And now let's just re-valuate date. So we'll say date equals date conversion or date_conversion of that date, and then everything else can basically stay the same. And in fact, in the last tutorial we didn't fix this. You can always reference plt, and as long as you're modifying that whatever axis you're modifying at the time, it'll write on that axis, but we should've already converted this. So, let's change that to ax1.plotdate, and everything else should be fine. So, let's save and run that and see if we get away with this new format basically. So Tesla, we're pulling that information, up pops the graph and here it is. So this is date information using Unix time. It's got all these extra zeros here that we don't really need, but we'll just not worry about it. So this would be today, actually the market is open right now, so we're getting data for today. So, it's not actually even a full day here, but this is the stock price for Tesla right now.

    07:00 S1: So anyways, that is how you can go ahead and convert data to... Unix data to timestamps. Again, I just wanted to cover converting a visual date stamp as well as converting a Unix time. You're not really gonna see data in any other form. It's either gonna be a timestamp that you recognize, like a written time like June 12th, 2014, or it will be a Unix timestamp. So, depending on what kind of format you need to convert from, now you can. So anyways, let's go ahead and we'll close this, and let me close over here. Now, in the next tutorial, what we're gonna be talking about is customizing ticks and doing maybe some colors, and specifying the amount of ticks, and even removing some ticks. And ticks if you don't know, they're the markers on the edge of the graph that kind of denote new prices or whatever. So in our case it's displaying numbers for the price or the dates, and so on. So that's what we're gonna be talking about in the next tutorials, so stay tuned for that.
    00:01 Speaker 1: Hello, everybody, and welcome to the seventh part of Section Three with data visualization with matplotlib in Python. In this tutorial, what we're gonna be talking about is customizing the ticks and doing some coloring and all of that. So let's get started. So first of all, what we can do is we can set the label colors for axis. Now, at the moment we actually don't have axis labels. So, let's go ahead and add those. So, let's do it up here. So, you can't get away with something like this, like ax1.Ylabel, let's say. You can't get away with doing that, you have to use PLT. Now, what that ends up doing to us is we can only add these labels as we're working on the axis but as soon as we start working on another axis, if we do though... We try to add a Y label, it'll add it to that axis. Right now, we just have the one axis, so it's not gonna really be totally obvious yet but as we go on you'll see what I'm talking about. So let's go and add the label, and we'll call the Y label as price, and then we'll do a X label, and this one will be date.

    01:15 S1: Okay. So now that we have those we can change their colors. Now generally, what I do is you need to define the figure, you define the axis, and you maybe make the edits to the axis with labels, and then the same thing with a title if you wanna add titles per axis, this is where you would do it. And then I don't do anything until I get down to the axis here. And then I start making modifications and generally actually I would have multi-line modifications underneath the single line modifications like this, because as time goes on and you start editing axis, you can edit them basically wherever you want because you reference them by saying AX1 or AX5, or whatever. So you can get away like that but it can get messy really quick. So it's definitely a good idea to kind of block them all together. So, continuing on, we've got AX1 grid and then we'll add... Let's do ax1.Yaxis.label. And we could do something like "set color", and this will change the color of that label, and let's do M for magenta.

    02:24 S1: Then we'll do ax1.Xaxis.label.set_color, and then we can choose a different color. We'll do cyan. And so now, we can save and run this, and it's taking a while to pop up. There we go. And asks us what stock we wanna plot. Let's plot XOM for Exxon, and here's our Exxon plot. So now we can see there's price and there's different colors. So, by default they'd be black but because of our modifications they are purple and blue. So, we'll close this out now and I'll just move this over. And another thing that we might wanna be able to do is modify the ticks. So, with our prices there... We had a lot of prices along the side so let me just pull that up one more time. So we had all these marks and it basically it's going in tenths, but what if you didn't want to go in tenths? What if you just wanted less ticks? Well, we can modify ticks. We can modify exactly what shows up by doing something like this.

    03:28 S1: So we could say ax1.set and we can set X or Y and we're gonna do set_Yticks, and then in here you pass a list of the ticks that you're willing to use. So, we could do a... I forget what the prices there were so let's bring that up one more time. I think it was in the mid 80s though at the moment but let's look again. Right, so the lowest would be 83.5 and the highest would be maybe 84.5, something like that. Yeah, so 83.5 to 84.5. So let's close this and we'll set the ticks as 83.5, 84 and then 84.5. And in fact, let's go all the way to 85. Actually, no, no. We'll do 84.5 for now and then we'll show adding more and why you might do that. So, let's just plot the same one that we've been plotting, so Exxon. Right. So, here the graph starts at a reasonable amount and it goes up but we can see the graph is a little less volatile appearing, even though it's the same graph. But if we were to say zoom in on the graph as it did naturally, now it looks like, "Wow, really sharp changes". But the more that we do something like this to the graph... And let's keep going. Hopefully... I was trying to get a... Oops, oh my goodness. What have I done?

    [chuckle]

    04:54 S1: We can make the graph look really stable though, by doing something like this, usually. Right. So, you do something like this and it looks like, "Oh, there's not really that much volatility at all" but then you can make it look really volatile if you just make the height and the high and the low max. So anyway, but the other reason why you might do something like that is for a legend. So, say you wanna have a legend, you know it's gonna be up here. Well, you can add 10% and set the Y ticks. You can also set Y min and max. So you could do something like that as well to make sure that you're not covering over your legend. Although, I prefer legend with alpha and we'll be talking about that later.

    05:34 S1: So anyway, that's it for the customizing ticks and all that. In the next tutorial we're gonna be talking about how to add fills and alpha. So generally when you make a fill it's a solid color but we can add an alpha to it which is a measure of a opaqueness, which we'll be talking about next. So, thanks for watching and stay tuned for that.
    00:00 Speaker 1: Hello everybody and welcome to the eighth part of Section 3, all about customization for the data visualization module Matplotlib for Python. In this part we're gonna be talking about fills and alpha, so let's go ahead and get started. So first of all if you recall from the stack charts there was a sort of fill under the line, and under the line it was filled with color, so we can actually do that with any line we want. One problem with fill, though, is that it sometimes can cover up things that are important, so we add an alpha, and this is a measure of opaqueness. So a full alpha means you cannot see through it, but an alpha of zero is completely transparent, so the lower the alpha the more transparent something will be. So, that's that. Again, just like with the stack, remember how we had that problem with labels? Same thing is true here, so anything that's a poly element, like a polygon element with matplotlib, cannot be labeled, so we have to kind of do a pseudo-label if we wanna have labels. But that's okay. As you saw, pseudo labels are super easy to do, but anyway, let's go ahead and get started with some fills and some alpha.

    01:11 S1: So first of all, the fill, we can treat a fill... We can plot like this if we want and then do a fill, and that's one way we could retain our labels, but for now let's just comment out that plot completely, and let's do a simple fill. So to do a pretty simple fill you just do ax1.fill_between, and then you fill between, what? You have the X-value, so that's date, and then you've got the price that we wanna use, so we would say 'Close p', and then between what values, so we're gonna say 84, we're gonna fill between the price and 84. Actually let's do 83.5, cause that's the bottom of our graph, and that's all. So let's go ahead and just run that real quick, see what we get. Again, we'll do Exxon Mobil because of the way that we're filling, and the resulting chart is this. So you've got the line, and then below the line it's filled in with this nice blue, so that's a really simple fill, and some of the options that we can do once we fill, is something like, alpha, and we can say the alpha is 0.5 now, so we can do that. Let's save and run that. Again, we'll do Exxon, and now we've got the same stuff, only this is clearly a little more transparent, we've decreased the alpha, so to speak. So we close this.

    02:34 S1: Another thing you can do is you can add an edge color, so we could say edge color, and we can say that equals 'K' and 'K' is black, for anyone who doesn't know, 'K', it's kind of a weird one, but 'B' is actually blue, so black needed a color, [chuckle] and no one was using 'K' so that's why. So there we did that, and we could color it all kinds of colors, we could do maybe red would be a little more obvious then what that was. Yeah, so red you can definitely tell. Anyway, so those are some of the options you have. I'm gonna get rid of edge color because I don't really care that much about it, but we can leave the alpha if we want, it really doesn't matter there. So now what we can do is get a little more complex, but let's comment that out there, and now let's do... We'll keep this line here, and then what we're gonna do... Let's just delete this line. So we'll do an... Let's first plot this up and see the dates that we're working with here. So plot Exxon prices, rather. So let's say anything above 83.75, that'll be our division number, you'll understand what I mean here in a second, so let's close that. So 83.75 is the number we'll use.

    03:50 S1: So, let's, underneath this line, we'll add an ax1.fill_between and then we're gonna say 'Date', the 'Close p', for close price, and then we're gonna say, between that and 84.5, because that's the top where it can be, and then what we can do is we can add this little clause here and it can be where, and then we can issue a really simple equivalency thing here, so we can say, "Where close p is greater than or equal to," and we said what was it, 83.75. So we can fill this where this is the case, and where it needs to be equal to. So where close p is greater than or equal to 83.75, and then we're gonna say "Face color equals green". So this is anywhere the price is above 83.75 we're gonna say that's good, that's green, fancy that.

    04:50 S1: And then we'll do alpha equals 0.5, I don't know, something like that. So let's save and run that real quick, and again, we'll do Exxon, and we filled a little bit the wrong direction, so let me close this. We wanna have not 84.5, we wanted this to be 83.75, like it was in the other one, there we go. Exxon, there we go. So any time the price is above, basically this imaginary line here, we're calling that green. Now we wanna handle any time the price is below that line, what do we wanna do? Well, we can take, basically this exact line, so let's just take this line, copy it, come down, paste, and then we wanna fill between this and we can still keep 83.75 as the line, but now it's where close p is less than or equal to that, and then we'll do a red face color, that should do it. Exxon, we'll see. Right.

    06:00 S1: And now we see, okay we've got fills all the way up to this again, sort of imaginary line that we're calling something of use. Maybe we bought this company at 83.75 and so when it's below 83.75, we've lost money on the company and as it's above we've made all this money on the company and so on. So you could use this to visualize that but mostly what we're interested in is showing the fills and then options with the fills and things that you can do. You can get pretty darn fancy with your fills actually.

    06:32 S1: So that's it for the fills and alpha tutorial. In the next tutorial, we're gonna be talking about how you can add, remove and customize the chart spine. So, the spines are the edge of the graph basically. So anyways, that's what we will be talking about in the next tutorial, so stay tuned for that.