Loading

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

Study Reminders
Support
Text Version

Set your study reminders

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

    -

    7am

    +

    Tuesday

    -

    7am

    +

    Wednesday

    -

    7am

    +

    Thursday

    -

    7am

    +

    Friday

    -

    7am

    +

    Saturday

    -

    7am

    +

    Sunday

    -

    7am

    +

Creating Customization Options

00:01 Speaker 1: What is going on everyone? Welcome to Part 10 of our Data Visualization with Python Matplotlib tutorial series. In this part, what we're gonna be talking about is the creation of open-high-low-close candlestick charts. So open-high-low-close is generally the data that we get. Open-high-low-close pertains to the bar of data you get. So generally, with stocks, I mean, you have many stocks traded a second even. So when you get data, especially if it's free data, a lot of times, the granularity is changed to maybe you've got one-minute data. And in one-minute data, you basically are given usually the open price, high price, low, and a close. The open is the first price in that one-minute bar. The high is the highest price in that one-minute bar. The low, the lowest, and then the close is the last price. With that, people make these charts called 'candlestick charts', which put all that data together and make a pretty nice visual representation of it. So that's what we're gonna be covering in this tutorial, is the creation of open-high-low-close graphs, which is a pretty customized graph type. So let's get started.

01:08 S1: First of all, with this, what we wanna go ahead and do is we're gonna modify our ticker because we're gonna end up changing the tickers in a little bit. So first, we're gonna come up to the very top of our script. And along with all of our other matplotlib imports, we're gonna import matplotlib.ticker as M ticker. Again, M ticker is just something that a lot of people do as standards. Also, in order to plot open-high-low-close method, tell matplotlib that that's what we're about to throw at it, so it's a very custom chart. So we're gonna say from matplotlib.finance, which is where a bunch of the finance functions for matplotlib are, we're gonna import the candlestick_openhighlowclose. There's also another one that's closehighlowopen or something like that, the other order, but we'll use OHLC since that's what really most people call them anyways. So that's what we're gonna go ahead and do.

02:13 S1: Rolling on down, the next thing that we'll do is basically after we get to... Well, what we have to do first, actually, is we need to... Let's go ahead and pick a longer timeframe. We'll get out of this one-day data stuff. So we'll change that to... Not three days, let's do three months. And then our converter, we basically wanna use this line and not these lines, so I'm gonna comment that out. And actually, let's comment this out. And then we're gonna uncomment this line, so we're given back our old values there.

02:54 S1: Now what we wanna go ahead and do is we'll come down here. And after we have this data, when you feed the data to the open-high-low-close, you basically feed it the array of date open-high-low-close, and you can even stuff in volume there. So that's what we're gonna go ahead and do first, and we're just gonna run through date. We're just gonna do this in a slightly sloppy way, but that's okay. What we're gonna do is we're gonna say, first of all, X=0. We can use mapping for this, but mapping can be really confusing. We'll cover an example of mapping later on. It'll be a little slightly more simple example anyways. So X=o, and we're gonna say Y is equal to the length of dates. So how many date parameter or variables or values do we have, that's what Y will be. And then we're gonna say New R for new... I suppose it's an array, but maybe we should call it something else, new list. There's no arrays in Python unless you have NumPy anyway.

03:57 S1: New list equals that. And then we're gonna say while X is less than Y, what do we wanna do? Well we're gonna say append line is equal to the date X, open PX, high PX open high low PX, close PX, and then volume X. So that's our append line equals that. And then we're gonna say new list equals or actually, new list.append line. And then we'll say X+=1. So that'll just run through this wow statement here. And whenever that's all done, it'll be populated. And then what we can do is instead of plotting this close price, we can instead, plot the candlestick. And also, let's go ahead and comment these out. So again, it's all three. And in fact, I think this code is in the tutorial, so I'm gonna just remove it where the sample code is saved. So no sense in having a bunch of commented out code honestly, so we'll get rid of that, too.

