0:00 okay this is gonna be an introduction to
0:02 line plots with matplotlib and Matt plot
0:04 led is the primary graphics library for
0:07 Python it's a third-party library but
0:09 it'll be installed automatically if you
0:11 have installed a scientific platform
0:13 such as anaconda now there are many
0:14 other graphics libraries out there but
0:16 most of them are just wrappers around
0:17 matplotlib so in other words they use
0:20 matplotlib as the base case and
0:22 matplotlib allows the production of
0:24 publication quality graphics now if you
0:27 consider all of the elements of a graph
0:29 the axes the labels the plot area you
0:33 can just imagine that it may take many
0:35 lines of code to get some of your grass
0:37 looking just the way you want them now
0:39 this is going to be an introduction to
0:41 line plots and I'm gonna cover most of
0:43 the common elements in the graph so you
0:45 can get started so I'm gonna be working
0:47 with a jupiter notebook here and you're
0:48 gonna be able to download this notebook
0:50 from a link in the video the first thing
0:52 we're gonna need to do is to set up the
0:53 environment the only library I really
0:56 need here is matplotlib but I'm going to
0:57 import pandas and pandas data reader so
1:00 that I can work with some real data
1:06 now matplotlib is a huge library and it
1:09 has lots of modules within it the main
1:12 module you're gonna want to work with is
1:14 PI plot and so that's what I'm going to
1:16 import okay we'll run that cell okay and
1:19 then I'll go ahead and get some date I'm
1:20 gonna get some stock price data for
1:22 Google and Amazon and then we'll just
1:24 take a look at the first few rows of
1:27 that okay so I have data going back to
1:29 the beginning of 2019 here and with that
1:32 we're ready to go ahead and start
1:34 plotting so at the most basic level all
1:37 we need to do is call that import PLT
1:41 and then call plot so plot is the main
1:45 line plot method and then I'll just
1:48 reference what I want to plot okay so
1:51 you can see we get a very basic graph
1:53 alright another artifact we get is the
1:57 reference to the object we created we
2:00 can suppress this with a semicolon
2:02 okay if we want to plot the Amazon with
2:06 the Google
2:09 we just go ahead and add another line
2:11 okay and this gives you an example of
2:13 how the pipe lot module works
2:16 essentially what we're doing is we're
2:19 building these graphics in layers and so
2:22 Python always assumes that we are
2:25 talking about the current graph so
2:27 anything that I reference here is gonna
2:30 get added to this current graph alright
2:32 so we'll make a few other changes
2:34 because this is looking pretty boring
2:36 one of the first things we're going to
2:37 do is make the graphic larger so I'm
2:40 going to import another module from
2:43 matplotlib
2:45 okay this is going to allow us to set
2:47 certain parameters so I'll set the
2:50 figure size okay the parameters actually
2:52 live in a list like structure
2:57 okay so we'll see what that does okay so
2:59 we get a larger graphic if you want to
3:01 play around with the height and width we
3:04 can do that all right so at least now we
3:06 can see the dates clearly all right and
3:09 then let's add a few more features to
3:11 our graph okay I'm always gonna end with
3:14 this semicolon so I suppress that object
3:17 address all right so the next thing I'm
3:19 gonna do is add the legend and if I
3:24 don't give it any location it's gonna
3:27 show up in the upper right here all
3:29 right and I can change the location
3:31 there's lots of ways to do it I tend to
3:33 use the numeric system
3:35 all right so location 2 is upper left so
3:39 that's location 1 2 3 and 4 we can set
3:42 it in the middle so location 6 in the
3:45 middle there and I can get it on the
3:47 other side okay and then along the
3:50 horizontal axis we can Center it there
3:53 as well all right in the top or the
3:55 bottom so location 9 all right so I said
3:57 it as location 2
3:58 all right next thing we're gonna do is
3:59 add some lines in here so these things
4:02 are not just kind of floating in space
4:04 so I'm going to call the grid and I'm
4:07 gonna set it as true ok so by default we
4:10 just get this sort of grayscale line you
4:13 can control it a little bit so we can do
4:15 something like set the color you can set
4:17 the color as anything you want there are
4:19 seven color codes CMYK GRB all right so
4:24 I'll just set it as black okay so that
4:26 basically makes the grids take the front
4:31 stage here all right so they're sort of
4:33 featured more prominently than any of
4:35 the data we're graphing I'm going to
4:37 change their line style
4:39 and there are a few line styles so we
4:42 can do a dashed line and dash dot a dot
4:45 dot is what I'm about to do I'm paying
4:47 so then that I guess makes the data
4:49 front and center again what would your
4:51 graph be without a title so we're gonna
4:53 add a title
4:57 so just like that we have a title we
4:59 could if we wanted to set the font we
5:02 could set a different font we can set a
5:04 different font size okay there's lots of
5:06 options I'm not gonna be able to go
5:07 through here in a quick video okay if we
5:10 want to label the axes I can set X label
5:15 or Y label here I'll just set the X and
5:18 we'll get date down there okay if we
5:23 want to play around with the limits of
5:26 our axes either X or Y we can set limits
5:30 so I will do this quickly just to
5:32 demonstrate it for the y axis
5:35 okay the property is either x limb or Y
5:37 limb we need to tell it where to start
5:39 and where to stop so we'll go down to
5:41 500 and up to 2500 just so we get a
5:45 different axes here it's not probably
5:47 gonna look very good okay so there it is
5:49 it kind of centers the data there all
5:51 right if we don't want to see all of
5:53 these ticks all right we can set a limit
5:56 so Y ticks X ticks all right and then
6:00 it's going to take a list like structure
6:02 so I can set a variable pass this into X
6:05 ticks and so I can just start at 500 and
6:09 go every 500
6:14 all right a couple more things if we had
6:17 a line style that we can use for the
6:20 grids we must have one that we can use
6:23 on the actual data and we do all right
6:25 so I will play with the data just a
6:28 little bit and I'll make it read all
6:31 right so this sort of a shortcut method
6:34 of setting the color and the line style
6:36 all right I can break them out and
6:38 specify it if I want I can use OHS
6:42 alright so I get a bigger dot there
6:44 alright and so as I mentioned there are
6:47 seven colors you can use with the
6:50 shortcut method CMYK RGB if you want
6:55 some other color you can specify a color
6:58 and you can actually use any system
7:02 alright so I can use blue okay Python
7:05 has a number of built-in colors so
7:08 something like Alice blue would work
7:10 alright I can use a hexadecimal color so
7:13 okay so there's a hex code I can use RGB
7:19 using three coordinates between zero and
7:23 one that indicate how close to black I
7:25 want to get okay the most common thing
7:27 you probably do is just let matplotlib
7:29 decide what the color will be all right
7:32 so that's what I did when I first
7:33 started I just let matplotlib decide
7:35 what the default should be okay and if
7:38 we want to get something that's a little
7:40 bit awed mented here we can import a
7:43 different theme to plot
7:46 okay so there's a number of styles we
7:49 can use I'm going to try ggplot alright
7:53 so there's a common wrapper library that
7:55 you can also install and work with if
7:58 you don't want to work with matplotlib
8:00 okay we see it looks pretty much the
8:02 same as the default so we'll get
8:05 something that looks a little bit
8:06 different here and yeah if you're
8:07 wondering and you're familiar with our
8:09 that is the same ggplot that is the sort
8:13 of de facto standard in our alright let
8:15 me try a different style here okay so
8:18 there's the 538 style and as I mentioned
8:22 at the beginning a lot of times getting
8:23 your graphics to look just right takes
8:25 lots of lines of code alright we haven't
8:27 really done that much but you can see
8:29 that oh there are quite a few lines of
8:31 code here just to get a pretty simple
8:33 graph out there alright so that's why
8:35 maybe you should take a look at some of
8:37 the themes and see if any of those will
8:40 at least get you to a baseline that you
8:43 can work from okay and then down below
8:45 here I have included a couple of links
8:48 out to the documentation the
8:50 documentation is very good and extensive
8:53 so you can spend a lot of time out there
8:55 I just pointed at the common things that
8:58 you might want to do like look at the
9:00 different themes okay so we can scroll
9:05 through and we can set one of these to
9:07 be used instead of the default all right
9:10 and there are quite a few alright and
9:13 the interesting thing here is down at
9:15 the bottom they actually show you the
9:17 code that they use to generate these
9:19 this graphic up above alright so if you
9:21 want to get a little bit more exposure
9:23 you can take a look at this and even try
9:26 that code okay and then just I showed
9:28 you one quick line marker or two I guess
9:31 I showed you the dots and the and the
9:33 O's here's the extensive listing of all
9:36 the different markers you can use
9:38 alright so that would be a line style
9:40 okay so I hope that can get you started
9:42 with line plots and matplotlib