Full transcript
0:07hello my name is richard kirschner with
0:09the simply learn team that's
0:11www.simplylearn.com get certified get
0:14ahead
0:15pandas really is a core python module
0:18you need for doing data science and data
0:21processing there's so many other modules
0:23that come off of it there it actually
0:25sits kind of on numpy so if you've
0:26already had our numpy array hopefully
0:28you've already gone through the numpy
0:29tutorial one and two so today we're
0:31going to cover what is pandas we'll
0:33discuss series we'll discuss basic
0:36operations on series then we'll get into
0:38a data frame itself basic operations on
0:41the data frame file related operations
0:44on a data frame visualization and then
0:46some practice examples roll up our
0:49sleeves and get some coding underneath
0:50there and let's start with just some
0:53real general what is pandas pandas is a
0:56tool for data processing which helps in
0:58data analysis it provides functions and
1:01methods to efficiently manipulate large
1:03data sets
1:05now this is a step down from say using
1:07spark or hadoop in big data so we're not
1:10talking about big data here but we are
1:12talking about pandas when there is some
1:14connections there's like an interface
1:15going on with that so there is
1:16availability but you really should know
1:18your pandas because if you're working in
1:20big data you'll know there's data frames
1:22well pandas is a data frame primarily it
1:25has a couple different pieces we'll look
1:26at here and if you've never worked with
1:28data frames before a data frame is
1:31basically like an excel spreadsheet you
1:32have rows and columns you can access
1:35your data either by the row or the
1:37column and you have an index and
1:39different that kind of setup and we'll
1:41dig more into that as we get deeper into
1:44pandas but think of it as like a giant
1:46excel spreadsheet that's optimized to
1:49run a larger data on your computer
1:51and then i said it that it's a data
1:53frame so the data structures in pandas
1:56are series one-dimensional arrays and
1:59then we have data frame two-dimensional
2:02array and it really centers around the
2:03data frame the series just happens to be
2:05part of that data frame and here's a
2:07closer look at a pandas series series is
2:10a one-dimensional array with labels it
2:12can contain any data type including
2:14integers strings floats python objects
2:17and more so it's very diverse if you
2:19remember from numpy we studied they had
2:21to be all uniform not in pandas and
2:24pandas we can do a lot more and pen is
2:26actually kind of sits on numpy so you
2:27really need to know both of those if you
2:29haven't done the numpy tutorials and you
2:31can see here we have our index one two
2:33three four five and then our data a b c
2:36d and e very straightforward it's just
2:38two columns and we have a nice index
2:40label and a column label for the data
2:43and then a data frame is a two
2:44dimensional data structure with labels
2:47we can use labels to locate data and you
2:49can see here we had if we go back one we
2:52had our index one two three four five so
2:55in each one of these series they would
2:56share the same index over there the row
2:58index so you have your row index df dot
3:01index and then you have a column index
3:03df.columns and this would look like i
3:05said this would be really familiar if
3:07you've done any work with spreadsheets
3:08excel so it kind of resembles that this
3:11does make it a lot easier to manipulate
3:13data and add columns delete columns move
3:16them around same thing with the rows so
3:18you have a lot of control over all of
3:20this now we're of course going to do
3:21this in our jupiter notebook you can use
3:24any of your python editors but i highly
3:26suggest if you haven't installed jupiter
3:28and haven't worked with it it is
3:30probably one of the best ways for easily
3:32displaying a project you're working on i
3:34skip between a lot of different user
3:36interfaces or ides for editing my python
3:39and it's just simply jupiter.org
3:41j-u-p-y-t-e-r.org
3:44and then i always let mine sit on
3:46anaconda anaconda.com
3:49and just real quick we'll open that up
3:50for you oops offline mode don't show me
3:53that again but you can see here that i
3:55have different tools that i can actually
3:57install in my anaconda including the
4:00jupiter notebook which comes by default
4:02and then i have access to the
4:03environments
4:04and again that's
4:06anaconda.com named after the very large
4:08one of the largest world's largest
4:10snakes and then jupiter notebook in this
4:12case jupiter.org and when we're in our
4:15i'm going to go in here to our jupiter
4:17notebook and we're going to go ahead and
4:18just do new and a python 3
4:21and this will open up a python 3
4:23untitled folder
4:25so diving right in let's go ahead and
4:27give this a title pandas tutorial and
4:30we'll go up to cell and we'll change the
4:33cell type to markdown so it doesn't
4:35execute it as actual code
4:37one of those wonderful tools when you
4:39have jupyter notebooks you can do demos
4:41with this and let's go ahead and import
4:44pandas
4:45and usually people just call it pd that
4:47has become such a standard in the
4:49industry so we'll go ahead and run that
4:51now we have our pandas has been imported
4:53into our jupyter notebook
4:55and then oh we can go ahead and let me
4:57do the control plus since it's internet
4:59explorer i can enlarge it very easily so
5:00you have a nice pretty view oops too big
5:03there we go and whenever you're working
5:04with a new module it's good to check
5:06your version of the module in pandas you
5:08just use the in this case pd dot
5:10underscore underscore version underscore
5:12underscore that's actually pretty common
5:14in most of our python modules there's
5:16different ways to look up the version
5:18but that's one of the more common ones
5:19and we'll go ahead and run that we get
5:200.23.4
5:22and if we go to the pandas site we see
5:250.23.4 as the latest release and of
5:28course a reminder that if you're going
5:30to environment you need to install it so
5:33you'll need to pip install pandas if
5:35you're using the pip installer we'll go
5:37and close out of that
5:39and the first thing we want to do is
5:40we're going to work with series a lot of
5:42stuff you do in series you can then do
5:44on the whole data set we need to do what
5:46create one we need to manipulate it
5:50take pieces of it so query it query it
5:54delete so you can delete different parts
5:56of it so we want to do all those things
5:58with the series and we'll start with the
6:00series and then almost all the code in
6:02fact all the code does transfer
6:04right into
6:06the actual data table so we go from a
6:09series of a single list of one column
6:12and then we'll take that and we'll
6:13transfer that over to the whole table
6:15and we'll start by creating let's put up
6:17there we go
6:19creating a series from
6:23list
6:24and let's just call this arr equals
6:27and we'll do 0 1 2 3 4. if you remember
6:31from our last one we could easily do
6:33r equals
6:34range of five which would be zero to
6:36four but we'll do r equals zero to four
6:39and we'll call this s1 and we'll go pd
6:43and
6:44series is capitalized this one always
6:46throws me is which letters do you
6:48capitalize on these modules they're
6:50getting more and more uniform but you
6:51got to watch that with python and we're
6:54just going to go ahead and do arr
6:56so we're just going to take this python
6:58list and we're going to turn it into a
6:59series
7:00and then because we're in jupiter we
7:03don't have to put the print statement we
7:04can just put s1 and it'll print out this
7:07series for us
7:08and let's go ahead and run that and take
7:10a look
7:11and you'll see we have two rows of
7:13numbers so the first one is the index
7:17now it automatically creates the index
7:18starting with 0 unless you tell it to do
7:20differently so we get 0 index row 0 0 1
7:241 2 2 3 3 4 4. and because it's a series
7:28it doesn't need a title for the column
7:30there's only one column so why title it
7:34and this also lets you know that it's a
7:35data type of integer 64. so we print
7:38this out this is our series our basic
7:40series we've just created
7:42now let's do a second series
7:45pd and we'll use the same
7:49data list and let's go ahead and do
7:51order we'll give it an order
7:53equals oh let's do it this way
7:56let's go
7:57index
7:58equals order
8:00and it helps if we actually give it an
8:02order so we'll do order
8:04equals
8:05and let's do one two three four five so
8:09instead of starting with zero we're
8:10going to give it an order starting with
8:11one we're going to run that and we'll go
8:14ahead and print it out down here s2
8:17and we'll see that we now have an index
8:19of 1 2 3 4 5 and that represents 0 1 2 3
8:234 in the series and we're still data
8:26type integer 64. and very common as
8:29you're missing with numpy arrays is we
8:31can import our numpy as np remember that
8:33from our numpy tutorials we can go ahead
8:35and create a numpy out of random with
8:38the random numbers of five and let's
8:40just see what that end looks like so we
8:42can see what our number looks like so we
8:44have some nice random float values here
8:45two point three three so on and that's
8:47from our last tutorial the numpy
8:49tutorial one and two and instead of
8:51calling it order let's call it index
8:55and we're going to set our index equal
8:56to a b c d and e
8:58i want to show you that the index
9:00doesn't have to be an integer so it can
9:02be something very different here and
9:04then let's go ahead and create our we'll
9:05just use s2 again and here's our np for
9:09numpy
9:10series capital s
9:12and n is our
9:14np for numpy
9:16pd for pandas there we go switching my
9:19anachronisms so we have pd.series of n
9:22and we're going to do our index
9:24equals our index we just created
9:28and then let's go ahead and see what
9:29that looks like s2 is a print it and
9:31let's run that
9:32and we can see here we have a nice
9:34series going on a b c d and e for our
9:37indexes so instead of being zero one two
9:39three or four we can make this index
9:40whatever we want and you can see the
9:42numbers here going down that we randomly
9:44generated from the number array so we
9:46use numpy to create our
9:49panda series right here
9:51and so continuing on with creating our
9:53series this one i use so often we create
9:56a series from a dictionary so we have
9:58our dictionary in this case we went
9:59ahead and did a of 1 b is 2 c of 3 d4
10:03ef5 so each one of those is a key and
10:06then a value and then we're going to use
10:08oh let's use s3 equals pd for pandas
10:12series
10:14and then we want to go ahead and just do
10:16d in here
10:17print out s3 here and let's go ahead and
10:19run this and you can see we got a is one
10:22b is two c is three d is four e is five
10:25and it's still of integer 64 because the
10:28actual data is one two three four five
10:30and it's all integers 64 type 64. and
10:33the last thing we want to do in the
10:35creating section of our series is to go
10:38ahead and modify the index because we're
10:41going to start modifying all this data
10:42so let's start with modifying the index
10:44of the series and if you remember let's
10:47do a print this time s1
10:49i'll go ahead and run this and the
10:51reason i did print is because it only
10:53prints out the last variable so if i put
10:55s1 up here and we're going to do another
10:58variable back down lower it won't print
11:00the first one just the last one and
11:02we're going to go ahead and take s1
11:05the index and we're just going to set it
11:07equal to a new index and obviously
11:11the number of objects in our index has
11:13to equal the number of objects in our
11:15data and then because it's the last
11:17variable we can go ahead and just do an
11:18s1 and let's run that and you can see
11:21how we went from 0 to 0 0 1 2 3 4 as our
11:25index we've now altered it to a b c d
11:27and e
11:28so this would be much more readable or
11:30might be representational of a larger
11:32database you're working with
11:34so cool tools we've covered creating
11:37database based on
11:39a basic array python array we've showed
11:42you how to
11:43reset the index
11:45then we showed you how to use a numpy
11:47array so you can put a numpy array in
11:49there it's all the same you know
11:51pd.series a numpy array and then we can
11:53set the index on there and the same
11:55thing with the dictionary so it's very
11:57versatile how it pulls in data and you
11:59can pull in data from different sources
12:01and different setups and create a new
12:03series very easily in
12:05the pandas and then we looked on
12:06changing your index so now we have a new
12:09index on here
12:10and then we want to go ahead and do some
12:12selection let's do some basic
12:15slicing most common thing you'll
12:17probably do on here and we'll just do s1
12:20this notation should start to look
12:22really familiar again this is going to
12:24put an output so i'd usually it doesn't
12:27change s1 this just selects it so we
12:30might do a equals s1 and then print a
12:34and you'll see that it just looks at the
12:35first three zero 1 2. we can do the same
12:38thing by not having the a in there i'll
12:41go ahead and take that out but just a
12:42reminder that it's not actually changing
12:44s1 it's just viewing s1 so simple
12:47slicing on here and we can likewise do
12:49an append so before we do a pen let's
12:52just do a quick kind of fun one we'll do
12:54two minus one and you'll see it covers
12:56everything but the e of course you can
12:58do minus two on this side
13:00so one another way to select it is to go
13:02how far from the end and likewise we can
13:05do a 2 here
13:06a cde to the end so it starts at the
13:09second one and another way we can do
13:11this is we can do a minus 2 over here
13:14and that looks at just the last two in
13:15the slice so you can see how easy it is
13:18to slice the data and of course
13:20there's no reason to do this but you
13:21could select all of them
13:23if you wanted to view all of them on
13:25there up 32 there's not 32 so it's just
13:28going to show the first three there we
13:29go and then we can also append so i can
13:32take and oh let's create another series
13:34and append one to it and if you remember
13:37we had s3 there's our s3 and we have our
13:40s1 we'll go ahead and do s1
13:43and let's go ahead and do
13:46oh let's call it s4
13:48equals s1
13:51a pin
13:52s3
13:54so we're just going to combine those two
13:55into s4
13:57and if we go ahead and print s4 on here
14:00you'll now see that we have a b c d e a
14:03b c d e zero one two three four one two
14:05three four five because we started the
14:06data at one it's a very easy to append
14:09one series to the next
14:11and if we're going to append one series
14:13to the next we need to go ahead and drop
14:16or delete one and drop is a key word for
14:19that and let's just do e or index e and
14:22so if i run this
14:24you'll see that it'll print it out and
14:26a b c d there's no e
14:29and remember all these changes if i type
14:31in s4 again
14:33you'll see that s4 still has e in it so
14:36this change does not affect the series
14:39unless you tell it to so i'd have to do
14:41like x s4 equals s4.drop e and there's
14:44another way to do that which we'll show
14:46you later on let me just cut this one
14:47out
14:49there we go
14:50all right so we've covered all kinds of
14:52cool tools here we have appending we
14:54have slicing we did all the creating
14:57stuff earlier as you can see here on the
14:59setup how easy it is to manipulate the
15:01series
15:03so next what we want to get into is we
15:05want to get into
15:07operations that happen on the series let
15:09me go ahead and change this cell to
15:12mark down there we go and run that so
15:15series operations what can we do with
15:16the series
15:17and let's start by creating a couple
15:19arrays we'll call it array 1 and we'll
15:21do 0 through 7 and array 2 six through
15:26six seven eight nine five i don't know
15:28if we threw the five on the end let's go
15:30ahead and run those so those load up
15:31into jupiter
15:33and uh we'll do this a little backwards
15:34we're gonna do s5 equals a panda series
15:38of array two so i'm doing this in
15:40reverse and then when we do s5 you'll
15:43see that we have zero to four it
15:45automatically assign the index
15:4767895
15:49for our series
15:51and let's go ahead and do the same and
15:52we'll call this s6 and we'll set this
15:55equal to pd series for
15:58our first array
16:00and if we do an s6 down here to print it
16:02out
16:03we'll see something similar i got 0
16:06through 6
16:07zero one two three four five seven for
16:09the data so those are two series we just
16:11created series six five and six
16:14and one of the first things we can do is
16:16we can add one series to the next so i
16:18can do s5 dot add s6 and let's see what
16:23that generates and just a quick thing if
16:25you never use pandas what do you think
16:27is going to happen with the fact that
16:29this only has five different values in
16:32it and this one has seven values
16:35so let's see what that does
16:37and we end up with 6 8 10 12 9 and it
16:41goes oh i can't add this there's nothing
16:43there so it gives us a null return
16:45very different than the numpy that would
16:47have given you an error this instead
16:49tells you there's no value here because
16:51we couldn't generate one so we can
16:52easily add s5 dot add s6 and likewise we
16:56can do s5 dot
16:59sub for subtract s6
17:03and we'll run that and on the add the
17:05subtract and you guessed it we're going
17:07to do multiply and divide next again you
17:09can see there's the null values where it
17:11can't subtract the two because there's
17:12no values there to subtract we can also
17:15do s5 multiply mul they're all three
17:18letters on these that's one of the ways
17:19to remember how they figured out the
17:22code for this so remember these are all
17:23three letters mole we'll go ahead and
17:25run this
17:26and you again you can see how they're
17:28multiplied together and then we can also
17:30do the s5 div three letters again
17:33s6
17:34and run that
17:36and you'll see here this goes to
17:38infinity because we have zero in the
17:40wrong position so it actually gives you
17:42a whole different answer here that's
17:43important to notice and then in the null
17:46values because there's no data and it
17:47can't actually produce an answer off of
17:49an old off of missing data and since
17:52we're in data science let's do s6
17:56median so let's look at the median data
17:59which is simply
18:00median sorry for those who are following
18:02the three letters because median is not
18:03three letters and you can see an s6 is
18:063.0 and let's do a print here and we'll
18:09do median
18:10or average s6
18:13and let's print max
18:16comma s6
18:18and just like median there's max value
18:20and if we're going to have a max value
18:22we should also have a minimum value so
18:25let's pop in minimum
18:28we'll go ahead and run this and you're
18:30starting to see something that would be
18:32generated like say an r where you're
18:33starting to get your different
18:34statistics we have a median value of 3
18:37max value of 7 and a minimum value of 0.
18:40and what it does when it hits these null
18:42values if there is no values in there
18:44because we could still do that we could
18:46actually you know what let's go up here
18:47and do
18:50let's pick this one we multiplied let's
18:52go s7
18:54equals i'll go and print the s7 just so
18:57i keep it nice and uniform so i still
18:59have my s7 down there and run it
19:01and then i want to take the s7
19:04because s7 now has
19:06null values and an infinity value and
19:09let's see what happens
19:10this is going to be interesting because
19:11i want to see what it does with infinity
19:13and we end up with a median of 6 maximum
19:16of 27 and minimum of 0. which is correct
19:20it drops those values so when it gets to
19:22there and it doesn't know what to do
19:23with them it just drops those values and
19:25then it computes it on the remaining
19:27data on there so that's important to
19:29know when you're making these
19:30computations you're looking at min and
19:32max and median
19:33you're not going to know that there's no
19:35values unless you double check your data
19:36for the null values it's a very
19:38important thing to note on there so just
19:40a real quick
19:42review on there we've done our created
19:45our pd series and we've gone ahead and
19:48done addition subtraction multiplication
19:51division all those are three letters so
19:53sub min div add
19:56and then we looked at median maximum and
19:58minimum so we're going to go ahead and
20:00jump into the next big topic which is to
20:02create a data frame
20:04so now we're going to go from series and
20:06we're going to create a number of series
20:07and bundle them together to make a data
20:09frame
20:12there we go cell type markdown let me go
20:14ahead and run that so we have a nice
20:15title on there it's always good to have
20:16a good title all right so our first data
20:18frame we'll jump in with some stuff that
20:20looks a little complicated we'll break
20:22it down first i'm going to create some
20:24dates and you know what let's just go
20:26ahead and do this i want you to see what
20:28that looks like what i'm creating here
20:30i've created a series of dates pd date
20:33range and we're going to use these for
20:35the index okay so when you look at this
20:38you'll see that it's just
20:39basically it comes out kind of like a
20:41basic python list or numpy array however
20:43you want to look at it with our
20:45different dates going down and we've
20:46generated six of them and it's going to
20:48have whatever time it is right now on
20:50your on the thing for the date for the
20:52time that's that time stamp right there
20:55and then you'll see we have 11 19 2008
20:5811 20 11 19 and looking into the future
21:02there so that's all this is is
21:04generating a series of dates that we're
21:06going to use as our index and this is a
21:09pandas command so we have a date range
21:12which is nice it's one of the tools
21:13hidden in there in the pandas that you
21:15can use
21:16and next we're going to use numpy to go
21:18ahead and generate some random numbers
21:20in this case we'll do the
21:21np.random.random
21:23in 6 comma 4. you can look at this as
21:26rows and columns as we move it into the
21:30pandas and of course you could reshape
21:32this if you had those backwards on your
21:34data but we want the six to match the
21:36rows and we have six periods so our
21:38indexes should match along with the rows
21:40on there and then you know before we do
21:43the next one let's go ahead and just
21:45print out our numpy array so you can see
21:46what that looks like here we have it one
21:48two three four by one two three four
21:51five six four by six
21:53so it's a nice little setup on there and
21:55since working with data frames can be
21:57very visual let's give our columns we
21:59have four columns and we're going to
22:00give them names a b c and d so now we
22:04have columns on there also and then
22:05let's put this all together in a data
22:07frame and we can actually you know what
22:09let's do this since i did it with
22:10everything else let's go ahead and do
22:11columns and you can see there's our
22:12columns on there
22:14and we'll go ahead and do df1 equals
22:18pandas dot
22:20data frame and note that the d and the f
22:23are capitalized series it was just the s
22:26and i always highlight this because you
22:28don't know how many times these things
22:30get retyped when you forget what's
22:32capitalized on there it's a minor thing
22:33you'll pick it up right away if you do a
22:35lot of it and the first thing we want to
22:36do is we want to go ahead and take our
22:37numpy array so we're going to create our
22:39data frame off of is the numpy array and
22:42then we want our index equal to our
22:45dates so there's our index in there and
22:48then we also have columns equals
22:50columns
22:51and then finally let's see what that
22:53looks like now remember we had all the
22:55different data that just looked like a
22:57jumble of data we have our column names
22:59and everything else our numpy array kind
23:01of just a jumble array over there four
23:03by six you could sort of read it but
23:05look how nice this looks i mean this is
23:07you come into a board meeting you're
23:09working with your
23:10shareholders
23:12this is pretty readable this is you know
23:14this is our date this is our a b c d
23:17whatever it is maybe each one of these
23:18dates has your leads
23:21closures lost leads total dollar made
23:24you know whatever it is if it's in a
23:26business maybe it's measurements on some
23:28scientific equipment whether searching
23:30material you know where this is like
23:33higher the temperature low of the day
23:35humidity of the day whatever it is so
23:37you can see that we can really create a
23:38nice clear chart and it looks just like
23:40a spreadsheet you know we have our rows
23:42and we have our columns and we have our
23:44data in there now this one i use all the
23:47time if we're going to create we can
23:48create it like you saw here with our
23:50numpy array very easy to do that and
23:52reshape it you can also create it with a
23:54dictionary array so here we have some
23:56data let me just go down a notch so you
23:57can see all the data on there we have an
24:00animal in this case cat cat snake dog
24:03dog cat snake cat dog we have the age so
24:06we have an array of ages we have the
24:08number of visits and the priority was it
24:10a high priority yes
24:12no
24:13and then we're going to take that we're
24:15going to create some labels we have a b
24:17c d e f g h i and what i want you to
24:20notice on this is we have a title animal
24:23and then we have basically a python list
24:27and these lists they don't necessarily
24:28have to be equal because we can have
24:30non-data you know np.nan numpy array
24:33null value but we want to go ahead and
24:35create labels that are equal to the
24:36number in the list so a
24:38the first cat b the second cat c the
24:41snake d the dog and so on so we'll go
24:43ahead and create our labels which we're
24:44going to use as an index
24:47and we'll call this df let's do it this
24:49way we'll call this df2
24:51equals pd for pandas
24:54data frame
24:56and then we have our data just like we
24:58did before and we have our index equals
25:01labels
25:03and if we're going to go from there
25:04let's go ahead and print it out so we
25:05can see what that looks like df2 so
25:07let's go ahead and run that another
25:09again you have a nice very clean chart
25:11to look at we've gone from this mess of
25:14data here to what looks like a very
25:16organized spreadsheet very visual and
25:18easy to read
25:19animal age visits priority and then a
25:22through j cats and all your different
25:24animals so on and so on and then when
25:26you do programming a lot of times it's
25:28important to know what the data types
25:29are so we can simply do df2
25:33d types
25:34and if we run that we can see that our
25:37animal
25:38is an object because it's just a string
25:40but it comes in as an object age is a
25:42float64 integer 64 and then priority
25:45again is just an object
25:47and exploring this this one's very
25:49popular let's go df2
25:51[Music]
25:52head
25:53and if we print that out the df2 head
25:57returns the first five and we can change
26:00this you don't have to do five you might
26:01want to just look at the top two maybe
26:03you want to look at
26:05let's see let's do six so maybe we'll
26:07look at just the top six in the database
26:09in your data frame and you can actually
26:12this creates another data frame so i
26:15could have a df3 equal to df2 and this
26:19now takes the df2 and just the first six
26:22values
26:23so if we do df3
26:26run get the same answer
26:29and if we do it the head of the data we
26:31can also do the tail it's the same thing
26:34df tail you can look at the last we'll
26:37just do the tell which by default does
26:39five the last five and of course you can
26:41just look at the last three of those
26:43real quick just to see what's at the end
26:45of the data and this is i need to tell i
26:47love doing the tail of one because i'll
26:49have like the index or something like
26:52that and it will just show me the last
26:53whatever the last entry was i'm looking
26:55at stock values and i might want to look
26:57at just the last five days of the stock
27:00values i can do that with the data frame
27:02tail
27:03and some other key things to look up
27:06are the index so we can do df2.index
27:11and i want you to notice that this isn't
27:13a call function so if i put the brackets
27:15on the end it'll give me an error
27:16because index is not callable it's just
27:18an object in there
27:20so we do
27:21df2.index there's also columns
27:25so we can go ahead and let's do a let's
27:27print this
27:29remember the first one is not going to
27:30show unless i print it and then df2
27:34columns so now we can see we have our
27:35indexes
27:36and we have our columns listed here
27:38df2.columns animal age visits priority
27:42it tells you what kind of object it is
27:44or what kind of data type it is and
27:46they're both object
27:47and then finally df2 dot
27:50values and again there's no brackets on
27:53the end of df2.values
27:55because this is an actual object it's
27:57not a callable function so we'll go
27:59ahead and run that and it creates just
28:01displays a nice array a very easy way to
28:03convert this back to a numpy array
28:05basically so before i go into the next
28:08section let's just take a quick look at
28:10what we covered so far with the data
28:12frame we came up here we created our
28:14data frame we did it from a numpy array
28:16first
28:17setting the columns and the index the
28:20index is setting it up is the same as
28:22when we set up the series so that should
28:24look very familiar so is the whole
28:26format the numpy array the index dates
28:29and the columns columns and remember in
28:31our numpy array we're looking at row
28:34comma column so six rows four columns is
28:38how that reads in the data frame
28:40and we went ahead and also did that from
28:42a dictionary in this case animal was the
28:45column name with all the date data
28:48underneath that column and then age with
28:50that data visits that data priority that
28:52data and then of course we added our
28:54labels in there for our index so there's
28:56no difference in there but it
28:57automatically pulled the column names
29:00important to know when you're dealing
29:01with a data frame and importing a data
29:03frame this way
29:04and then we did looking up d type we
29:06looked at head and tail looking at your
29:08data really quick
29:10we also did index and columns and values
29:13and note these don't have the brackets
29:15on the end
29:16so the next thing we want to do is go
29:18ahead since we're dealing with data
29:19science is we want to go and describe
29:21the data so we have
29:24df2.described to do that and we're going
29:26to manipulate it in just a minute but
29:27let's just see what this generates
29:30and you can see right here we have age
29:32and visits
29:34so looking at our data from up above let
29:36me just go all the way up here
29:37animal age visits priority
29:41and it does a nice job generating your
29:43age versus visits which has all the data
29:45you have your account your means your
29:46standard deviation your minimum value
29:4925 or in this group 50 75 and your
29:52maximum value so this will look familiar
29:54as a data science setup with your
29:56describe for a quick look at your data
29:59frame data so let's start manipulating
30:02this data frame moving stuff around and
30:04we'll start with transposing and it is
30:07simply capital t for transpose
30:10and when we run that it flips the
30:13columns and the indexes so now the
30:16indexes are all column names and the
30:18columns are all indexes animal age
30:20visits priority
30:21so if we had come in here with our data
30:24shaped wrong up above where we had a 4x6
30:27we can quickly just swap it if we had it
30:30backwards not a big deal and we can also
30:33sort our data something that you can't
30:35do which is more difficult to do with a
30:37lot of other packages in the data frame
30:39it's really easy to do take our data
30:41frame df2 and we're going to sort
30:43underscore values
30:46by equals age and so when we run this
30:49you'll see the default is ascending so
30:52we have 0.52 2.53 and everything else is
30:55organized so if you look at your indexes
30:58they've been moved around because each
31:00index it moves a whole row not just the
31:03one piece of data is not being sorted so
31:05very quick way to sort by age are
31:07different data in the data frame and in
31:09addition to sorting it we can also slice
31:12the data frame so i could do df2 and
31:15this should look familiar from earlier
31:17we'll just do one
31:18to three so we're going to pull out oops
31:22it does help if i use the df instead of
31:23just d and we're going to pull up just
31:25between one and three so we have not
31:27zero which is a but we have b which is
31:30two or b which is 1 and c which is 2. so
31:331 2 and then it does not include 3 which
31:36is the standard in python and we can
31:38even do something like this we can
31:40combine them which is always fun because
31:42remember this returns a data frame so if
31:44i take df2 dot
31:47sort
31:49values and we'll do by
31:52equals age
31:54this is just kind of fun and then i'm
31:56going to slice it
31:57there we go double check my typing and
31:59run it
32:00and now you should see fa because fa are
32:03now one and two on there
32:05so you can very quickly create a whole
32:07string on here which narrows it you know
32:10that you can sort it then slice it and
32:12do all kinds of fun things with your
32:14data frame we'll just go back to the
32:15original one run there we go and if we
32:18can slice it by row we can also query
32:20the data frame so we can do df2 and this
32:23is a little different because i'm going
32:25to create an array within an array and
32:27in this case we're going to look at oh
32:29let's do
32:30age comma
32:33visits
32:34so look at the different format in here
32:36we have 1 to 3 so we've done this by
32:39slicing by an integer value and then on
32:42here i've done df2 age comma visits in
32:46an array and when i run this
32:49you can see that we get just these two
32:51columns on here we get age and visits so
32:53it's a quick way to select just two
32:55columns or select number of columns
32:56you're working with
32:58and if you stop there we did the slicing
33:00almost identical to slice is i location
33:04which uses the integer location one
33:06comma three
33:07there's a push in pandas to move to this
33:10particular setup instead of doing just a
33:13regular slice
33:14and that's because this can be confusing
33:16when we slice one to three and then we
33:19select age and visits
33:21so there is a push to go ahead and move
33:23to an i location which does the same
33:26thing you can see here bc it's the same
33:28as up above there's also copy command so
33:31we can do df3 equals df2 copy we're just
33:35going to create a straight copy of it
33:37and of course if we do df3
33:40it'll be the same as a df2 on there so
33:42df3 equals df2.copy
33:45and then let's do df3 dot is null so
33:49we're looking for null values and this
33:52will return a nice map and you'll see
33:54that everything is false except when you
33:56go up here under the cat or h they had a
34:00null there and so if we go down a couple
34:02up here also underneath of let's see the
34:04dog okay there's a bunch of nulls in
34:06here there's d up here so let's look at
34:08d down here and you'll see false true
34:10there it is there's our null value so we
34:12can create a quick chart of null values
34:14you can use this to do other things we
34:16can leverage that null value to maybe
34:19take an average or something and fill
34:21those null spaces with data and we can
34:23also modify the location so here's our
34:26df3
34:28location
34:30and notice this is location not eye
34:32location ilocation has i for integer
34:34location uses the
34:37in this case the variables on the left
34:39and what we can do on here and we're
34:41going to set this equal to
34:441.5
34:46and then let's um i'll pick a spot
34:49let's go back up here where we had let's
34:51do f
34:52a just let's see what are we looking at
34:54oh here we go let's do f and h
34:57and up here f is set to age of 2.0 and
35:01we find out that that's incorrect data
35:03so we go ahead and switch to df3
35:05equal and then we're going to print out
35:06our df3
35:08and if we go to f and age it is now 1.5
35:12so we're just changing the value in the
35:14df3 and this is changing the actual data
35:16frame remember a lot of our stuff we do
35:18a slice
35:20and like it returns another data frame
35:23this changes the actual data frame and
35:24that value in the data frame
35:27so we've covered location and eye
35:30location is null making a copy here's
35:33our eye location which is equivalent of
35:35a slice and also selecting columns
35:38so now we want to dive just take a
35:40little detour here and let's look at df3
35:43means
35:45and this is kind of nice because you can
35:46do this you can either do this by as you
35:48can select a single column here by the
35:50way you can just add the column
35:51selection right here like we did before
35:53so we could have age
35:55look up the mean that just creates a
35:57series if i run that there's our age
36:00but if i take that out instead of
36:02selecting it we can do the whole setup
36:04it has age and visits so why doesn't it
36:07have priority or animal
36:09well those are not integers so it's
36:11really hard
36:12they're non-numerical values so what is
36:14the average i guess you could do a
36:16histogram which probably will look at
36:18that later on but the only two things we
36:20can really look at is age and visits and
36:21we have
36:22the average or the mean on the age is
36:253.375
36:26and the mean on visits is 1.9
36:30and let's do df3
36:34visits we'll go ahead and steal the
36:35visits again
36:37and remember all those different
36:38functions we looked at for a series well
36:40we can do those here we can do the sum
36:43so if we run that we'll see that these
36:45sum up to 19
36:47we could also look up minimum if you
36:49remember that from before the minimum is
36:51one
36:52max
36:53so all that functionality is here
36:55i'll just go back to summing it up and
36:57adding it all together so real quick
36:59we've shown you how to take the series
37:03operations and put them into the data
37:06frame and then we can actually this is
37:08interesting one we can just do df3 sum
37:11run and you'll see the different
37:12summations on there
37:14it just combines them i like the way it
37:16just combines the strings on there for
37:18priority and animal we've looked at is
37:20null we've also looked at copying along
37:23with the different slices which we
37:25talked about earlier so let's talk about
37:26strings let's dive into the string setup
37:29on there and let's go ahead and create a
37:31string series string equals pd series
37:34and we just put it right in there we
37:36have a c d a a b a c a popped in a null
37:40value cow and al i don't know why they
37:43picked cal and al in the background
37:45someone must like those animals and of
37:47course we can just do string if we run
37:49that you'll see
37:51leave the r out we'll get an error but
37:52if we put it in there you'll see that we
37:54have a simple series 0 a 1 c 2 d and it
37:57automatically indexes it 0 to 8.
38:00and then we can go string dot lower so
38:04when we're talking about our data frame
38:06in this case or our data series string
38:08in this case we use the string
38:11function str and we're going to make it
38:13lower and if we go ahead and put the
38:15brackets on there and you'll see that
38:17we've gone from capital a capital c so
38:19on to abc and baca cba cow al they were
38:24all lower case already and of course if
38:26you want to go lower you can also do
38:29upper we'll go ahead and run that and
38:31you can see we now have acd aaa baca
38:34everything's capitalized except for the
38:35null value which is still null all right
38:37so we looked at a few basic string you
38:39can see that string functions upper and
38:41lower
38:42we're going to jump into a very
38:44important topic i'm even going to give
38:46it its own
38:47header on here because it's such an
38:49important topic what do you do with
38:51missing values
38:52panda has some great tools for that so
38:54we'll dive into those we'll call we'll
38:56work with df4 and if you remember the df
38:59copy from above we're just going to make
39:01a copy of df3 and let's just take a
39:03quick look at the data we're working
39:05with oops df3 forgot the 3 on there
39:08there we go
39:09so here we have our cats snakes and dogs
39:13hopefully not all in the same container
39:15because that would be just probably mean
39:16to all of them so we made a copy we're
39:18going to be working with df4 and the
39:20reason we made a copy is we want to go
39:22ahead and fill the data and we just
39:24simply do fill in a and we're going to
39:27give it the value we want to put in
39:28there we'll give it the value 4. so i
39:30can run in here and you'll see now that
39:32df4
39:34now has where the n a was it's filled
39:36with the value of 4. same thing down
39:38here
39:39a lot of times we'll compute the mean
39:42first so i might do a mean
39:45age
39:46equals df4 and then we want to go ahead
39:49and do age
39:53and dot mean
39:56and then i'll do something like this df4
39:59i only want to select the age and i want
40:01to fill that
40:03with the mean
40:04h
40:05and i run in there and you'll see that
40:07our df4h now has the means in there just
40:12a quick way of showing you how you can
40:13combine these
40:15let me go back to our original one there
40:17we go and run that
40:19and keeping with good practices df5
40:22equals df three dot copy
40:26and we'll print our df5 which should be
40:28the original one
40:30and then on the df5 we can now drop our
40:34missing data
40:35i'm going to simply drop in a and we're
40:38going to use how equals any so i'm going
40:40to drop any row that has missing data in
40:43it and you'll see we had d here with
40:45missing data and h
40:48and then let's go ahead and see what df5
40:50looks like when we do that
40:54there we go and there it is d is gone
40:55and so is h so we create a new data
40:58frame off of this missing those values
41:00now if you have a lot of data dropping
41:03values is a good way to take care of it
41:05because you don't miss some data if you
41:07have not a whole lot of data you're
41:08working with like the iris data set or
41:10something like that or something small
41:12you want to start trying to find a way
41:14to fill that data in so you don't lose
41:16your computational power the data you
41:17got
41:18so just a quick look at processing null
41:21values
41:22or missing values you can fill them
41:25usually with the means some people use
41:27medium or the mode there's different
41:29ways you can fill it one way is means
41:32and we can also just drop those rows
41:34those are the two main things we do with
41:36missing data
41:38here we go uh we're going to cover next
41:40this is i so love data frames for this
41:44file operations
41:46it saved me so much time
41:48because they have so many different
41:50tools for bringing data in and saving
41:52data
41:53so we're looking at the data frame file
41:55operations it's really streamlined i
41:58don't know how many times they'll go on
41:59to different data downloads and they'll
42:01have panda download standard on there
42:04just because it's so widely used so
42:06let's start with the most common file is
42:09a csv so we have df3 to csv or animal
42:13and let me just show you with the
42:14folders going into
42:16right now i have some untitled and a few
42:19things in here but nothing labeled
42:20animal so we go ahead and run this
42:23and this is now saved the animal to
42:26my hard drive and you can now see the
42:28animal folder up here and if i let's do
42:30edit with a notepad oh let's open up
42:32with just a regular notepad there we go
42:34or wordpad if i open that up you can see
42:37it's comma separated our titles they
42:39don't have an index on the categories on
42:41the top and the index comma then all the
42:43different data is separated by commas
42:46standard csv file on there and if we're
42:49going to send it to csv and notice the
42:52format is dot 2 underscore csv
42:55and it's just the name of the file we're
42:57sending it to you can also put the
42:59complete path by default it's going to
43:01go whatever the active directory this
43:02program is running on that's why those
43:04other folders are in there so we have
43:06our df3 to csv and then if we're going
43:08to put it in there we want to also get
43:10it back out and we'll call this one df
43:12underscore animal equals pd read
43:15underscore csv
43:18i always have to remember is two
43:19underscore csv and read underscore csv i
43:22always want to do like a capital in
43:23there and not the underscore we're going
43:26in here again it's the active directory
43:27so if i now do print out my df animal
43:31and let's just do the ahead we only want
43:33to look at the first three lines so if i
43:35go ahead and run this
43:37we'll see the first three lines and they
43:38should match up here what we saved to
43:40our csv
43:41so very easy to save and import from our
43:44csv files on here
43:47and it turns out
43:49df3 also has a two excel they actually
43:52have a lot of different formats but you
43:54know old school
43:56excel was real popular for so long still
43:58is we can go ahead and save it as animal
44:00dot xlsx we're going to call the sheet
44:03name sheet1
44:05and then i can also do df we'll call it
44:07animal two
44:09animal two and this one's gonna come
44:11from and the same format on here there
44:14we go so we still have our animal xlsx
44:17the sheet one that's where it's coming
44:19from index columns equals none so we're
44:22not going to we're going to suppress the
44:23indexing on the columns n a values and
44:26it'll just assign that 0 on up on your
44:28indexes so if it says index columns
44:30equals none that's what it does and then
44:32we've added null values because there's
44:33no values in here and we want to just
44:35make sure that they're marked as n a and
44:38we'll go ahead and just print out the
44:40animal animal two there we go and let's
44:43run that let's make this let's just do
44:45the whole thing so we'll go ahead and
44:46run that
44:47and it probably doesn't help that i
44:49completely forgot the read so animal 2
44:52equals pd.read
44:55excel there we go excel so now we go
44:58ahead and run it
44:59and what we expect is happening here we
45:02have the same data frame on here and if
45:04i flick back to my folder you can now
45:06see that we have the animal one of these
45:08is in excel and one of these is a csv on
45:12here and so there's our two file types
45:14on there and they have other formats
45:15these are just the two most common ones
45:17used and
45:18i don't know how many times i've had
45:20stuff from excel i need to pull out if
45:21you've ever played with excel it's a
45:23nightmare in the back end
45:25because of the way they do the indexing
45:28so this just makes it quick and easy to
45:30pull in an excel spreadsheet
45:32so we looked at two different ways to
45:34bring data in and save it to files we've
45:36looked at all kinds of different ways of
45:38manipulating our data set and slicing it
45:41and creating it for our data frame let's
45:44get in there put your visualization
45:47always a big thing at the end because
45:49one it lets you check to see what you
45:52did make sure it looks right and then
45:54also if you're going to show somebody
45:55else it makes it very clear what's going
45:57on if they see something visual so this
45:59is where a really important part of data
46:01science is so let's go ahead and bring
46:04in our tools we're going to do import
46:06numpy as np we want to make sure we have
46:08our amber sign matte plot library in
46:12line this just lets jupiter know that
46:14we're going to print it on this page if
46:15you're using a different ide you don't
46:17really necessarily need that but this
46:19does help it displays correctly in
46:21jupiter notebook and if you remember for
46:23earlier we could create a
46:25we're going to call it ts we're going to
46:26create a pandas which are cute cuddly
46:28creatures versus a pandem short for
46:30pandemonium no so we have ts equals pd
46:33series and we're just going to create a
46:35random
46:36setup of 50. we'll do an index we'll set
46:38it equal to the pandas date range today
46:42periods equals 50 so the 50s should
46:44match and i want you to notice something
46:46here i did not import the map plot
46:49library why because it's already in
46:52there pandas already has its built-in
46:54connection and interface with matplot
46:56library so you don't have to import it
46:59and we'll go ahead and do ts
47:01equals ts dot
47:03cumulative sum we're going to do the
47:05cumulative sum
47:06so a little reformatting there and we'll
47:08go ahead and plot it and let's take a
47:09look at what that looks like
47:11so we have a nice graph here we have the
47:13dates on the bottom we set this up so we
47:15have a nice range between in this case
47:17minus four to looks like about two maybe
47:20or one minus four and one so what we've
47:23done here we plotted a basic series just
47:25a single row of data and we've set
47:27indexes on there but we can also do the
47:30whole data frame on there and let's see
47:32what that looks like
47:33so first let's go ahead and create the
47:35data frame we have here random numbers
47:37so we're going to do 50 by 4 and then
47:40we'll go ahead and create columns a b x
47:42and y
47:43just because we can index is the
47:45ts.index on there so we're going to use
47:47the same index as before
47:49just to keep it nice and uniform we've
47:50already generated the dates to go with
47:52it and then we can do just like we did
47:54with the series
47:56we can also do with the data frame
48:00df equals df cumulative sum
48:03so we're going to sum the whole data
48:05frame and then we'll do simply df plot
48:07and let's put that in and let's go ahead
48:09and run this and look how easy and quick
48:11that was to generate a nice graph with
48:14all the different data on there so we
48:16have our shared index we have the shared
48:18columns and then we have the different
48:20data from each one that we can easily
48:22look at and compare so very quick way of
48:24displaying data you can imagine if you
48:26were working in oh i think i mentioned
48:28stock earlier because i've been doing
48:29some analysis of stock lately so you'd
48:32have your date down here and then you
48:34would have stock a stock b stock x y
48:37whatever it is and you can put them all
48:38on one chart and see how they
48:41what they look like next to each other
48:43and this isn't too far off from what
48:45some of those graphs looks like and this
48:46is just randomly generated so stock has
48:48a lot of randomness in it which is one
48:50of the reasons i actually play with it
48:52for doing some of my models on for
48:53testing them out
48:55now there are a lot of features in
48:57pandas so we're going to show you one
48:59more thing on here there's some of the
49:01things like i didn't go too deep we
49:02looked at the top two for importing data
49:05from a csv and from an excel spreadsheet
49:07showed you how to quickly plot the data
49:09there's more settings in there you can
49:11do
49:12we're going to do one more thing down
49:13here and this is kind of a fun one
49:16change this to a markdown and run that
49:18so how would you remove repeated data
49:21using pandas
49:23and this is where you have a data set
49:24that comes in and maybe it's feeding
49:27from one location and instead of noting
49:30that it's repeated the date like oh
49:32let's go back to stocks that's a good
49:34visual we have the stocks from the 23rd
49:37and it adds another row and it's the
49:39same row it's importing the 23rd again
49:42and again so now you have that data
49:44repeated three times and you need to go
49:46back and figure out how to get rid of it
49:48how do you track that down
49:50so let's start by creating a quick
49:51database or data frame not a database i
49:54keep saying database it's a data frame
49:56and we'll just make this data frame has
49:58using our dictionary going in this data
50:01frame only has one data series in it
50:03which is fine so if we do df to print it
50:06out you'll see a
50:08one two two two two four five five six
50:10seven so on so how would you remove that
50:12well there is a neat feature in data
50:15frames called shift
50:17along with another feature that lets us
50:20select just certain date information and
50:23we'll go with
50:25the location function put that in
50:27brackets remember that from above
50:28location and then in the location let me
50:31just spread this out a little bit so
50:32it's really easy to read in fact i'm
50:34going to go upscale on that since we're
50:36doing some a little bit more complicated
50:38here
50:40what you can see on this on the location
50:43is i have dfa dot shift
50:46so this is going to shift up one by
50:48default you can actually change this to
50:50two or three you can even do a minus 1
50:53and it shifts the other way but it's
50:55going to shift up by 1 by default that's
50:57going to say if that does not equal
51:00df of a
51:01then we want that if you look down here
51:03we had 1 2 2 2 2 2 when we run this
51:06logic on here and we do the shift
51:10it now gets rid of all the duplicates so
51:12we went from one two two two two four
51:14four five whatever it was here it is one
51:16two two two four four four five five
51:18five six six six two one two four five
51:20six seven eight and you'll see on the
51:23index it just deletes them out of there
51:25so the index stays the same obviously
51:27you don't want the dates to change if
51:28you're working with an index dated setup
51:30so it just deletes those duplicates out
51:32of there this is just a quick way to
51:35introduce you to
51:36one the fact that you can add logic
51:39gates into here and two the eye location
51:42allows you to use shift so there's the
51:45shift function and then the eye location
51:47selects that based on true or false
51:50wow so we've actually covered a lot
51:52today in pandas we've really covered
51:55into the basics of selecting your
51:56different series out of your column out
51:59of your data frame how to index rows how
52:02to slice how to plot
52:04hopefully you'll take this beyond that
52:06and start combining these different
52:07things and you can create long strings
52:09and really explore your data generate
52:12some nice graphs if you're in jupiter
52:14notebook it's a great demo to show
52:16others
52:17and i didn't know this about jupiter
52:19notebook you can do this in jupiter
52:20notebook and then you can download and i
52:23always i never really look too closely
52:24at all the downloads which you now load
52:27as an html and post it to your blog so
52:29it's got a neat feature in there but any
52:31of this is really powerful tool all of
52:33this is really powerful tools for doing
52:34your data science
52:36if getting your learning started is half
52:38the battle what if you could do that for
52:40free visit scale up by simply learn
52:43click on the link in the description to
52:44know more today we're going to study the
52:47matte plot library and the python code
52:50so what's in it for you what is matte
52:53plot library types of plots plotting
52:56graphs and sub graphs adding a graph
52:58inside a graph graph parameters title
53:01label legend line graphs line types
53:04color and transparency canvas grid and
53:07axis range 2d plots scatter step bar
53:10fill between radar chart histogram
53:13contour image
53:143d surface image and then we'll hit a
53:16practice example pie chart
53:18so let's start with what is matte plot
53:21library map plot library is an open
53:23source drawing library which supports
53:25rich drawing types it is used to draw 2d
53:28and 3d graphics
53:30and there are so many packages in the
53:33matplot library we're going to cover the
53:34basics and there are so many packages
53:36that sit on top of the maplight library
53:38that we can't even cover them all today
53:41but we'll hit the main one so you have a
53:42good understanding of what the matplot
53:44library is and what the basics can do
53:47you can understand your data easily by
53:49visualizing it with the help of matplot
53:51library you can generate plots
53:52histograms bar charts and many other
53:55charts with just a few lines of code and
53:57here we have some basic types of plots
53:59you can see here that we'll go into we
54:01have the bar chart the histogram boy i
54:04use a lot of histograms in my stuff
54:06scatter plot line chart pie chart and
54:09area graph
54:10let's start plotting them and to do this
54:13i'm going to be using jupiter notebook
54:16you can use any of your python
54:18interfaces for programming or scripting
54:20and running it of course we here really
54:22like the jupiter notebook for doing
54:24basic a lot of basic stuff because it's
54:26so visual and in our jupyter notebook
54:28which opens up in this case i'm using
54:30google chrome you can go up here to new
54:32and we'll create a new python 3 and set
54:35that up
54:36if you're not familiar with jupiter
54:38notebook we do have a tutorial that
54:40covers some of the basics of that and
54:41you'll look at any of our tutorials i
54:43usually cover a number of them showing
54:44how to set up jupiter and anaconda i
54:47myself use jupiter through anaconda in
54:49fact let's go ahead and open that up and
54:50just take a look let's see what that
54:52looks like you can see your anaconda
54:54navigator if you install it it will
54:56automatically install the jupiter
54:57notebook but that also installs a lot of
54:59other things i know some people like the
55:01qt console for doing python or spyder
55:04i've never used them i actually use
55:06notepad plus plus as one of my editors
55:08and then i use the jupiter notebook a
55:10lot because it's so easy to have a
55:12visual while i'm programming and even
55:14simple script in python i'll take it
55:17from the jupiter notebook and then do a
55:19save as you always go under file and you
55:21can download as a python program so that
55:24will download it as an actual python
55:25versus the ipython that this saves it as
55:28so let's go ahead and dive in and see we
55:29got going here and let's go ahead and
55:31put matplot library tutorial and i'm
55:34going to turn this cell into a mark down
55:36so it doesn't actually run it
55:38you can see it has a nice little title
55:39there that's all jupiter notebook
55:42and then from matplot library
55:45let's
55:46import
55:48pi lab
55:50back one and then let's go ahead and
55:52just print
55:53we'll go pi lab
55:55and the version let's go ahead and run
55:57this so we're going to import our pi lab
55:59module from the matplot library and we
56:01find out the word version 1.15.1
56:05always important to note the version
56:07you're in probably i was reading an
56:08article that said the number one thing
56:10that python programmers struggle with is
56:13remembering what version they're working
56:14in and making sure that they're going
56:16from one platform to the other with the
56:18same version and if we're gonna graph
56:20things i think we need some data to
56:21graph so we're gonna import numpy as np
56:25now if you're not familiar with numpy
56:27definitely go back and check out our
56:28numpy tutorial there's so many different
56:30things you can do with it dealing with
56:32reshaping the data and creating the data
56:34we're just going to use it to create
56:35some data for us
56:37and there is a lot of ways to create
56:38data but we're going to use the np.line
56:41space
56:42so we're going to create a numpy array
56:44and the way you read this is we're going
56:46to create numbers between 0 and 10 and
56:49we're going to create 25 of these
56:50numbers so we're just going to divide
56:51that equally up between 0 and 10. and if
56:54we have x coordinates we should probably
56:55have some y coordinates and we'll do
56:57something simple like x times x
57:00plus 2 and let's just take a look we're
57:03going to print x
57:05and print
57:06y
57:07let me go ahead and run this
57:09and let's see we got going on here so we
57:11have our x coordinates which is 0 0.4
57:140.83 etc and you can look at this as an
57:17xy plot so we have 0 we have 2. so we
57:21have 0.416 we have 2.17 and just as a
57:26quick reminder we're going to do print
57:28np array x comma y dot reshape 25 comma
57:312. and the reason i want to do this is i
57:33want to show you something here
57:35a lot of times a program returns x comma
57:38y and it's an array of x comma y x comma
57:41y x comma y
57:43and so when you're working with the pi
57:46plot
57:47you have to separate it out and reshape
57:49it so if i start off with pairs like
57:51this i can reshape them if i know
57:53there's 25 pairs in there i can switch
57:55the 2 and the 25 and this is kind of
57:58goofy but we'll do it anyways reshape so
58:00i'm going to reshape my 25 by 2 back to
58:032 by 25
58:05and if i run that you'll see i end up
58:07with the same output as the x y the two
58:10different arrays in here
58:12and this is important that we want x and
58:14y separate
58:16again that's all numpy stuff but it's
58:18important to understand that this is a
58:19format that matplot library works with
58:22it works with an array of x's and they
58:24should match your array of y's so each
58:26one has 25 different entities in it
58:29and then for our basic plotting of this
58:32data it only takes one command to draw
58:35graph of this data and so we use our
58:38from up here where we imported pi lab we
58:40take our pi lab and the key net under
58:43there is plot for plotting a line and
58:46then we want our x coordinates and our y
58:48coordinates and we'll throw in r and the
58:52r simply means red so we're going to
58:54draw the line in red let me go and run
58:55that
58:57you can actually switch this around if
58:58you wanted to do different there's b
59:00for blue we have a lot of fun yellow
59:02hard to see yellow there we go but we'll
59:05go ahead and stick with red
59:06run
59:07and when you're doing presentations with
59:09these try to be consistent you know if
59:12the business and the shareholders send
59:14you a
59:15spreadsheet and they have losses in red
59:18use red for losses in your graph
59:21try to be consistent use green for
59:23profit for money you don't have to
59:24necessarily use green but whatever
59:25they're using whatever the company's
59:27using try to mirror that that way people
59:29aren't going to be confused if you
59:30switch your data around every time one
59:32graph has red for loss and one graph has
59:35blue for loss it gets really confusing
59:37so make sure you're consistent in your
59:38graphs and your coloring and something
59:41to know because we're going to cover
59:42this in a minute this is your canvas
59:44size so we have a canvas here and what
59:46we're going to do next is we're going to
59:48look at sub graphs okay
59:51so let's take our pi lab and create a
59:54sub plot
59:56and one of the things also to know when
59:58we're working with the matplot library
1:00:01i'm not setting when i do this this is
1:00:04my drawing canvas the pi lab so once
1:00:06i've imported the pi lab i'm drawing my
1:00:09images on there very important to know
1:00:11and with the subplot we're going to give
1:00:13it some different values
1:00:16and we're going to represent by rows
1:00:17columns and indexes
1:00:20and let's do one two one so it's going
1:00:23to be the first row second column and
1:00:25the index is like you can stack your
1:00:28graphs and things like that we don't
1:00:29worry too much about indexes but rows
1:00:32and columns we want to go ahead and use
1:00:33row one and column two and if we're
1:00:35going to have one object we should
1:00:37probably have two but before we do that
1:00:39we have to plot data onto the subplot so
1:00:43the order is very important and we're
1:00:45going to stick with our x comma y
1:00:47and let's do this we're going to add in
1:00:50a third parameter here remember we did
1:00:52red we're going to add shorthand dash
1:00:54dash for dash lines so this plots the
1:00:57data into row one column two
1:01:00and if we're going to do that let's do
1:01:02up another one pi
1:01:03lab.subplot and if we're going to do row
1:01:06one let's do
1:01:08column two and index two
1:01:11and this time we're going to add g for
1:01:13green and this denotes a style and if
1:01:16we're going to set up our pilab
1:01:18subplot there we go all right lab we've
1:01:21got to go ahead and plot that pi
1:01:23lab plot
1:01:26and instead of x y we want y comma x
1:01:29oops i messed up this is in the wrong
1:01:31spot there we go we'll move that down
1:01:33here real quick because that goes in the
1:01:35plot part so the subplot tells it the
1:01:38row column and index and the pi plot
1:01:41tells it what data in this case we
1:01:43switched them and the color and then the
1:01:45style shorthand now let's go ahead and
1:01:47run that
1:01:48and you'll see it takes this canvas
1:01:51splits it in two and now we have two
1:01:54different graphs and we have the red one
1:01:56with dashed lines and we have the green
1:01:58one which is has a little stars going up
1:02:01and if we take this and let's just um
1:02:03just for fun let's change this and run
1:02:06that with an index of one it puts them
1:02:07both on the same index and also gives me
1:02:10a warning because it's a strange way of
1:02:12doing two subplots there's depreciated
1:02:15there's another way to do it but most
1:02:17people just ignore that warning because
1:02:19it's not going to go away anytime soon
1:02:21now that's using the same setup what
1:02:23happens if we do
1:02:24instead of
1:02:26this let's change the column on here and
1:02:28find out what happens
1:02:29and if we do the column
1:02:32it didn't really like that on the setup
1:02:34it just disappears so let's keep our
1:02:36column as two and let's change the row
1:02:39on the second one to two
1:02:41and run that
1:02:43and you'll see again it kind of squishes
1:02:45everything together and causes some
1:02:46issues so let's take the index so these
1:02:48need a unique index and you can see here
1:02:51where i made some changes i said row two
1:02:54and look what happens when i change to
1:02:56column two so i now have row two column
1:03:00two index two i squished it up here so
1:03:02you could put another graph underneath
1:03:04is what that does and there's all kinds
1:03:05of different things you really have to
1:03:07just play with these numbers until you
1:03:09get a handle on them because
1:03:12you know you have to repeat it 164 times
1:03:14according to cambridge university if
1:03:16it's completely new to you and you can
1:03:18see right here where we go three
1:03:21run there we go but you can see it takes
1:03:23a little bit sometimes to play with
1:03:24these and get the numbers right
1:03:25hopefully hit the wrong one that's why
1:03:26let's go three there three there run
1:03:29there we go now it's overlapping so i
1:03:30have this doubled over here on the right
1:03:33for now we'll just go ahead and leave
1:03:34this with the
1:03:36where we have column and row two and the
1:03:37two different indexes so they appear
1:03:39nice and neatly side by side
1:03:42and then as we just saw as we were
1:03:44flashing through them we can put them on
1:03:46top of each other
1:03:47and let me just highlight that and copy
1:03:49it down here
1:03:52paste it down there and here we have one
1:03:54two one and then we'll do one two one
1:03:56also for this one and that puts the two
1:03:58subplots directly on top of each other
1:04:01gives us that warning and you can see we
1:04:03now have two different sets of data
1:04:05graphed on top of each other and you can
1:04:07also see how it did the indexes since
1:04:10one of them is from 0 to 10 that's the
1:04:12green one on the x axis and the other
1:04:15one is from 0 to 10 on the y axis so it
1:04:17took the greatest value of either one
1:04:19and then used those as a shared value
1:04:22so let's
1:04:23next look at operator description and
1:04:26we'll go ahead and turn this cell into a
1:04:29markdown and run that so it looks nice
1:04:31so fig and you remember i talked about
1:04:33the canvas earlier i briefly mentioned
1:04:35it we're going to look a little bit more
1:04:36at the canvas later on but that's what
1:04:39the figure is fit we're going to add
1:04:41axes so we're going to initialize the
1:04:42subplot add the subplot
1:04:45in rows and columns and all kinds of
1:04:47different things with this you can do
1:04:49let's look at that code and see exactly
1:04:51what's going on and i want you to notice
1:04:53that there's fig which is the actual
1:04:55canvas in the matplot library and ax is
1:04:59commonly used to refer to the subplots
1:05:01so we're creating subplots you'll see ax
1:05:03equals plt subplot
1:05:06earlier we did the pi lab so let's go
1:05:09ahead and import pi plot from matplot
1:05:12library and we're going to do it as plt
1:05:15you'll see that a lot that's really the
1:05:17standard in the industry is to call it
1:05:19plt just like pandas as pd and numpy
1:05:22array as np certainly you can import it
1:05:24as whatever you want but i would stick
1:05:26to the standards and we're going to do
1:05:28the same graph as we did above
1:05:31with the pi lab but with the plt so if
1:05:34it looks familiar there's reason we're
1:05:36doing this because we want to show you
1:05:38how the figure part works and working
1:05:39with the canvas goes but we're going to
1:05:41do the same plot as we did before and
1:05:43we'll call it fig and we're going to set
1:05:44that equal to plot figure so there's our
1:05:47figure or canvas on there and let's
1:05:50create a variable called axes and we're
1:05:52going to set that equal to
1:05:53fig dot add
1:05:56axes
1:05:58and in this we're going to control the
1:06:01left right the width the height of the
1:06:03canvas from zero to one
1:06:05so we can go ahead and i'm just gonna
1:06:06put some stuff in there i got point five
1:06:08point one point eight point eight so
1:06:10when you're looking at this this is a
1:06:11zero to one or you could say fifty
1:06:13percent ten percent eighty eighty
1:06:15percent but it's a control it's going to
1:06:17control your left and your right along
1:06:19with the width and the height so the
1:06:21width and the height we're going to use
1:06:2280 percent and we're going to have like
1:06:24a little indent on the left and the
1:06:25right and this should look familiar from
1:06:27above x use dot plot x comma y
1:06:31and then let's give it a color how about
1:06:32red since we're recreating the same
1:06:34graph let's keep it uniform oops and it
1:06:36helps if i use a axis instead of ax es i
1:06:40don't know where that came from but this
1:06:42looks identical to the one we had up
1:06:43above so here's our axis plot x comma y
1:06:46of red
1:06:47same graph same setup but this time
1:06:50we've added a variable equal to the
1:06:52figure dot add axes so our plot figures
1:06:55our canvas our axis is what we're
1:06:57working in and then our axis.plot x
1:07:00comma y
1:07:01and again we can draw sub graphs we put
1:07:03that down here
1:07:06[Music]
1:07:09just like we did before and a little
1:07:11different temptation here we're going to
1:07:12fig comma axes equal
1:07:16plt.subplots
1:07:18and in here it's gonna be the number of
1:07:20rows
1:07:21we're gonna do one row
1:07:23and columns equals two so if you
1:07:25remember before that's what we did we
1:07:27had one row with two different graphs on
1:07:29it we're going to do the same thing but
1:07:31know how we did this here's our figure
1:07:33our canvas and our axes we're going to
1:07:35create actually two different axes we're
1:07:38going to create row one column two and
1:07:40so axis is an array of information so we
1:07:43can simply do
1:07:45for
1:07:46let's do x in axes
1:07:50this will look familiar x dot plot we're
1:07:52going to do x comma y we'll go ahead and
1:07:54make a red keep everything looking the
1:07:56same remember nice uniform graphs
1:07:58everything looks the same and if we go
1:08:00ahead and run this
1:08:02you'll see we get two nice side-by-side
1:08:04graphs so just as we had before the same
1:08:06look the same setup
1:08:08and just for fun let's change in columns
1:08:11to three we'll run that and now you'll
1:08:13see we'll have three on there and let's
1:08:15see if we make it a little bit more
1:08:16interesting we'll do in rows equals to
1:08:18two and you can see down here we're
1:08:19going to get in the tributaries is
1:08:21trying to scrunch everything together
1:08:23so it does have a limit how much stuff
1:08:24you can put in one small space that's
1:08:27important to know you can fix that by
1:08:28changing the canvas size which we'll
1:08:30look at in just a minute and there's
1:08:32other ways to change it on here but here
1:08:34we go we can do in rows 2 and columns
1:08:36equals 1. you can see two nice images
1:08:38right above each other we'll go back to
1:08:40the original one row two columns side by
1:08:44side left to right and
1:08:46we can also
1:08:48draw a picture
1:08:50or graph inside another graph
1:08:55and that's kind of a fun thing to do
1:08:56it's important to note that we can layer
1:08:58our stuff on top of each other which
1:08:59makes for a really nice presentation
1:09:01so let's start by fig we'll create
1:09:04another figure so we're going to start
1:09:05over again with our canvas we set that
1:09:07equal to plt.figure
1:09:11so there's our new canvas and let's do
1:09:13axes we'll call it axes one and two axis
1:09:16one equals fig dot add axes remember
1:09:19this from earlier
1:09:20and
1:09:21this here similar numbers we used before
1:09:24saying how big this axis is this figure
1:09:27and the axes is so this is going to be
1:09:28the big
1:09:30axes and let's do axes 2 equals
1:09:34another figure add axes and then point
1:09:36two point five point four point three
1:09:39and if we're going to do this they need
1:09:40data on them so let's go ahead and plot
1:09:42some data on our axes so axes one dot
1:09:44plot
1:09:45and we'll make this simply x comma y
1:09:48comma make it red and then let's go
1:09:50axes2 dot plot and let's reverse them y
1:09:54comma x comma green there we go doing
1:09:58what i told you not to do you shouldn't
1:09:59be swapping axes around and plotting
1:10:01your data in five different directions
1:10:02because it's confusing let's go ahead
1:10:04and run this and see what this looks
1:10:05like and then let's talk a little bit
1:10:07about this we talked about the 0.2.5.4.3
1:10:12let me just grab the annotation for that
1:10:14that's left right width and height
1:10:17so we have in here that this is going to
1:10:19be left right so here's our left is
1:10:21point one in point five and we you know
1:10:24what let's just play with this a little
1:10:25bit what happens when i change this to
1:10:27point one moves it way over to the left
1:10:29so there's our point one so we can make
1:10:31this point four run that there we go so
1:10:35you can see how you can move it around
1:10:36the branches on here point two
1:10:39point five is the
1:10:41left so that's our right so see what
1:10:43happens when we do point oh let's make
1:10:44this point one
1:10:46that actually is they had it down at
1:10:47left right i thought this was wrong it's
1:10:49actually how far from the bottom let me
1:10:51switch that on here bottom there we go
1:10:53so we had here on this we can go ahead
1:10:55and put that back to 0.5 and run that
1:10:57and this is 0.3
1:10:59let's make this 0.3 also and that is the
1:11:02width and then of course there's the
1:11:04height we can make that really tiny
1:11:05actually let's do 0.2
1:11:07let's run that and you can see it
1:11:09changes the height on there we make it
1:11:11even smaller 0.2 by 8.2 and as you can
1:11:15see you can get stuck playing with this
1:11:16to make it look just right it can
1:11:18sometimes take a little bit
1:11:20certainly once you have the settings if
1:11:22you're doing a presentation you try to
1:11:24keep it uniform unless it doesn't make
1:11:26sense for the graph you're working on
1:11:28try to keep the same colors the same
1:11:30position and the same look and feel
1:11:32and i mentioned earlier we can adjust
1:11:34the canvas size so this is from earlier
1:11:36i just copied it down below we're going
1:11:38to re-plot the same data we've been
1:11:39looking at
1:11:40and what we can do is we can change the
1:11:42figure size to 16 by 9. let me run that
1:11:45and show you what that looks like so it
1:11:46fills the whole screen and then if you
1:11:48are normally when you're working on the
1:11:50screen you don't worry too much about
1:11:51this but we can set the dpi to 300 run
1:11:56that
1:11:57there it goes this is your dots per inch
1:11:59and if you are doing an output of this
1:12:02and you're printing a hard copy you want
1:12:04the higher quality i would suggest
1:12:06nothing under 300 if it's a professional
1:12:08print you might get a little less than
1:12:10that but whenever i'm doing professional
1:12:12graphics and printing them out on
1:12:13something 300 dots per inch is kind of
1:12:16the minimal on there you can go a lot
1:12:18higher too but keep in mind the higher
1:12:20you get the more memory it takes the
1:12:22more lag time and the more resources you
1:12:24use so usually 300 is a good
1:12:27solid number to use your dots per inch
1:12:29and you can see it drills a nice it
1:12:31draws a nice large canvas here which is
1:12:3316 by 9 and then the dpi is 300 on here
1:12:36so it's a little higher quality and just
1:12:38out of curiosity i wonder how long it
1:12:39takes to draw something double that size
1:12:41600 and you can see here where at 600
1:12:45dpi it's going to take a while there it
1:12:47goes just because it's utilizing a lot
1:12:48more graphics on there and let me just
1:12:50go back to the 300 now we'll actually do
1:12:52let's do a 100 you're not going to see a
1:12:55difference on this because it is web
1:12:57based graphics are pretty low
1:12:59and up here you saw i did this with the
1:13:01plot figure this works the same if i do
1:13:04figure axes subplot figure size and then
1:13:07we'll go ahead and do axes
1:13:11dot plot
1:13:14x comma y comma we'll stick to
1:13:16red let's go ahead and run this and you
1:13:19should get almost the same thing here
1:13:21here's our
1:13:22axis on the subplot on here with the
1:13:25fixed size and the dpi let me take this
1:13:27all out let me just remove all that real
1:13:29quick run it again there we go now we're
1:13:32back to our original figure and let's
1:13:33look at some of the other things you can
1:13:35do with this
1:13:36one things we do is we can set a title
1:13:38for the axis so axis set title you'll
1:13:41see right here since i put this on the
1:13:42axis it's the main title for the whole
1:13:44graph and if you're going to have a
1:13:47title you should also label so we can
1:13:49label our x label and we can set our y
1:13:51label in this case we're just going to
1:13:52call it x and y keep it nice and uniform
1:13:55and if we run this you'll see that we've
1:13:57added a nice x label and y label whoops
1:14:00where'd they go and it turns out in this
1:14:02environment that you have to put it
1:14:04before the title so let me go ahead and
1:14:06put it before the title and there's our
1:14:08x y and then we run that and of course
1:14:10we can also do upper size a little bit
1:14:12you can see what's going on a little
1:14:13better so here we have x label x if you
1:14:15come down here you'll see our x label
1:14:17and our y label we can of course change
1:14:19this to x
1:14:21label you can change this to y
1:14:25and be whatever you want on here of
1:14:26course and our title graph there we go
1:14:29run so here we have our title graph our
1:14:31y label and our x label all set up on
1:14:34our nice little plot and then before we
1:14:36move on to the next section let's do one
1:14:39more thing on here we have a thing
1:14:40called the legend and we're gonna do
1:14:42we're gonna set our ax legend label one
1:14:45label two up here it's a format for it
1:14:48but let's go down here and actually use
1:14:50it i'm gonna do two different plots
1:14:52we're gonna have axes plot x by x times
1:14:55x squared and x cubed and if i run this
1:14:57you'll see it puts two nice graphs on
1:14:59the setup on there but it's nice to have
1:15:01a legend telling you what's going on so
1:15:04for the legend we can actually do axes
1:15:06since we have the two plots legend and
1:15:08on here we've created an array
1:15:11and we have y equals x squared y equals
1:15:15x cubed you can actually put this as
1:15:17whatever you want those are just strings
1:15:19and then location two and let's go ahead
1:15:21and run this and see what that looks
1:15:22like and you can see it puts a nice
1:15:25legend on the upper left hand corner
1:15:26location two we can do location three
1:15:30and run it and it drops it down to the
1:15:32bottom
1:15:33location one i can't remember where
1:15:35that's at there we go upper right so
1:15:37each one of these is a number that
1:15:38refers to the different locations on the
1:15:40screen zero kind of have to play with
1:15:43them or look them up to remember where
1:15:44they're at but they do work it just kind
1:15:45of moves around depending on where you
1:15:47want your legend out on there so on this
1:15:49section we cover the title of the graph
1:15:51the y labels and legends this is we're
1:15:54getting into some starting to look
1:15:56really fancy here so we now have
1:15:57something we can actually put out you'll
1:15:59see the title of the graph looks a
1:16:00little fuzzy so i might in a web setup
1:16:04put the dpi up a couple notches maybe
1:16:07put it at 200 100 might work fine just
1:16:11so you know something to notice on here
1:16:13when you're playing with these different
1:16:14things we had our subplots dpi equals oh
1:16:18let's do 200 and see what that looks
1:16:20like
1:16:21so you can see now it's a lot clearer
1:16:22it's also larger so it's a nice little
1:16:24feature you can throw in there with your
1:16:26dpi dots per inch
1:16:28so the next section is let's look at
1:16:30some graph features we're going to look
1:16:32at line color transparency size and a
1:16:35few more things on here and oops i
1:16:37forgot the main title so we have our
1:16:40figure in our axes equals our plot and
1:16:42subplots and i'm going to do a dpi
1:16:45equals 150 so the graph comes out nice
1:16:48and large and easy for you to see
1:16:50let's go ahead and do three plots on
1:16:52here we'll do x by x plus once which is
1:16:54going to be a straight line plot
1:16:57x plus x plus 2
1:17:00and axes dot plot
1:17:03x x plus three it looks like we're doing
1:17:06nearest neighbor setup or showing how it
1:17:08uh located data putting your lines on
1:17:11there between the nearest neighbors
1:17:12there we go so it draws a nice little
1:17:14graph with three lines on it one of the
1:17:16things we can do is we can control the
1:17:18alpha on this oops and you can actually
1:17:20see the um when they did these lines it
1:17:23automatically pulls in different colors
1:17:25for your setup so there's some automatic
1:17:27automatic things going on in there and a
1:17:28lot of times we do that comma r where
1:17:30we're going to do color equals red
1:17:32another notation on here let's go ahead
1:17:34and run this now we have a bright red
1:17:36line down there and with the map plot
1:17:39library you're not limited to red you
1:17:41can also use the one of many different
1:17:44color references as you see here with
1:17:46the pound sign one one five five dd
1:17:49which just is just blue and we can do
1:17:51the same thing with another color on
1:17:53here which it turns out to be green i
1:17:55can just as easily do this green
1:17:58blue oops there we go blue and run that
1:18:02and you'll see here we have red blue and
1:18:03green and what i want to do is i want to
1:18:05make this we're going to say what's
1:18:07called the alpha on this and we're going
1:18:09to set this equal to 0.5 so this is
1:18:12halfway see-through when i run this and
1:18:14it's almost going to look pink because
1:18:15you can see through it and let's change
1:18:18this just a little bit just to make this
1:18:19kind of fun let's square it there we go
1:18:22run it so now we have this nice square
1:18:24that comes up and you can see when it
1:18:26crosses it because i plotted these two
1:18:29lines after it and they have no alpha
1:18:31the red is behind those lines or in this
1:18:34case pink because we did the alpha
1:18:35halfway through so let's go ahead and do
1:18:37this alpha equals 0.5 and oh you know
1:18:42what instead of squaring it let's take
1:18:44it to the 0.5 power that'll be kind of
1:18:46interesting to see what that does we'll
1:18:48just go to keep it squared there we go
1:18:51and run that and let's go back and look
1:18:53at this where it crosses over and the
1:18:55first thing you see right here is on the
1:18:58blue it's kind of light blue now you can
1:19:00see how the two colors add together you
1:19:02get almost a purple on there so i can
1:19:05clearly see where the red crosses the
1:19:06blue line and then the green just blanks
1:19:09it over because i didn't do any
1:19:10opaqueness no alpha on there so this is
1:19:13great if you have lots of data that
1:19:15crosses over and you need to be able to
1:19:16track those lines better and we'll go
1:19:18ahead and do this .5 and we'll run that
1:19:21oops i did
1:19:22equals 0.5 let me go ahead and run that
1:19:25and so you can see right here now you
1:19:26can easily see the red line how it
1:19:28crosses the green and the blue down here
1:19:31and if we want to we can do this as the
1:19:33default is one solid so we can change
1:19:35this all to point eight let me just do
1:19:38that
1:19:39oops 58 there we go
1:19:41run
1:19:42oops i must have hit a wrong button
1:19:43there let me try that again i actually
1:19:45get rid of a bracket and let's go ahead
1:19:47and run that
1:19:48and we come down here and look at this
1:19:50you can still see where it passes behind
1:19:52them but the green dominates and the
1:19:53blue dominates because we're now at
1:19:55eighty percent instead of fifty percent
1:19:57when you can do less that's kind of fun
1:19:59although at some point the lanes kind of
1:20:00fade
1:20:02so 0.5 is usually the best setting on
1:20:04there we have a nice pastel here at 0.3
1:20:07and you can easily see where they cross
1:20:08over and just like you can play with the
1:20:11colors we can play with line width and
1:20:14you know let's do
1:20:15let's try dpi 100 and see what that
1:20:17looks like on my screen equals 100 and
1:20:20we'll go and just take our ax plot
1:20:25let's do four of these lines just you
1:20:27can see how they look next to each other
1:20:30real quick here there we go
1:20:33and if i run this they should all appear
1:20:35the same it automatically does different
1:20:36colors on there so let's do color equals
1:20:39blue
1:20:41forgot my quotation marks there we go
1:20:44and we'll go ahead and just make these
1:20:45all blue
1:20:46just for purposes of being nice and
1:20:48uniform and then what i want to do is i
1:20:50want to do the line
1:20:51width
1:20:53width equals
1:20:560.25 and let's just copy and paste that
1:20:59down here
1:21:03let's do equals one
1:21:08about 1.5 and let's do one let's make
1:21:11this equal to two let's see what that
1:21:13looks like and we do that you can see it
1:21:15goes from a very thin line a 0.5 a 1 our
1:21:181.5 and 2 which is twice the width of
1:21:21the one and if we're going to do
1:21:23different sizes we had different colors
1:21:25we had our alpha scheme let's take this
1:21:27whole thing here
1:21:29let's paste it down here and do another
1:21:30one
1:21:31but instead of line width
1:21:33let's look at styles and something to
1:21:35know here you can actually abbreviate
1:21:37this with lw so line width can also be
1:21:41point let's just do everything point two
1:21:43and let's set up a line style we'll do
1:21:45the first one dashes and let me just
1:21:46paste that down here
1:21:48so i'm not doing a lot of extra typing
1:21:50there we go
1:21:52take this out so we have our dashed
1:21:55we can do a dash dot we'll just do the
1:21:57dash dot here and a colon here there we
1:22:00go and there's a lot of different
1:22:01options we'll look at a few more as we
1:22:03go down for different ways of
1:22:05highlighting data
1:22:06but when you look at this we have
1:22:08everything as a line width of two and
1:22:10now we have a straight line we have a
1:22:12dashed line or a dot dash and a dot dot
1:22:15dot line
1:22:17and then another thing we can add on
1:22:19here is we're gonna do here's our ax
1:22:21plot and we did x let's do x plus um
1:22:25four so it goes right on the top and do
1:22:27color black line width 1.5 so it's a
1:22:29smaller line and we're going to take the
1:22:31line and we're going to set dashes so
1:22:33look i've changed some of the notation
1:22:36here for my line and my axe plot so i
1:22:38can set my line comma equal to x plot
1:22:41and then i can change the line settings
1:22:43this way and when i run this let me run
1:22:46that on here you'll see the 5 10 15 10
1:22:50creates a series of dashes
1:22:52that are buried in link
1:22:54link in this case they alternate between
1:22:56a short dash and a long dash we can play
1:22:58with these numbers curiosity always has
1:23:00me what happens when you play with the
1:23:01numbers just to see what they look like
1:23:04let's do this let's paste this down here
1:23:06i'll do two of these just because
1:23:08they're kind of fun to play with and
1:23:10let's change this from 10 to
1:23:123 and we're going to change this one
1:23:15from 15
1:23:17to
1:23:184. and let's run that
1:23:20and you can see the differences in the
1:23:22lines oops very a little bit confusing
1:23:25on there because i forgot to change the
1:23:27lines are all on top of each other so
1:23:29let me change that really quick here and
1:23:31let's run that and now you can see
1:23:33here's our original dashed line
1:23:34alternating when i change these numbers
1:23:36on the second one the very end value to
1:23:38three you can see now we have dashes of
1:23:41five let's see i'm going to guess this
1:23:43is a dash is a five skip ten
1:23:46dashes of
1:23:47skip three and then it goes back to the
1:23:49beginning dash is five dashes skip ten
1:23:52fifteen dashes skip three and of course
1:23:55the last one we just switched up a
1:23:56little bit it looks a lot more uniform
1:23:58because i'm using two sets of ten or if
1:24:00i did something like this and change it
1:24:02to 30 it really becomes pronounced as
1:24:04far as the distances between them and
1:24:07instead of 4 let's go oh let's put 30
1:24:09here also 30 by 30 there we go really
1:24:11pronounced on that one and let's look at
1:24:14one more important group for plotting
1:24:16our data and in this we're gonna here's
1:24:19our plot we started with with the x plus
1:24:21one x plus two x plus three and did it
1:24:23in blue on this one's three or four
1:24:26different blue lines
1:24:27and this property we wanna add the
1:24:29actual plots so you can see where the
1:24:31plots are on the graph and for that we
1:24:33might have marker equals o and if we run
1:24:36this you'll see it puts a dot for each
1:24:38of these and there's 25 dots because we
1:24:40have 25 x values so we actually have
1:24:43zero and each of the different values of
1:24:44x y are then plotted here with the dots
1:24:48and we don't want to just limit
1:24:49ourselves to dots
1:24:52you can also do
1:24:53plus sign that's another option
1:24:55dots is most common i'll actually like
1:24:57the dots the best if we do the plus sign
1:25:00you see it puts a nice crosshairs or a
1:25:02plus sign on there and we can do a
1:25:04marker there's a number of different
1:25:05markers you can use
1:25:07and i think this one was it s is another
1:25:09one
1:25:10which is a nice square and that's
1:25:12actually a good one that's for square o
1:25:14for
1:25:14period okay that's just kind of weird so
1:25:17you can see that probably on these
1:25:18markers another one is uh number one so
1:25:21if we run that you'll see we now have
1:25:23these little hatch marks and let's take
1:25:26oh let's just go with the o on this one
1:25:30by the way this works with square really
1:25:32nicely some stuff we're gonna do here on
1:25:33just a second let's do marker
1:25:37size equals two and change that to five
1:25:41and run that and you can see here it
1:25:43puts a nice little tiny dot versus uh
1:25:45the size dot here this is interesting
1:25:47because it said two i thought it would
1:25:48be bigger
1:25:49but if you do 0.5 it gets even smaller
1:25:53and let's just do 10 to see what that
1:25:55looks like run that looks huge so marker
1:25:58size a lot of these are dependent on the
1:26:00dpi and the setups there's things that
1:26:02switch around as far as the way the size
1:26:04shows up you got to be a little careful
1:26:06when you change one setting it can
1:26:07change all the other markers and then
1:26:09let's take our square on here
1:26:12and we'll do we have marker size so we
1:26:14also have marker base
1:26:17we'll set that equal to red of course we
1:26:19i mean change the so it's up one notch
1:26:22we'll run that whoops must have mistyped
1:26:24something on here and i did it's marker
1:26:27face color equals red and so when i run
1:26:30that you can now see i have the squares
1:26:32on there with the marker face color of
1:26:34course we can mix and match these
1:26:36come down here and we'll make this
1:26:38instead of let's make this plus seven
1:26:40and we'll make this
1:26:42size 15
1:26:45marker face color
1:26:48equals
1:26:49and we'll do what green just because
1:26:51there we go run very hard to actually
1:26:53see what's going on there still 25 dots
1:26:55they kind of overlap as you can see they
1:26:57print them over each other and of course
1:26:59if we really wanted to make it look
1:27:00horrible we could just make that really
1:27:02huge generally though you want something
1:27:05a little bit smaller and cuter we'll
1:27:07just try doing it this way there we go
1:27:09that's too small to even see the face so
1:27:12four
1:27:13you can start to see the face on there
1:27:14around four and maybe an eight eight
1:27:17might be a good number for this there we
1:27:18go eight again that all just depends on
1:27:20what you're trying to show and display
1:27:22so we've covered a lot of stuff here as
1:27:25far as our lines we've covered
1:27:28opaque with our alpha setting on there
1:27:31give us some nice pastels you can see
1:27:32how they overlap and how they cross over
1:27:35we covered the line with different size
1:27:38on there different formats for the line
1:27:40itself and these are all you can combine
1:27:42all these so you can have our line width
1:27:43equals two line style equals dash you
1:27:46can bring this down here also to the
1:27:47markers and then we added markers in
1:27:49just entered a circle a plus sign the
1:27:53square a little tick which uses a one
1:27:56then we had a marker size and a marker
1:27:58color face and we combine those you see
1:28:00we get a nice different series of
1:28:02representations we also briefly
1:28:04mentioned color where you didn't have to
1:28:06use like in here we used color black
1:28:09someplace up here and have to find it we
1:28:11use the actual number for the color as
1:28:14opposed to i changed them to red and
1:28:15blue so you get very precise on the
1:28:17color if you have a very specific color
1:28:19set that you need to match your website
1:28:21or whatever you're working on all those
1:28:23are tools in the map plot library so we
1:28:26have
1:28:26one more piece to formatting the graph
1:28:29so we want to show you and then we have
1:28:31two big sections we're going to go over
1:28:32the different graphs that they have
1:28:34along with a challenge problem so let's
1:28:36go in the last section we're going to
1:28:37look at is limits we're going to limit
1:28:40our data
1:28:41so this first primer is going to paste
1:28:42in there we're going to create our
1:28:43subplots one two so one row two columns
1:28:47we're gonna do a figure size of ten
1:28:49comma five this should all look familiar
1:28:52now since we've done a number of them
1:28:53and we're gonna go ahead and plot and
1:28:55this is an interesting notation you
1:28:56should notice here our axes zero so one
1:28:59we've used instead of you can just
1:29:01iterate through them but they're just an
1:29:02array so it's an array of zero is still
1:29:04the axes of the first axes out of two
1:29:07and we're going to plot x
1:29:08x squared
1:29:10x
1:29:11x cubed line with two so we're gonna go
1:29:14ahead and just plot two graphs right on
1:29:16top of each other without doing multiple
1:29:18plots on here and we'll set the grid
1:29:20equal to true one here let's go ahead
1:29:21and run that and you can see here are
1:29:24two plots with the x value going across
1:29:27and i'm going to do
1:29:28something similar and by the way as you
1:29:31can just if you look at it you can see
1:29:32the grid on there that's all that is
1:29:34easier to spot the data going across
1:29:36we're going to take the same
1:29:37data for axes one so we have our plot of
1:29:40x x squared x and x cubed line with two
1:29:44and this time we're going to take our
1:29:46axis one and do y limit
1:29:50it's actually set underscore y limit
1:29:52this is the y-axis so it's going to be
1:29:55an array of two two values and we'll do
1:29:580 comma
1:29:5960 i'm just making these numbers up the
1:30:01guys in the back actually made them up
1:30:03i'm just using their numbers and we're
1:30:05going to set the x limit
1:30:08and we'll set the x limit as
1:30:11don't forget our brackets there
1:30:13two comma five
1:30:15so it's the same data going in and but
1:30:17we're setting a limit on it let's go
1:30:18ahead and run that and let's see what it
1:30:20comes out of and here we have the y
1:30:22limit 0 to 60 so we're looking at just
1:30:25the lower part of this curve here up to
1:30:26here and we have the x limit 2 to 5. so
1:30:30that starts right here at 2 and you can
1:30:32see very different graphs this is kind
1:30:34of nice because you could actually put
1:30:35one of these on top of the other if you
1:30:37wanted to draw focus to one part of a
1:30:38graph remember how we did that earlier
1:30:40one inside the other but just a quick
1:30:42note you can easily limit your graph and
1:30:44re kind of reshape the way it looks
1:30:47quite easily and we can also add that
1:30:49grid down there if you want a grid
1:30:52we'll run that and add the grid in there
1:30:54oops i guess you have to do the grid
1:30:55beforehand
1:30:57switch that there we go sometimes the
1:30:59order on this is really important so you
1:31:01may double check your order when you're
1:31:02printing these things out it also helps
1:31:04if i change it to one so in this case
1:31:06might not be the order i wonder if i'll
1:31:08go back here as one there we go so it
1:31:10doesn't matter the order and grid but
1:31:12you can set the grid for easy viewing
1:31:14here nice setup on there but you can see
1:31:15how we can limit the data
1:31:17so let's start looking at some other 2d
1:31:20graphs and make this cell a markdown so
1:31:24we run it as a nice pretty title to it
1:31:26and let's go ahead and create some data
1:31:28with an np array we'll do
1:31:30zero to five on here there we go and
1:31:33let's look at uh four common
1:31:36graphs we'll put them side by side so
1:31:38we'll do a figure our axes equals plot
1:31:40subplots one four columns and then
1:31:43figure size hopefully it'll fit nicely
1:31:45on here it seems to do a pretty good on
1:31:47here and i'll go and just run that since
1:31:50we're in there run and you'll see i have
1:31:52my four blank plots on here
1:31:55and we'll start with axes of zero let's
1:31:58set title
1:32:02and we want this to be a scatter plot
1:32:05a scatter plot just means it has a bunch
1:32:07of dots on it so here's our axes of zero
1:32:11dot scatter easy to remember scatter a
1:32:13bunch of plots on there we'll do our
1:32:16n or we can do x or n there we go and
1:32:19let's go ahead and do axes set title
1:32:22scatter i've already did that we're just
1:32:24gonna do scatter
1:32:26that's how you do it on there notice how
1:32:28you create a scatter plot with simply
1:32:29with the scatter control and we'll do
1:32:31let's do the variable x
1:32:33x
1:32:34plus let's throw some randomness in here
1:32:36usually scatter plots are i have a lot
1:32:39of random numbers connected to them
1:32:40that's why they do them on there and so
1:32:42the bigger the x gets the bigger the
1:32:44randomness so 0.25 times the randomness
1:32:47and what we should end up doing here is
1:32:49with the scatter plot and you can see as
1:32:51you go up it just kind of has some
1:32:52random numbers and moves up and down the
1:32:54line
1:32:54but plus just the points so if you
1:32:56remember from back up here where we did
1:33:00marker
1:33:01this is plotting basically just the
1:33:02marker so it's a scatter plot
1:33:04probably less used is a step plot so for
1:33:07x is one we'll go ahead and do a step
1:33:09plot so you can see what that looks like
1:33:11and this time we'll use our n value
1:33:13instead of x we generated that n value
1:33:15up here and so for this we have n
1:33:17n times two r n squared n times two n
1:33:21squared line width equals 2 and if we
1:33:23run that it creates a nice step up
1:33:26let's see so we've got a scatter plot
1:33:28we've got a step
1:33:30plot let's do a bar plot
1:33:33and we'll use the same formula n n
1:33:35squared alignment centered because you
1:33:38can have them left or right with 0.5 and
1:33:41alpha if you remember correctly that's
1:33:43how opaque it is
1:33:44let's see what that looks like on there
1:33:46so we have some nice you can see here a
1:33:48nice bar plot it should look very
1:33:49similar to the step plot but colored in
1:33:53and we can change the width let's see
1:33:54what happens we do 0.9 run
1:33:57and if we take width out completely
1:33:59run that you can see it starts coming
1:34:02together on there and we can change the
1:34:04alpha we can take the alpha out too and
1:34:06run that so you know you have the solid
1:34:08colors and if we take out the center
1:34:11and run that
1:34:13everything you really can't see the
1:34:15shift on here because that's actually
1:34:16the default on this but these are common
1:34:18settings for the bar graph let me just
1:34:19put them back in there there we go
1:34:21alignment center and alpha now i can't
1:34:24say i've used the step craft very much
1:34:26there's certain other certain i guess
1:34:28domains of expertise require a step
1:34:30graph but the scatter plot and the bar
1:34:32graph very common especially the bar
1:34:34graph and we'll look at histograms here
1:34:36in just a minute so i use histograms a
1:34:38lot especially in data science but this
1:34:40is nice if you have very uh concrete
1:34:43objects somebody how many people wearing
1:34:45yellow hats that kind of thing but if
1:34:47we're going to do that let's go ahead
1:34:48and do the last one which i see a lot
1:34:51more in the sciences certainly using the
1:34:54data science but more like for mapping i
1:34:56saw publication on
1:34:59solar flares and they were discussing
1:35:01the energy and so filling in the graph
1:35:03gives it a very different luck so we're
1:35:05gonna do the fill between
1:35:07and it's just like you think it'd be
1:35:08it's fill between but with a underscore
1:35:10between them and we'll do x and x
1:35:12squared and x and x cubed and we'll do
1:35:15color green and alpha again in case you
1:35:18had other data you want to plot on there
1:35:20you can see it forms a nice squared
1:35:22coming up here and also if you look at
1:35:25the bottom one is your squared value the
1:35:26upper line is your cubed value and then
1:35:29it fills in everything in between
1:35:31if you remember from calculus this would
1:35:34be if you had like a car a motor an
1:35:36efficiency they would talk about the
1:35:37efficiency going up and the loss and
1:35:39you're looking for the space or the area
1:35:41between the two lines so it gives you a
1:35:43nice visual of that now let's look at a
1:35:45few more basic two dimensionals so we
1:35:48have our figure figure size on here
1:35:50we're going to do a radar chart to be
1:35:52honest i've never used a radar chart in
1:35:55business or in data science i get to
1:35:57find a reason to use one now so the
1:36:00first line for doing a radar chart we
1:36:02have to add axes and the figure and with
1:36:05this this actually creates our oh let's
1:36:08let's run it so you can see what it
1:36:10creates it creates a nice looks like
1:36:12you're on a submarine and you're
1:36:14tracking the hunt for red october or
1:36:16something like that and it needs all of
1:36:18these the polar is the fact that we're
1:36:20doing polar coordinates
1:36:220 0.6.6 has to do with the size if you
1:36:25take out any of these things and run
1:36:27them you get just a box
1:36:29if you take out the other half you
1:36:31pretty much get nothing in there and if
1:36:33you change these numbers and change them
1:36:35a little bit you can see it gets bigger
1:36:37they had 0.6 on here i'll go ahead and
1:36:39leave it as one because that's just kind
1:36:40of fun that's all about the size on here
1:36:42the height and the width and then let's
1:36:44create some data t equals np line space
1:36:48and this is 0 to 2 times np times pi so
1:36:52if you remember that is the
1:36:55distance across and we're going to
1:36:56generate 100 points
1:36:58so this is just a thing of data we're
1:36:59putting together then we simply do an ax
1:37:02dot plot and in this case let's do t
1:37:05comma t
1:37:06which would be a diagonal line on a
1:37:08regular chart and we'll give it a nice
1:37:10color equals
1:37:12blue
1:37:13and line width equals three let's see
1:37:16what that looks like and we can see here
1:37:18a spiral coming out remember this would
1:37:20be just a diagonal line on a regular
1:37:22chart what happens if we take this and
1:37:24instead of t
1:37:26times 0.5 there we go
1:37:28and you can see it slightly alters the
1:37:30way it spirals out we could do t times
1:37:32two spirals out a little quicker so it's
1:37:34kind of just a fun i've like i said i've
1:37:36never used a
1:37:37radar chart it's a column but you can
1:37:39always think of radar submarine kind of
1:37:41looks like one of those or an airplane
1:37:44and none of this would be completed if
1:37:45we didn't discuss histograms oh my gosh
1:37:48do i use a histogram so much and we'll
1:37:51use our numpy that we have set as np to
1:37:54generate oh looks like we have a hundred
1:37:56thousand variables we're going to set
1:37:57equal to n and of course we create our
1:37:59figure and our axes subplots 1 2 figure
1:38:02size 12 14. so we're going to look at
1:38:04two different variations of the
1:38:06histogram and we'll set a title default
1:38:09histogram set our title there and then
1:38:11this is simply
1:38:13hist for histogram and we'll just go
1:38:16ahead and put in our n in there and let
1:38:18me run this and see what that looks like
1:38:21and let's talk about what is going on
1:38:23here so we generated an array here of
1:38:26data 1000 random arrays it looks like
1:38:29they're mostly between minus four and
1:38:30four
1:38:31and then it adds up each one it says
1:38:34zero you have thirty five thousand that
1:38:37are zero so that's what's most common on
1:38:39here and we have twenty thousand that
1:38:41are somewhere in this range right here
1:38:43between the minus two and well it looks
1:38:45like one and minus two and somewhere
1:38:47between zero and one there's thirty
1:38:49thousand numbers so all this is saying
1:38:52is this is how common these variables
1:38:54are and this gives you this point in so
1:38:56many directions when you're looking at
1:38:57data science to go ahead and run your
1:38:59histogram so you should always have your
1:39:01histogram and you can always put limits
1:39:02and all the other different things on
1:39:04your array just like you did on the
1:39:05other graphs on there and then we're
1:39:07going to do a cumulative detailed
1:39:09histogram
1:39:10and all it is is a histogram let me just
1:39:12do that
1:39:14and we set cumulative equal to true
1:39:18and bins equal 50. and i really want to
1:39:21highlight the the cumulative equals true
1:39:24is important but we can now choose how
1:39:26many bins we have in the first one it
1:39:29kind of selected them for us in this
1:39:31case let me go ahead and run this and
1:39:33you'll see it has the prints of data out
1:39:35for us and here's our whoops
1:39:36must have missed oh there we go it
1:39:38doesn't help that i put it over the old
1:39:39one there we go okay
1:39:41so now you have your default histogram
1:39:43and then we have a cumulative histogram
1:39:45and we should have 50 steps in there and
1:39:48let's just find out if that's true not
1:39:50so much by counting them i'm not going
1:39:52to count them if you want to you can
1:39:53count them let's just change it to 10
1:39:55and see what happens and we see here we
1:39:57have now 10 counts of that
1:39:59and we could set that for 5
1:40:03and run that
1:40:05and then we have our 5 on there and we
1:40:07go ahead and take the cumulative equals
1:40:09true out just so you can see what that
1:40:11looks like and let me run that on here
1:40:13too
1:40:14that looks just like it did before i
1:40:16think there's what one two three four
1:40:18five six seven eight they have eight
1:40:20different bins on here is what the
1:40:21default came out of
1:40:23put that back in there run
1:40:26and so now it should look almost
1:40:27identical and it does and then we can
1:40:29put the cumulative back in see what that
1:40:31looks like with the cumulative
1:40:34and run that
1:40:36and we can see how that shifts
1:40:37everything over and has a slightly
1:40:39different luck
1:40:40wait it shifts it all to the right no it
1:40:42doesn't actually shift it to the right
1:40:44it's cumulative so it's the total of the
1:40:47different currencies and so what that
1:40:49means is like if you consider this like
1:40:50for the year of rainfall we have like
1:40:52day one you had a little bit of rain day
1:40:55two we have more rain and so if you look
1:40:57at the number this is a hundred thousand
1:40:59thirty five thousand so it's the
1:41:00cumulative detail the histogram of the
1:41:02currents as it grows and rainfall is a
1:41:04good one because that would be a
1:41:06cumulative histogram of how much rain
1:41:08occurred throughout the year and we're
1:41:09going to look at two more graphs we've
1:41:12already looked at a bunch of them we
1:41:13looked at our radar graph we've looked
1:41:15at scatter step bar fill in basic plots
1:41:19we've looked at different ways of
1:41:21showing the data you know we can
1:41:22increase the size of the line the look
1:41:24the color the alpha setting
1:41:26so let's look at contour maps let's put
1:41:29that in there there we go draw a contour
1:41:32map and before we draw a contour map
1:41:35we need to go ahead and create data for
1:41:36it and if you have contours your data is
1:41:38all going to have three different values
1:41:42so let's go ahead and create the data
1:41:43here we have our you'd import your
1:41:45matplot library your numpy so we have
1:41:47our numbers array
1:41:48and we'll import matplot.cm
1:41:52and that's your color maps so you have
1:41:54all these different color maps you can
1:41:55look at there's like hundreds of color
1:41:57maps so if you don't want to do your own
1:41:59color you can even do your own color map
1:42:00they're pretty diverse and of course our
1:42:03plt we're going to our pi plot and to
1:42:06generate our different data we're going
1:42:08to create a delta
1:42:090.025 and we'll start with x and we're
1:42:12going to create an array between -3 and
1:42:153 and delta increments of 0.025
1:42:18and we'll have our y we'll do something
1:42:21similar and then we'll create our x y
1:42:23into a mesh grid again these are all
1:42:25numpy commands so if you're not familiar
1:42:28with these you'll want to go back and
1:42:29review our numpy tutorial and we'll do
1:42:32an exponential on here minus x squared
1:42:34minus y squared for z1 we'll do a z2 so
1:42:38we have two different areas and z equals
1:42:41z of 1 minus z2 times 2.
1:42:44so we've created a number of values here
1:42:45and let me go ahead and run this and
1:42:47let's plug that in so you can see where
1:42:48those values are going so once we've set
1:42:51these we're going to create our figure
1:42:52and our x from our plt subplots we're
1:42:55going to create the variable cs and this
1:42:58is going to be our contour so right here
1:43:00cs is our contour surface and we're
1:43:03feeding it x y and z
1:43:05if you remember x y we created as our x
1:43:08and y components using our mesh grid and
1:43:11you know what let's do this just because
1:43:13it's kind of good to see this let's go
1:43:15ahead and print
1:43:16x
1:43:17and let's print
1:43:19y and i always like to do this when i'm
1:43:21working with something that's either is
1:43:23really complicated in this case is what
1:43:24we're looking at or you don't understand
1:43:26yet so we've created a mesh grid we have
1:43:29x y and when we're done with this we end
1:43:31up with here's our x
1:43:33and this set of values and our y so
1:43:34those are x and y coordinates and then
1:43:36we've also created z based on our x and
1:43:39y so we have x capital x capital y and
1:43:42capital z is our three components
1:43:44x and y be in the coordinates well z is
1:43:47going to be our actual height since
1:43:49we're doing a contour map so we created
1:43:52our contour map from our x y and z
1:43:54coordinates we want to go ahead and put
1:43:55in a c label maybe we want to go ahead
1:43:58and do a title on here
1:44:01we'll put that in our set title and this
1:44:03is a
1:44:04contour there we go contour map
1:44:07and let's go ahead and run this and see
1:44:09what that looks like
1:44:10and you'll see we generated a nice
1:44:12little contour map there's different
1:44:13settings you can play with on this but
1:44:15you can picture this being you're on a
1:44:17mountain climb and here we have a line
1:44:19that's represent zero maybe that's sea
1:44:21level and then moving on up you have
1:44:23your contours of 0.5 and then minus one
1:44:26and different setups little hills i
1:44:28guess if it's minus that's like a pit so
1:44:30i guess you're going down into a pit at
1:44:32-5 and -1 when on the other side you can
1:44:36see you're going up in levels so here's
1:44:38a mountaintop and here's like a basin of
1:44:40some kind and in data science this could
1:44:42represent a lot of things this could
1:44:44also be representing two different
1:44:46values and maybe profits and loss i
1:44:48don't know if i'd ever really do that as
1:44:50a contour map but i'm sure you can be
1:44:51creative and find something fun to do
1:44:53with a contour map and then we're going
1:44:55to look at one last map which is the 3d
1:44:57map and those are can be really
1:44:58important as a final product because
1:45:00they can show so much additional
1:45:01information
1:45:02that you can't fit on two dimensional
1:45:04graphs
1:45:06there we go draw a 3d image
1:45:08and so we're going to import from our
1:45:10mpl toolkits the implant 3d and the axis
1:45:153d we're going to import axis 3d this is
1:45:17what's going to let us work with the 3d
1:45:19image and this should look familiar
1:45:21we're going to create another figure
1:45:22just like we did before figure size 14
1:45:24by 6. it's a good fit on the screen
1:45:26we'll go ahead and run that
1:45:28so we have our figure and let's go ahead
1:45:30and take our x and we're going to set
1:45:32that equal to fig dot add subplot that
1:45:36should also be familiar from earlier and
1:45:39we're going to work with this sets the
1:45:41settings for the projection we're going
1:45:42to use one two one projection 3d and
1:45:45we'll see what that looks like in just a
1:45:47minute and we just created some
1:45:48three-dimensional data here before
1:45:51where we had x y and z capital x y and z
1:45:53so we're going to reuse that data we're
1:45:55just going to use that since this also
1:45:57this is also a three-dimensional image
1:45:59so let's use that for a
1:45:59three-dimensional graph and we simply do
1:46:02ax plot underscore
1:46:05surface
1:46:06and our capital x
1:46:08capital y capital z so there's our data
1:46:11coming in and we're going to add some
1:46:13settings in here we're going to do r
1:46:14stride 4 c stride 4 and line width 0.
1:46:19i'll show you what that is here in just
1:46:20a minute let's go ahead and run that so
1:46:22we can see our graph
1:46:24and of course it helps if i don't add an
1:46:25extra comma in there and you can see it
1:46:28generates this really beautiful
1:46:29three-dimensional graph
1:46:31so let's take a little bit time to
1:46:33explore some of these numbers we have
1:46:35going in here
1:46:37we have the r stride 4 the c stride 4
1:46:42and the projection 3d projection 3d is
1:46:44the important one because that's telling
1:46:46us that this is a 3d graph here
1:46:49so what are these first numbers 1 2 1
1:46:52let's just change one of these i'm going
1:46:53to change this to 5
1:46:55and it's going to give me an error let's
1:46:57change it to 1.
1:46:59and oh that didn't work let's change
1:47:00this middle one to 3 instead and you're
1:47:03going to see how it starts reshaping the
1:47:05size and how it fits on the screen and
1:47:08we'll change the first one to two we'll
1:47:10run that one
1:47:12and again it's changed the dimensions
1:47:14and the size and how it fits on here
1:47:16play with these numbers to get a nice
1:47:17look and feel for it part of it is the
1:47:19tilt and the angle
1:47:21we'll do seven on this one
1:47:24there we go you can see it really
1:47:25shifted it there
1:47:27but again that changes the size now fits
1:47:29on the canvas
1:47:30but we'll leave it at the one
1:47:32two and just so you get a good look at
1:47:34what we're talking about here this is
1:47:36column width and index from before
1:47:39if we do 1-1-1 you can see that it now
1:47:42spreads it out all the way across uses
1:47:44the whole set up on there so this has to
1:47:47do with the size and how big you want it
1:47:48to be now there's one term that we
1:47:51didn't cover in this yet but we've used
1:47:53it throughout the whole setup
1:47:56and i'm just going to type that down
1:47:57here even though we're not going to go
1:47:59into detail and that's the term heat
1:48:01map you might see that it's kind of
1:48:03starting to lose ground as far as a
1:48:05common reference but there sure are a
1:48:07lot of people who still talk about heat
1:48:09maps what is a heat map well it is
1:48:11simply a color map that's all it is so
1:48:13if you ever see the term heat map
1:48:16that refers to the fact this is in
1:48:17different colors representing different
1:48:19heights
1:48:20that one is in the heat map but you can
1:48:22see up here we switched into
1:48:24let me go back up here here we go this
1:48:26one has different colors for the
1:48:27different values
1:48:29a lot of times you'll use like instead
1:48:31of x and y you might do a heat map
1:48:34where you have a fourth value and the
1:48:36fourth value represents the color and so
1:48:38you'll see this 3d image in a nice
1:48:39colors represented by a heat map that's
1:48:42all it is so if you see the term heat
1:48:44map that only means we're plotting some
1:48:45of the data in color to make it stand
1:48:48out or to give it a fourth dimension in
1:48:50this case
1:48:51so we've covered a lot of things on
1:48:53matplot and that brings us cover all the
1:48:55basics so that brings us to practice
1:48:58example and this is going to be the
1:48:59challenge for you and let me go ahead
1:49:01and change our cell
1:49:03cell type mark down and run that so it
1:49:05looks pretty
1:49:06practice example write a python program
1:49:09to create a pie chart of the popularity
1:49:11of programming languages
1:49:13okay excellent
1:49:15and if you're going to have a challenge
1:49:16we need some data and i'll just throw in
1:49:19our import our map library at the
1:49:21beginning you should do that
1:49:22automatically and so for our data to
1:49:25plot we're going to have our languages
1:49:26we're going to have python we're going
1:49:28to have java php javascript c sharp c
1:49:31plus plus so those are six categories
1:49:34and then we have our popularity oops
1:49:36misspelling there popularity we'll give
1:49:38the first one 22.2 percent java 17.6 and
1:49:43i don't know if these are real numbers
1:49:44they pulled
1:49:46my guess is that they might have just
1:49:48been made up because i don't know if
1:49:49python's really that much more popular
1:49:51than the other ones maybe specific to
1:49:53data science because python is very
1:49:55popular in data science right now
1:49:57because it has so many options the only
1:49:59other program that's highly used
1:50:01exclusively for data science is r so
1:50:04python's big and python also does a lot
1:50:06more it's a full programming language
1:50:08where r is primarily for data science
1:50:11they didn't put r in here so we have
1:50:13python we have java we have our php and
1:50:15you can see the different values they've
1:50:16given it or different percentages
1:50:18and i did add these up does not add up
1:50:20to 100 it adds up to 71 percent or
1:50:23something like that
1:50:24and then we're going to give colors and
1:50:26we've chosen these guys in the back
1:50:28brought in these colors i'm not sure
1:50:30what these colors are we'll find out in
1:50:32a minute so i'll be exciting but you can
1:50:34see they're using the actual color
1:50:35values you can pull off of a color wheel
1:50:37or something like that you could have
1:50:39just as easily done blue red green if
1:50:41you're too lazy to pick the exact colors
1:50:44and then let's go ahead and solve this
1:50:46and see we got here we're going to do
1:50:48something a little fancy just because we
1:50:50can the first thing we're going to do is
1:50:52we're going to use a variable called
1:50:54explode and you'll notice that there's
1:50:56six variables in here so that matches
1:50:57our six different categories and the
1:51:00first one we've done is point one and
1:51:02then zero zero zero zero zero point one
1:51:05when we put this in here under the
1:51:06explode in the plot it will actually
1:51:09push that square out so it's a really
1:51:12cool feature to highlight certain
1:51:14information on a pie chart
1:51:16and this is simply
1:51:18plt dot pi
1:51:20and we're plotting
1:51:23popularity there we go
1:51:25and before we add in all the really cool
1:51:27settings for this let's go ahead and run
1:51:30it and you'll see we generate a nice
1:51:32flat pie not too exciting there and then
1:51:35we'll go ahead and put in all the extras
1:51:38i talked about explode we can explode
1:51:40one of the values out so here's our
1:51:42explode equals explode labels as
1:51:44languages because we want to know what
1:51:46the different colors mean here's our
1:51:48colors equals colors our auto picture
1:51:51and this is standard print format so
1:51:54that's a python setup on there and
1:51:56that's just going to put the value on
1:51:58the pie slice and then we're going to
1:52:00add shadow because it just looks cooler
1:52:01with a shadow gives a little 3d look and
1:52:04we'll do a start angle of 140. let's go
1:52:06ahead and run this and take a look and
1:52:08see what comes out of that
1:52:10and look how that changes the whole
1:52:12setup so here's our labels there's our
1:52:14value we put on there there's our slice
1:52:16it's pushed out there's our shadow a 3d
1:52:18effect
1:52:19and then we started at 140. we could
1:52:21also rotate this let's just do this
1:52:24angle
1:52:2590
1:52:26and if we run it
1:52:28you'll see the blue pie slice has moved
1:52:30up a little bit we could actually do
1:52:32actually let's just take the whole
1:52:33starting triangle out and run he'll
1:52:35default to zero
1:52:37this is what it looks like if it
1:52:38defaulted to zero so depending on where
1:52:41you want the highlighted slice to appear
1:52:43usually you want that appear on the left
1:52:44because people read left to right and so
1:52:46it draws a focus onto in this case
1:52:48python and how great python is i'm a
1:52:50little biased we're teaching a python
1:52:52tutorial so it should be understandable
1:52:54that we're looking at python and one
1:52:56last reference before we close you can
1:52:59go over to the map plot library dot pi
1:53:01plot set up and if you go underneath
1:53:04there the different functions on there
1:53:06you can look this up on their website
1:53:08you'll see a full list and this is why
1:53:10it's so important to go through a
1:53:11tutorial like this because this list is
1:53:14just so massive trying to figure out
1:53:16like here's our bar plot there's a bar h
1:53:19you can add barbs there's a box plot we
1:53:22didn't cover
1:53:23c labels a totally different kind of for
1:53:25your contour plot you can set up in
1:53:27there if you go down here we have our
1:53:29figures we used on there we showed you
1:53:31the basics how to do the figure you'll
1:53:33see some
1:53:34closer references on those
1:53:36there's a histogram down here hist
1:53:39there's also the hiss 2d makes a 2d
1:53:41histogram plot h lines all of this these
1:53:44are all the different commands that are
1:53:46underneath of here and you can see it's
1:53:47pretty extensive
1:53:49we've covered all the basic ones so that
1:53:51you know have a solid ground to look at
1:53:53these different options so when you come
1:53:56to these functions some of them are
1:53:57going to look a little off or not off
1:54:00will look unfamiliar but you'll still
1:54:02have the availability to probably
1:54:03understand most of this and have a basic
1:54:05understanding of your matplot library
1:54:08with that i'd like to thank you for
1:54:09joining us today for another step in
1:54:11your journey in programming with python
1:54:13in this part in metplat library for data
1:54:16science for more information visit us at
1:54:19www.simplylearn.com get certified get
1:54:22ahead
1:54:27hi there if you like this video
1:54:28subscribe to the simply learn youtube
1:54:30channel and click here to watch similar
1:54:33videos to nerd up and get certified
1:54:35click here