05:20 S1: So once we've got the data, the next thing that we're gonna go ahead and do is we wanna call candlestick_OHLC, and then you give the axis that you wanna plot it on, which is AX1. And then the open-high-low-close data, which is new_list. And then you can... Basically, that can be all you do. So let's go ahead and just run that one real quick and make sure everything is hunky-dory. Plotting X on... Right. And so there's our data for now. Because we're doing longer data, just recently, the price has come into these numbers, so that's kinda what we're looking at here. We'll get rid of this tick stuff next. But this is the data. And so we can see these bars, and this is what are open-high-low-close bars.

06:04 S1: Now, what we can do next is generally, people do open-high-low-close, and an up is a positive movement is usually marked as green, and a negative movement is marked as red. Also you can modify the width, so we'll do width equals.6. You'll see it might a little fatter. And then we have these color up equal something, and then color down equals something. Up until this point, we've been just using letters, like R, G, and then color down could be R for red, and so we can look at that real quick. Sure enough, they got a little fatter and the upward movements are green and then red is denoted as loss. There's some more loss here and so on. But we can also do colours in hexadecimal form. So version of green would be the 418049, so 418049, use this hash, the pound. And then again for color down, pound, and we'll do ff1717 for red. Save and run that. XOM again. And that's just a slightly different version, but it's hexadecimals anyways. So it's a little lighter of a green or more pastel-ly green, I suppose, but nonetheless green.

07:24 S1: So now that we've done that, the next thing is you may notice that our ticker down here is no longer in proper date format, and that's why we needed to do mticker up above. So now, let's get rid of basically all that code as well. And we can keep the rotation, but the next thing that we wanna do is come down here and do an ax1.xaxis.set_major_locator. And first, we'll say mticker.maxNLocator. And then this will be 10. Note the caps, capital N, capital L there. And then we'll also set the major formatter, so ax1.xaxis.set_major_formatter, and this is mDate.formatter, and then here we put whatever the format of the date we want to have, and that'll just be %year-%m-%d. And then we'll go ahead and do the typical plt.y label. This will be price, plt.x label. Can be date, although actually, we don't even need date. It's pretty obvious these are dates.

08:46 S1: And then let's go ahead and save and run that. XOM again. And then sure enough, we can see very clearly now, we can see very clearly that these are the dates, the price, and we can see the dates. That's great. We can zoom in, all that. One thing to note is of course, like before when we had dates, we can zoom in and in and in and in, and we got timestamps, but now you can see just repeats that date. If we wanted to show more, we could. We could do percent hours and colon percent minutes and percent seconds if we wanted, but because this is daily data, we don't even have that data anyways.

09:21 S1: So that's it for this tutorial with the open-high-low-close. We're gonna keep building on this kind of chart type, and we'll be adding more subplots and stuff like that. But in the next tutorial, what we're gonna be talking about is styles, and it's just an easier way for us to modify how our graph looks. We can use some of the presets, but we can also make our own styles for the customization options that we like. So that's what you guys have to look forward to, so stay tuned for that. Thank you for watching.

00:00 Speaker 1: Hello everybody, and welcome to part 11, Customizing Matplotlib which is for Data Visualization in Python. In this part, what we're gonna be talking about is the Matplotlib styles. If you happened to be following along, and you've got maybe an older version of Matplotlib, you might not have styles. So you'll wanna go ahead and do a Pip install Matplotlib, or a Pip install upgrade Matplotlib, update if you're using Sudo app to get it, and so on. So make sure your Matplotlib is up to date and let's go ahead and get started.

00:32 S1: First, we're gonna go ahead and do from Matplotlib, import style. Now, what this brings to us is the ability to use these kind of style sheets, that's what I wanna call them, style sheets. They're just called styles, but they remind me of style sheets, like for CSS, Cascading Style Sheets for HTML, where you define a bunch of values, and the idea is the same as the idea for style sheets. You can have a website without a style sheet and style everything as you go, and that's totally fine. But it's a lot easier to have one central style sheet for your entire website, and then when you go to change something or make a new page, you don't have to go through and code all those styles in again. Same thing with the graph. As you could see before, we had this massive, now it's deleted because we got rid of it. But when we were styling all of those elements in our axes, remember it was eight lines just for axes one, and it really wasn't that much customization actually. So with styles, we can save ourselves a lot of headache, especially if we like to apply the same styles every time, even if maybe it's just five, 10 lines, having your own style makes your life a little easier.

