Coding

Python – Matplotlib Tutorial for Beginners

  • 00:00:01 welcome to this video another video
  • 00:00:04 about the Python data analysis basics in
  • 00:00:08 this video we will have a look at net
  • 00:00:11 plot lip net plot lip is one of the most
  • 00:00:14 popular libraries for Python when it
  • 00:00:17 comes to data visualization so to
  • 00:00:19 creating charts in the end so let's get
  • 00:00:22 started with this library in this video
  • 00:00:28 to get started you need Python installed
  • 00:00:30 on your machine you need a tubular
  • 00:00:32 notebook and you need matplotlib
  • 00:00:34 installed in case this is totally new
  • 00:00:37 for you have a look at this video right
  • 00:00:38 here I'll dive into the basic
  • 00:00:41 installation steps with that being set
  • 00:00:44 up we can now start and how do we start
  • 00:00:46 well by importing matplotlib to our
  • 00:00:49 project right here for that I'll simply
  • 00:00:53 type import and now mat plot lip like
  • 00:00:58 that but now we have to add dot PI plot
  • 00:01:02 right here PI plot is basically a sub
  • 00:01:05 module of matplotlib and it allows us to
  • 00:01:09 conveniently create charts to plot
  • 00:01:11 charts basically in the end for that we
  • 00:01:15 import PI plot as PLT that's the most
  • 00:01:18 common way how we use it and then we
  • 00:01:21 have to add a second line this is
  • 00:01:23 required if you use a Python free
  • 00:01:25 notebook which is the case for us right
  • 00:01:28 here as you can see right here python
  • 00:01:30 free because we want to display the
  • 00:01:32 charts we create in line in our notebook
  • 00:01:35 and not in a separate window
  • 00:01:36 therefore you simply have to add the
  • 00:01:39 percentage sign right here and now net
  • 00:01:41 plot lip oops
  • 00:01:44 plot lip space in line this is it if you
  • 00:01:49 know hit shift and enter we are ready to
  • 00:01:52 get started with matplotlib now the
  • 00:01:54 focus of this video is on the very
  • 00:01:57 basics of matplotlib therefore I don't
  • 00:01:59 want to use any external data I just
  • 00:02:02 quickly enter some hard-coded real data
  • 00:02:05 though right here the data I prepared is
  • 00:02:08 about stock prices not really
  • 00:02:10 spectacular I would say but it's enough
  • 00:02:12 for our purposes so let's add the Apple
  • 00:02:15 price right here maybe like this equals
  • 00:02:19 and now create a list right here and now
  • 00:02:21 I have to look up the values so I will
  • 00:02:23 forward the video a bit to let you type
  • 00:02:25 the numbers on your own so that's the
  • 00:02:28 first thing these are the stock prices
  • 00:02:30 of the Apple stock and I took the prices
  • 00:02:33 all-round about mid November of the
  • 00:02:36 corresponding year we'll have a look at
  • 00:02:37 the years in a few seconds and now we
  • 00:02:39 need a second
  • 00:02:41 range and I think the good choice would
  • 00:02:44 be the Microsoft price let's call it ms
  • 00:02:46 price right here now same logic I'll
  • 00:02:48 create a list and three values in and
  • 00:02:51 fast forward the video so that's it with
  • 00:02:54 that we have our two prices right here
  • 00:02:56 and now we need the years also know so
  • 00:02:59 let's call this variable year right here
  • 00:03:01 and the prices are from 2014 2015 2016
  • 00:03:06 17 and 2018 as I said all prices are
  • 00:03:11 taken round about mid-november but it's
  • 00:03:14 not the core focus of this video now
  • 00:03:16 with that we can hit shift and enter
  • 00:03:18 once again and we got our data the thing
  • 00:03:21 now is that we want to visualize that
  • 00:03:22 information right here and the easiest
  • 00:03:24 way to do so is to simply create a
  • 00:03:27 simple line chart we can do this by
  • 00:03:30 simply typing PLT you remember we
  • 00:03:33 imported matplotlib Zoar the pie plot
  • 00:03:35 sub-module as peel team so PLT dot plot
  • 00:03:40 this simply means that we want to use
  • 00:03:42 this SAP module to create a line chart
  • 00:03:46 that's basically what this is telling us
  • 00:03:48 now for this line chart we need data and
  • 00:03:51 we define the data in all variables up
  • 00:03:54 here so if we now simply open the
  • 00:03:57 brackets right here and now enter the
  • 00:03:59 value for our x-axis that's the first
  • 00:04:02 value right here which could be the
  • 00:04:05 years in our case so this one right here
  • 00:04:06 so we simply type here like that and now
  • 00:04:10 we enter a values for our y-axis and
  • 00:04:12 these could be right here the value
  • 00:04:14 stored in our Apple price variable so
  • 00:04:17 let's type at paw APL underscore price
  • 00:04:21 like that now with that we told pie plot
  • 00:04:24 that we want to create a line chart with
  • 00:04:26 this information inside of it now we
  • 00:04:29 have to tell pie plot also that we want
  • 00:04:30 to show this chart now now I set the
  • 00:04:33 command already we simply type ELT shown
  • 00:04:37 and not two brackets and if we now hit
  • 00:04:39 shift and enter boom here we are here we
  • 00:04:42 created our line chart if you watch the
  • 00:04:45 first video of the series it's not
  • 00:04:46 totally new for Europe but as you could
  • 00:04:48 see this is just as simple as it is to
  • 00:04:51 create a basic line chart in Python
  • 00:04:54 now the formatting
  • 00:04:55 the years is not really good down here
  • 00:04:56 as you can see it with this decimal
  • 00:04:58 numbers after the actual value we'll
  • 00:05:01 have a look at that in a few seconds so
  • 00:05:02 please ignore that for now but the thing
  • 00:05:04 is that by just creating the chart we
  • 00:05:07 are not done yet
  • 00:05:08 because we could say that we need labels
  • 00:05:10 maybe for this chart always something
  • 00:05:13 you want to add here to do that that's
  • 00:05:15 also the reason why show has to be added
  • 00:05:17 right here separately because basically
  • 00:05:19 we can now add additional things that
  • 00:05:21 you want to have in our chart we can
  • 00:05:23 have an impact on the formatting for
  • 00:05:25 example so you can remember that as
  • 00:05:27 basically telling puffin that we want to
  • 00:05:30 create a chart and defining all our
  • 00:05:32 parameters all our things that should be
  • 00:05:34 displayed and as a last step we tell –
  • 00:05:36 and now please show our final chart now
  • 00:05:39 I talked about the labels so we can
  • 00:05:42 simply add a label by typing PLT dot X
  • 00:05:45 label again pretty simple now we could
  • 00:05:48 give our X label a name I think a good
  • 00:05:51 name would be here for example for the X
  • 00:05:52 label and now we plot our Y label also
  • 00:05:56 and a good name right here could be
  • 00:05:57 maybe stock price something like that
  • 00:06:00 and don't forget these single quotation
  • 00:06:03 marks as I just did it now if that if we
  • 00:06:06 hit shift and enter once again you can
  • 00:06:08 see that we added the stock price right
  • 00:06:10 here on y axis and we edit a year on the
  • 00:06:13 x axis now we don't have a single stock
  • 00:06:17 price only we also got our Microsoft
  • 00:06:19 stock price right here now for that we
  • 00:06:21 basically have two options how we can
  • 00:06:23 achieve this one could be to again type
  • 00:06:26 PLT plot now again use our year variable
  • 00:06:30 but now use the NS price right here if
  • 00:06:33 we do that and hit shift and enter you
  • 00:06:35 can see that we also added our Microsoft
  • 00:06:39 stock in our chart and automatically
  • 00:06:41 change the color so this is one way how
  • 00:06:43 we can display these two charts right
  • 00:06:45 here
  • 00:06:46 we could also change the way our let's
  • 00:06:49 say our Microsoft chart is displayed we
  • 00:06:51 could quickly change this from a normal
  • 00:06:53 line chart to a scattered line chart for
  • 00:06:55 that you simply change plot right here
  • 00:06:57 to scatter like that if you know it
  • 00:07:00 shift enter you can see that D well
  • 00:07:02 Microsoft price is now displayed as a
  • 00:07:05 scatter chart so this is one way how we
  • 00:07:07 can achieve
  • 00:07:09 right here but let's say we want to have
  • 00:07:11 we don't want to have to scatter charge
  • 00:07:13 right here but we want to have this plot
  • 00:07:15 style for both our stock prices if you
  • 00:07:18 want that we can get rid of this second
  • 00:07:20 line right here now simply go back to
  • 00:07:23 our first code add a comma and maybe hit
  • 00:07:26 enter to add it in a second line and now
  • 00:07:29 type here once again
  • 00:07:30 sort of value for our x-axis and now add
  • 00:07:33 DNS price right here if we do that and
  • 00:07:37 hit shift and enter you can see that we
  • 00:07:39 again display both our stock prices but
  • 00:07:42 now in one single chart and included in
  • 00:07:45 this single code right here now could
  • 00:07:47 still say that you want to format these
  • 00:07:49 two line charts a bit differently so
  • 00:07:52 this means you want to have an impact on
  • 00:07:53 to the actual line style you can already
  • 00:07:56 see for example that we have two
  • 00:07:58 different colors here by default and
  • 00:07:59 let's say you don't like these colors
  • 00:08:00 let's say you want Apple to be I don't
  • 00:08:02 know maybe black and you want Microsoft
  • 00:08:04 to be something like green so for that
  • 00:08:06 you simply go right here back to the
  • 00:08:09 code and after a comma you add signa
  • 00:08:11 quotation marks again and another common
  • 00:08:13 because this code right here basically
  • 00:08:15 refers to this part of the chart so it's
  • 00:08:18 kind of a connected part you could say
  • 00:08:20 and if you want to have a black line for
  • 00:08:23 the Apple price you can simply type K
  • 00:08:25 right here if you do that and hit shift
  • 00:08:27 and enter you can see that we now change
  • 00:08:29 the line color more information on the
  • 00:08:32 different colors available can be found
  • 00:08:34 in the link in the video description by
  • 00:08:35 the way now we can also change color for
  • 00:08:39 the Microsoft price of course it's the
  • 00:08:41 same logic we simply add a comma right
  • 00:08:42 here now it's single quotation marks and
  • 00:08:44 now type G for green I would assume so
  • 00:08:48 let's add shift and enter and as you can
  • 00:08:49 see we now have that green line style
  • 00:08:51 right here besides the actual color of
  • 00:08:55 the line we can also work on the line
  • 00:08:57 style itself for that you will simply
  • 00:08:59 add the line style like this for example
  • 00:09:02 if you can't hit shift and enter now you
  • 00:09:05 can see that we have this kind of dashed
  • 00:09:07 line style right here there are also
  • 00:09:09 other line styles available for example
  • 00:09:11 this one right here if you enter that :
  • 00:09:13 then you have this well lighter – as I
  • 00:09:17 would call them or if you add all right
  • 00:09:20 here like that then you got this
  • 00:09:23 scattered style once again so that's
  • 00:09:25 another way how you can have an impact
  • 00:09:26 on treeline style more information about
  • 00:09:29 the line style and the different line
  • 00:09:31 properties can be found here as I said
  • 00:09:33 you can also find a link in the video
  • 00:09:35 description but for example these are
  • 00:09:37 some line Styles we applied right here
  • 00:09:40 and this one for example but I don't
  • 00:09:43 want to dive too deep into these as you
  • 00:09:44 can see you have lots of things that you
  • 00:09:46 can customize right here so let's go
  • 00:09:48 back to our Jupiter notebook now because
  • 00:09:51 another typical use case you might have
  • 00:09:53 is that you want to define the limits
  • 00:09:55 for the X and the y-axis in our case we
  • 00:09:58 saw that the limits were created
  • 00:09:59 automatically by PI plot but let's say
  • 00:10:02 you want to customer T's a bit more for
  • 00:10:04 that we simply type plot access like
  • 00:10:07 that and now simply create a list of the
  • 00:10:09 values that you want to have the first
  • 00:10:11 value is the minimum value for the
  • 00:10:13 x-axis which in our case could be lets
  • 00:10:16 say 2013 to change this a bit I know we
  • 00:10:19 don't have a value for that but it's
  • 00:10:21 just to show you how this works
  • 00:10:22 the second list item would be the
  • 00:10:26 maximum value for the x-axis which could
  • 00:10:29 be 2019 in our case I know it's 2018
  • 00:10:31 again but just for testing purposes and
  • 00:10:33 now we have to define the minimum value
  • 00:10:35 and the maximum value for the y-axis now
  • 00:10:37 the minimum value could be right here
  • 00:10:39 maybe something like 20 and the maximum
  • 00:10:43 value could be let's say something like
  • 00:10:45 180 if we do that and hit Rifton enter
  • 00:10:49 you can see that we indeed changed our X
  • 00:10:52 and y-axis values we start with 2014
  • 00:10:55 right here 13 right here and we end of
  • 00:10:58 2019 and we start with 20 and go up to
  • 00:11:00 180 by the way also the decimals were
  • 00:11:04 changed automatically but as I said
  • 00:11:06 we'll have a look at that
  • 00:11:07 in a few minutes now we can do this
  • 00:11:10 right here but actually I'm happy with
  • 00:11:12 the default axis so we can simply
  • 00:11:15 comment this out with the hashtag and
  • 00:11:17 now simply get back our initial chart
  • 00:11:20 style and these are actually the basics
  • 00:11:23 for our basic plotting logic right here
  • 00:11:25 so simply using that PLT dot plot create
  • 00:11:27 the line chart and then we'll playing
  • 00:11:29 around with it the thing is that pie
  • 00:11:31 plot is a lot more powerful than that
  • 00:11:33 and the general logic behind
  • 00:11:36 pipe lot is not just this simple
  • 00:11:39 plotting thing right here pipe lot
  • 00:11:41 things in figures axes with an inner of
  • 00:11:45 an eye then the actual axis with an eye
  • 00:11:47 and the actual content of the chart you
  • 00:11:52 could say the figure basically finds the
  • 00:11:54 entire space where your charts are
  • 00:11:56 displayed with the axis you define the
  • 00:11:59 area of each single visual and then the
  • 00:12:03 axis are the actual axis of each single
  • 00:12:06 visual you have so the x axis and the y
  • 00:12:08 axis with the corresponding content a
  • 00:12:10 line chart for example in our case we
  • 00:12:14 basically have the figure created
  • 00:12:16 automatically
  • 00:12:17 sorry space where our axes are displayed
  • 00:12:20 I refer to the axis now as the actual
  • 00:12:24 visual I think that is easier to
  • 00:12:25 remember so we have a figure which
  • 00:12:27 defines the space of our actual chart
  • 00:12:29 then this chart is displayed right here
  • 00:12:31 with its X and y axis and with the
  • 00:12:34 actual content another thing is we
  • 00:12:36 basically created this figure
  • 00:12:38 automatically right here and we can also
  • 00:12:41 create this step by step which makes a
  • 00:12:43 lot of sense for us especially if we
  • 00:12:46 want to understand how pie plot is
  • 00:12:48 actually working now let's create such a
  • 00:12:50 figure to understand this step by step
  • 00:12:52 for that I'll create another variable
  • 00:12:54 let's call it f IG one for figure 1 and
  • 00:12:58 set it equal to P LT that's the command
  • 00:13:00 we already know and now add the figure
  • 00:13:03 method right here this will allow us to
  • 00:13:06 create a figure we cannot open the
  • 00:13:08 brackets and now we have the option to
  • 00:13:11 add an integer right here this will
  • 00:13:14 basically add this number to the figure
  • 00:13:16 and hold it as a number attribute so we
  • 00:13:18 could add one right here for example and
  • 00:13:20 now we can also add another option which
  • 00:13:23 is the so called fig size the figure
  • 00:13:26 size which basically defines the space
  • 00:13:28 of our figure which in the end will
  • 00:13:30 contain all our charts the fixed size is
  • 00:13:33 defined like this so you type a fixed
  • 00:13:35 size and set it equal to 6.4 and 4.8 now
  • 00:13:42 why 6.4 and 4.8 these are the default
  • 00:13:45 values for your figure so basically also
  • 00:13:47 the values used right here for the width
  • 00:13:49 right here
  • 00:13:50 and for the height we will change his of
  • 00:13:52 course but let's stick to the standard
  • 00:13:53 values for the moment you can also
  • 00:13:56 define additional things right here for
  • 00:13:58 the figure but let's stick to the basics
  • 00:13:59 and continue now with that we created
  • 00:14:02 our figure you can see the width and the
  • 00:14:05 height once again this in pixels I think
  • 00:14:08 and this by the way is in inches so not
  • 00:14:10 in centimeters or something like that
  • 00:14:11 and we basically find the space we're
  • 00:14:14 going to display our axis our charts in
  • 00:14:17 there basically I will also refer to our
  • 00:14:20 axis so basically the area where our
  • 00:14:22 chart is plotted as chart now because I
  • 00:14:25 think it's easier to remember it like
  • 00:14:27 that now what we need here is we need to
  • 00:14:29 add these charts to our figure for that
  • 00:14:31 I will again define two variables one
  • 00:14:34 will be called chart one and I will
  • 00:14:37 refer to our fig one that we just
  • 00:14:39 created and now use the add subplot
  • 00:14:44 method because at subplot will basically
  • 00:14:46 add well a chart to our figure right
  • 00:14:49 here let's select this and now open
  • 00:14:53 brackets and now we'll add three numbers
  • 00:14:56 and you probably won't understand why I
  • 00:14:59 add these but we'll come back to that in
  • 00:15:02 a few seconds I promise for the moment
  • 00:15:04 let's just add one to one right here now
  • 00:15:07 we also want to display the Microsoft
  • 00:15:10 stock price so we will add chart two
  • 00:15:13 right here
  • 00:15:13 set it equal to figure one dot add
  • 00:15:17 subplot once again and I'll enter one
  • 00:15:20 two two
  • 00:15:21 so a slight difference we will see what
  • 00:15:24 this means in a few seconds with that if
  • 00:15:26 we hit shift and enter we can see
  • 00:15:29 already that we have created two charts
  • 00:15:32 two subplots here these subplots are now
  • 00:15:35 part of our actual figure I can prove
  • 00:15:38 this to you if I increase the width
  • 00:15:40 right here to let's say 20 for example
  • 00:15:42 and hit shift and enter you can also see
  • 00:15:45 that our two charts increase the width
  • 00:15:47 and split the room available in our
  • 00:15:50 figure equally so that's the core logic
  • 00:15:53 here you have the figure which defines
  • 00:15:55 the entire space this 20 inches right
  • 00:15:57 here and then you have the two subplots
  • 00:15:59 that we added which are now part of the
  • 00:16:01 figure each subplot each
  • 00:16:04 chart or each axis is basically this
  • 00:16:07 space right here or that space right
  • 00:16:10 there and comes with two axes the x axis
  • 00:16:13 and the y axis a lot of access and axis
  • 00:16:16 right there that's why I will stick to
  • 00:16:18 figure chart and axis I think that's a
  • 00:16:21 lot easier now the problem is that our
  • 00:16:23 charts are now basically available as
  • 00:16:26 kind of raw templates you could say but
  • 00:16:28 they don't contain any kind of
  • 00:16:29 information for that we can simply
  • 00:16:32 change that but now referring to you
  • 00:16:34 charge one right here like that add a
  • 00:16:38 dot and now well basically do what we
  • 00:16:42 learned before we can simply enter dot
  • 00:16:45 plot and now we simply have to add our
  • 00:16:47 data that we want to display in our case
  • 00:16:50 this was year and I think Apple price
  • 00:16:55 like that and we could do the same thing
  • 00:16:58 for chart two dot scatter maybe let's
  • 00:17:03 use a scatter chart and add here and an
  • 00:17:05 S price like that if we shift and enter
  • 00:17:09 we can see that we created our two
  • 00:17:11 charts right here and if we enter PLT
  • 00:17:14 show like that then we also display them
  • 00:17:17 correctly
  • 00:17:18 so without that output stuff so with
  • 00:17:20 that we basically have created two
  • 00:17:23 charts in our figure and we can also
  • 00:17:25 refer to the different charts by our
  • 00:17:28 different variable names right here but
  • 00:17:30 what about these numbers here I talked
  • 00:17:33 about these a few seconds ago well these
  • 00:17:35 numbers basically define how many charts
  • 00:17:38 you want to display in your figure and
  • 00:17:40 how the charge you're referring to is on
  • 00:17:43 our case chart one the Apple chart where
  • 00:17:45 this chart is positioned inside the
  • 00:17:48 structure in our case we simply created
  • 00:17:51 a 1.2 grid right here so we have one row
  • 00:17:55 right there with two columns and inside
  • 00:17:58 this grid our charged one is positioned
  • 00:18:01 on the first position so to the left
  • 00:18:03 right here if I would change this to two
  • 00:18:06 and go to chart two and change it to one
  • 00:18:08 kind of vice versa and hit shift and
  • 00:18:11 enter you can see that both charts
  • 00:18:13 switch their position because now we
  • 00:18:15 again still live our one dot
  • 00:18:17 grid saw one row two columns but our
  • 00:18:20 first chart is now positioned on the
  • 00:18:22 second position and by the way let's
  • 00:18:24 also change the variable names now
  • 00:18:26 because I think it got clear so let's
  • 00:18:27 call this Apple price and let's call
  • 00:18:30 this maybe Microsoft price like that and
  • 00:18:37 also change the name right here and
  • 00:18:40 right there
  • 00:18:41 because I think this makes it easier to
  • 00:18:43 remember like that so we have this grid
  • 00:18:46 structure and we can define the position
  • 00:18:47 of our charts in there but we can also
  • 00:18:52 create different grids we could for
  • 00:18:54 example say we want to have two rows and
  • 00:18:56 two columns if we do this and hit shift
  • 00:18:59 and enter you can see that nothing
  • 00:19:00 changes on the first side but the height
  • 00:19:03 of our two charts decreased of course
  • 00:19:05 because remember we defined the size of
  • 00:19:08 our figure up here so if I would now
  • 00:19:11 change this to let's say 9.6 so twice
  • 00:19:14 the value we had before then we kind of
  • 00:19:17 cut back the height of our two charts
  • 00:19:19 that we had in the beginning but we now
  • 00:19:21 have a 2.2 grid so actually we would be
  • 00:19:24 able to display four different charts in
  • 00:19:27 there I can prove this to you if I
  • 00:19:29 change the number right here for our
  • 00:19:32 Apple price chart to four if I do this
  • 00:19:36 in Egypt and enter you can exactly see
  • 00:19:38 the structure that we created inside our
  • 00:19:41 figure we have the figure right here we
  • 00:19:45 have two rows and two columns and the
  • 00:19:48 first position inside the grid right
  • 00:19:50 here is the one for all Microsoft price
  • 00:19:52 this one and the last position so in the
  • 00:19:55 right bottom corner we have the Apple
  • 00:20:00 price chart now so with that approach
  • 00:20:02 you can easily create such grids and
  • 00:20:04 define the position of these different
  • 00:20:06 charts inside your grid inside your
  • 00:20:08 figure and also refer to these charts
  • 00:20:11 individually right here by the variable
  • 00:20:13 name now we will stick to our 1.2 grid I
  • 00:20:18 would say because I think it's enough
  • 00:20:20 for our purposes it's changed you to
  • 00:20:22 like that and maybe let's also decrease
  • 00:20:25 the size let's say to 5 like this I
  • 00:20:28 think this looks fine now before we
  • 00:20:29 continue let's come back to the
  • 00:20:30 beginning
  • 00:20:31 because there I charged it we will have
  • 00:20:33 a look at the formatting of our years
  • 00:20:35 down here because it still doesn't look
  • 00:20:37 right
  • 00:20:37 for that we have to go back to our top
  • 00:20:40 right here and import from Matt plot lip
  • 00:20:46 dot ticker because we want to have an
  • 00:20:48 impact on to our data ticks on to our
  • 00:20:51 tickers down there and we want to import
  • 00:20:53 the max and locator like that let's add
  • 00:20:58 shift and enter let's go down and let's
  • 00:21:00 have a look at the documentation to see
  • 00:21:02 what I actually did right here so here
  • 00:21:05 we are
  • 00:21:05 link can be found in the video
  • 00:21:07 description and well you can see right
  • 00:21:09 here what we can do with this ticker we
  • 00:21:12 just imported it allows us to configure
  • 00:21:14 the tick locating and the formatting and
  • 00:21:17 if you look right here at the end
  • 00:21:19 locator de Max and locator you can see
  • 00:21:21 that it will help us to find a maximum
  • 00:21:24 number of intervals and set the ticks at
  • 00:21:26 nice locations now for the data that we
  • 00:21:29 have this is actually totally enough and
  • 00:21:31 all we need because if we now go back to
  • 00:21:33 our Jupiter notebook right here back in
  • 00:21:37 the chart so down here and now maybe
  • 00:21:40 right here below our Apple price so say
  • 00:21:42 Apple underscore price dot tap if we now
  • 00:21:47 type X you can see that we can refer to
  • 00:21:50 the x-axis so let's hit enter now add
  • 00:21:52 another dot and type s and if you now
  • 00:21:56 scroll down a bit you can see that we
  • 00:21:58 have our major locator right here this
  • 00:22:01 is what we will need so let's select
  • 00:22:02 this one and let's open the brackets and
  • 00:22:04 let's not say that our necks and locator
  • 00:22:07 sort of one that we imported previously
  • 00:22:09 and open two brackets right here should
  • 00:22:12 be used so we want you find these nice
  • 00:22:15 tick marks these nice positions for the
  • 00:22:17 tick marks and it should be set to an
  • 00:22:20 integer integer should be equal to true
  • 00:22:22 now if this all worked correctly if a no
  • 00:22:25 hit shift and enter we should see that
  • 00:22:28 our integers or our x axis numbers
  • 00:22:31 became integers and with that D
  • 00:22:33 formatting became a lot nicer we can
  • 00:22:36 also quickly do the same thing for our
  • 00:22:38 Apple chart right here by copying that
  • 00:22:41 down there and adding Microsoft
  • 00:22:45 price right here check the Lynch once
  • 00:22:47 again and as you can see with that we
  • 00:22:50 also formatted our x-axis down here so
  • 00:22:53 this is how we can basically get started
  • 00:22:55 with matplotlib and how we can create
  • 00:22:57 such subplots now one less thing about
  • 00:22:59 like to show you in this video is a way
  • 00:23:01 how to create these subplots quicker
  • 00:23:04 because we only have two subplots right
  • 00:23:06 now but we always have to add the
  • 00:23:07 subplot right here we have to define the
  • 00:23:09 grid and the position and so on not
  • 00:23:12 really convenient if you want to have a
  • 00:23:13 lot of subplots for that let's go down
  • 00:23:16 right here and let's now combine some
  • 00:23:17 things in one single code line let's
  • 00:23:20 call this figure two like that and now
  • 00:23:24 add a comma and add axis right here so
  • 00:23:27 basically we want to create a figure and
  • 00:23:30 this figure should contain some X's
  • 00:23:32 without diving too much into the code
  • 00:23:34 behind and how this logically works this
  • 00:23:36 is basically what is written right here
  • 00:23:37 we can now set this equal to
  • 00:23:40 PLT dot sub plot as subplots because we
  • 00:23:46 want to add multiple plots to our figure
  • 00:23:49 right here
  • 00:23:50 now we can define the grid size so let's
  • 00:23:55 say we want to create a 1.2 grid once
  • 00:23:59 again and we can also define the fig
  • 00:24:02 size as little before let's say this
  • 00:24:05 should be equal to 20.5 once again the
  • 00:24:09 same structure we had above right here
  • 00:24:11 if we now add PLT show we can see
  • 00:24:16 exactly the same grid that we have
  • 00:24:18 before but created with one single line
  • 00:24:21 of code
  • 00:24:21 which is a lot more convenient as I
  • 00:24:23 would say and if you know say well I'm
  • 00:24:25 not happy with the grid structure I want
  • 00:24:27 to have a I don't know three rows and
  • 00:24:30 two columns grid you simply change the
  • 00:24:32 one two three hit shift and enter and as
  • 00:24:34 you can see you get these free grid
  • 00:24:36 lines keep in mind that the figure still
  • 00:24:38 defines the enter space available for
  • 00:24:40 our plots right here so if you change
  • 00:24:42 this to ten right here for example the
  • 00:24:45 space increases and you get a nice
  • 00:24:48 formatting or more space to display your
  • 00:24:50 charts I should say so this is how you
  • 00:24:53 can create these subplots quicker and if
  • 00:24:57 you not want to refer
  • 00:24:59 but for all purposes I think that the
  • 00:25:01 1.2 grid is again nicer and let's maybe
  • 00:25:04 change back to five and if you know want
  • 00:25:06 to refer to the different subplots right
  • 00:25:09 here because you want to add some
  • 00:25:10 information to it you can simply go
  • 00:25:12 right here and type access open the
  • 00:25:15 square brackets and now simply say that
  • 00:25:17 you want to refer to the chart with the
  • 00:25:21 index zero right here now this should
  • 00:25:25 become a scatter chart I'm just taking
  • 00:25:29 the values right here and it should
  • 00:25:32 contain the year and the Apple price if
  • 00:25:35 I do this and hit shift and enter you
  • 00:25:37 can see that I got the values for the
  • 00:25:40 I'm sorry this should be the Microsoft
  • 00:25:43 price up here for a scatter chart so you
  • 00:25:45 can change it to an S price right here
  • 00:25:48 like that now we got it and you can also
  • 00:25:51 refer to the chart with the index number
  • 00:25:55 of one in our figure right here by
  • 00:25:57 adding dot plot maybe year and Apple
  • 00:26:01 price like that hit shift and enter and
  • 00:26:04 you can see it works fine if it would
  • 00:26:07 increase it now to a 2.2 grid I get an
  • 00:26:09 error because I need to be more precise
  • 00:26:11 about the index because here we have the
  • 00:26:13 row index 0 but we also need the column
  • 00:26:16 index which could be 1 in our case so we
  • 00:26:19 want to display Microsoft right here in
  • 00:26:22 this chart and we want to display let's
  • 00:26:26 say Apple in 1.0 so in the second row
  • 00:26:30 because index starts at 0 with the first
  • 00:26:32 row and so on and so it should be the
  • 00:26:35 row with the index number 1 so the
  • 00:26:37 second row and the first column so it
  • 00:26:40 should be here so let's see if this
  • 00:26:41 works if I shift an enter you can see
  • 00:26:43 that we have the Microsoft price being
  • 00:26:45 displayed right here and we got the
  • 00:26:47 Apple price displayed in the left lower
  • 00:26:50 chart right there and with that I also
  • 00:26:52 want to finish this introduction video
  • 00:26:54 to matplotlib
  • 00:26:55 I hope you got a basic idea how easy to
  • 00:26:57 use matplotlib is in the beginning but
  • 00:27:00 you might also have seen already that it
  • 00:27:02 is really powerful and that you can do a
  • 00:27:04 lot of things with it so you can dive a
  • 00:27:07 lot deeper and we probably also will in
  • 00:27:09 some other videos on this channel but
  • 00:27:11 for
  • 00:27:12 moment this is everything you need to
  • 00:27:14 get started I think and with that I can
  • 00:27:16 only say thanks so much for watching and
  • 00:27:18 I hope to see you in one of the next
  • 00:27:20 videos bye