01:48 S1: So we can import style. And first of all, to use a style, we can go style.use and then you specify the name. Now, you're not expected to know all the names, but I'll show you guys in a minute, all of them, but ggplot is one of them. And so we can use that, we can save and run this now, and we will just plot Exxon Mobil again, and up pops this chart. As you can see, the things are really a lot prettier right out of the gate: The graph has its own little background, and the grid is actually a white line on the colored background, and the font looks to be a little smaller, and it's a little lighter, it's not solid black anymore. Just a really quick and nice change. So some of the other stuff that we could possibly do is we can look at... There's another one that's just dark_background, you can save and run that, and we'll bring that one up. And so it changes everything. So to have a dark background, if you recall, if your text is black, then to change your background to black means you have to change all your text to white, all your spines to white. Your grid needs to be white and so on. So, having these styles, it's like, bam, everything's done. You don't have to actually do much at all.

03:08 S1: I'm trying to think of some other ones. Another one is, there's a BMH. And then we can plot Exxon here. Okay. So I don't really care much for this one, but on some charts it makes a lot of sense. And then finally, there's one that is 538. This one is actually a pretty nice one, it's probably my favorite one of all of them, and ggplot, I don't know, one of those two. So here's 538, and as you can see here, what they've done is actually, there's no spines at all, but then you have your labels and all that. And the labels are pretty big, and the grid is fairly big and solid and all this kind of stuff. So, just nice to be able to import these really simply. Now, to figure out a list of the styles available, you can just print and do plt.style.available and that will print all the available styles to your console, which is here. Oops, there you go. So, dark background, BMH, ggplot, grayscale. Grayscale is just exactly what it sounds like, it's grayscale. So it's only grey elements basically, or white to black.

04:18 S1: So the next thing I'd like us to go ahead and do is I'd like to show you guys where these styles are hidden. So when we possibly want to make our own style, where would we go and all that. So the path is just in your Python directory, so if you're on Windows, you can follow along. Otherwise, you'll have to find your directory, wherever Matplotlib is stored. If you do not know, here's how you can find out however. You can open up a command prompt, you could also type it into IDLE too, but we'll just open it up here. And I'm gonna run C: /. You could probably just type "python". And in fact, let's go ahead and do it in our script that we're writing since that might confuse people. But what we can do is, we've imported matplotlib.pyplot as PLT, so we should be able to get by doing printplt.underscore underscore file underscore underscore. This is just one of those initial methods that comes with pretty much every module, and it'll tell you where that module is stored. So we can see, for me, this Pyplot module is stored under this directory, but we can see the parent directory is Matplotlib, so that's where we wanna head. So that's how you can figure out where the directories are. Now, let's go ahead and head there.

05:39 S1: So I'm in the Python34 directory, then I'll go into lib there, and site packages, and we're looking for Matplotlib. So now we're in the Matplotlib directory. And you can see here, there is a directory for style, but there's nothing there. So that's not actually what we're looking for, we're looking for an MPL data and then style lib, and then here are the style sheets. So now what we can do is say we liked 538. We could right click and let's open it with Notepad++. If you don't have Notepad++, you can open it with regular Notepad if you want, but I highly recommend Notepad++. So once you have that up, here we go. You can see up here the original author has put his information there, so we know where it's from and you can see here how this is working. Okay. We can see that you define various variables. So, axes.facecolor. He's identifying the face color. This is the color cycle, so if you have lines, it's only gonna cycle through one, two, three, four, five colors before it repeats itself.

06:43 S1: So you may wanna add more colors or something there, but this should do just fine. And anyway, all the code basically for pretty much all the stuff of this kind of style. But you can see even this is 40 lines, it's got some white space, but it's 40 lines. Probably like 30 without the white space, but even then, that's 30 lines of information and because he's using axes here, it's affecting all axes anyways, so you don't have to even worry about doing each axes at a time.

07:14 S1: So in the next tutorial, we're just gonna cover making our own little simple style sheet. It won't be anything too out, crazy, but we'll make our own, just so you can kind of get comfortable with it. But you can probably kind of deduce from this information how to do it, but that's what we're gonna do in the next tutorial. So stay tuned for that. Thanks for watching.
00:00 Speaker 1: What is going on everyone? And welcome to Part 12 of Section 3 of Matplotlib and Data Visualization with Python. In this tutorial, what we're gonna be talking about is creating our own styles. So we can use some of the premade styles and we can maybe even edit the premade styles if we wanted a little bit, or we can create our own style completely. And the good thing about a style is you can go with the Matplotlib defaults and just change a few things that you want, so you don't really have to change literally everything or all the colors or whatever, you can go with just modifying from the defaults exactly what you want. So if you find yourself typically always making the font size a little bigger and maybe getting rid of the edge spines or something like that, if you find yourself doing that every single time, then you might as well just make yourself a little simple style that makes the font bigger, gets rid of those side spines, and that'll be that.

00:55 S1: So in this tutorial, that's what we're gonna talk about. We're just gonna make our own little style that just does a few things. And instead of using this open high-low close graph, we're just gonna make another quick graph that just has a few lines so we can kinda see the change. So you should keep this code because we're gonna come back to this code eventually, so don't just delete it or something like that, keep it around. But I'm gonna go ahead and remove mine, but I have each video saved as a tutorial, so I have the back up code. So make sure you save your back up code.

01:27 S1: So anyways, I'm gonna delete that and then we're just gonna go through. And this will be kind of a good run through of Matplotlib basics anyways. And so we're gonna go import matplotlib.pyplot as plt from Matplotlib import style, and then finally, we're gonna import random 'cause we're gonna use it to draw random lines. Then, we're gonna style.use, and we'll use something. For now, we can just use GG plot. And in fact, let's not use this style. We'll use our own style in a minute, but the style I'll use that. And then what we're gonna say is we'll say, "Four label in range of eight." What do we wanna do? We're gonna say, "X is an empty set, and Y is an empty... " Actually, these are empty lists, rather. And then we're gonna say, "Four I in range of one to 10." What do we wanna do? We're gonna say, "The Ys are equal to random.rand range." It'll just be a random number basically from zero to 15. The X will just equal the I, so it'll just be one, two, three, four, five, six, seven, eight, nine, 10. And then we'll just say, "X.append the Xs, Y.append the Ys." And then once we're done with that four loop, we'll plot it so it'll be plt.plot XY, and then we'll say, "Label equals label," so that's just gonna be the number basically, label equals label. And then this means we're gonna have eight lines basically, okay? So we do that. And then finally at the very end, we'll plt.legend, and then we'll just do plt.show.

03:07 S1: Okay, so nice simple code there. Let's save and run that. And up pops this graph here. So not really the prettiest graph in the world, but we do have some lines. We see that we've got a lot of numbers. And the other thing that we can see right out of the gate is the first repetition of color. So like I was saying before, when you have that color cycle, Matplotlib we can see has six colors before it starts repeating. So 0 and 7 are actually the exact same color. So if you're trying to plot something that has a lot of different kinds of labels, let's say, or different datasets. Obviously, this is actually really messy, but there're sometimes where you might actually have a cleaner looking graph that has seven different datasets that need seven different labels, and so you need to have a way to handle for that. Anyway, so keep that in mind.

03:54 S1: So we're gonna go ahead and close this now, and let's just make our own style. So to do that, we come over here, and this is where mine is. Again, we showed how to get to this directory before by importing Matplotlib and then print Matplotlib. Actually, I think we did pyplot, and we did print plt.file, and that's how you can figure out where it is. But we already covered that, so I'm not gonna do it again. And so here's all the styles that we currently have, and we can just make another one. So let's just copy this one, paste, and we'll call this one, "Our style." And then we'll edit it with Notepad++, let me bring it down here, and here we go. So let's just clear out this entire style, and then we can start writing whatever we want.

04:40 S1: So first of all, we can make the line widths a little bigger. So initially, the line widths on that graph are just so skinny that I don't know, it just kinda bothers me sometimes. So we'll do lines.line width. And we can make these two. Then, we notice the other problem was those colors, so we can say something like this, like "Axes.color_cycle," and this is the colors that we're gonna cycle through. And these are hex color codes, but you don't have to have the pound sign there. Now, to figure out colors to use, the easiest thing you can do is just Google "hex color code," and you can find yourself on a website like this. This is the one I always end up using, and you can just start picking colors, okay? So you can start picking a greenish color would be this, and then you could pick more of a yellowy color like this. It's kind of an ugly yellow, but for purposes of the tutorial, that's okay. And then we'll do a reddish one. Let's do this light blue. And you can just pick your own colors, too. You can kinda go with whatever it is you want. There's a dark blue. I don't remember if we got a real dark green or not, but let's go ahead and grab a dark green. Okay, so that's six. And then we can get a brighter pinkish color. So that'd be seven.

06:00 S1: And then let's just pick basically one more. I think I like a color that we haven't got. I don't think we have a good orange yet. We'll just go with that. Okay, so now we've got, what is this, eight colors to cycle through. Obviously, you could keep going. If you find yourself plotting graphs with 30 different lines or something, go ahead, add 30 different custom colors. Now, the next thing is the axes face color and then the figure's face color. A lot of times, this looks good if they're identical. So what we'll do is we'll have axes.facecolor: And we'll make this an F2, F1, F1. And just for the record, that is this color, right? It's almost white, but not really. And then we're gonna do the exact same thing with figure.face color, and then we'll just have that be the exact same color. And this needs to be figure.

06:54 S1: And then we'll do axes.labelsize. We'll call these large labels axes.grid. We'll make that a true. We'll do grid.linewidth. 0.25 is good. Grid.linestyle. Let's just make it a dash there. And then we should probably change the color to grid.color, C9, C9, C9. And then font size, let's make that a 12. And let's get rid of those major and minor ticks. So xtic.major.size: Zero. And then let's just copy this line, so we're just gonna make a few edits as we go. So then paste, paste, paste. Instead of major here, minor. And then again, minor. Instead of x tick, this would be y and y. So you have x tick major, x tick minor, y tick major, y tick minor.

07:50 S1: Okay. So we've made a style, let's go ahead and save that. That's called "our style". Let's move this over. And the style.use, we'll use our style. So, now let's save and run that. Plot it up, and there you go. We've got our own style now. So as you can see, we've changed quite a few things. The labels are a little bigger, the colors are a little prettier, and the grid is... Maybe we would want a slightly darker grid, possibly. We could also make the grid a light, like a white or something like that. I believe F, F, F or something is a solid white. Let me try that. So grid color. What if we did one, two, three, four, six, yeah. So good, save that. And then so maybe a white grid would look good. Nope, I can't even see it. Okay. [chuckle] Maybe that was an invalid color though, actually.

08:38 S1: Anyway, you can kinda play with that if you want, but there's an example of how you can kinda write your own style and then maybe get rid of these x or the spines. I forgot to do that, but that's okay, we're kinda running long anyways. But you can get rid of the spines if you wanted or you can do all kinds of stuff. But anyway, this is already... It took like two minutes to make an already pretty substantially better styling than the default Matplotlib. So there you go.

09:04 S1: Okay, so that's it with styles. In the next tutorial, what we're gonna be talking about is creating live graphs, so graphs that update live as the data that they're reading is modified. So like I was saying before, when you have a graph, once you've plot it, you can't change that graph. Well, you can if you're using the animation functionality. So that's what we're gonna be talking about in the next tutorial, so stay tuned for that.