Bummer! This is just a preview. You need to be signed in with a Basic account to view the entire video.
Start a free Basic trial
to watch this video
NumPy integrates with a whole slew of external packages and tools. Let's take a look at the plotting library, matplotlib
Learn More
 [Introduction to Data Visualization With Matplotlib course](https://teamtreehouse.com/library/introductiontodatavisualizationwithmatplotlib_
My Notes for Routines in Action
## Common Routines
* Common [mathematical](https://docs.scipy.org/doc/numpy1.14.0/reference/routines.math.html) [routines](https://docs.scipy.org/doc/numpy1.14.0/reference/routines.html) are exposed so the formula can be abstracted away.
* [`mean`](https://docs.scipy.org/doc/numpy1.14.0/reference/generated/numpy.mean.html#numpy.mean) is a [statistics](https://docs.scipy.org/doc/numpy1.14.0/reference/routines.statistics.html) routine used to calculate the average.
* Reduction functions take a dimension and collapse it into a single value.
* These functions define an axis parameter, and you should remember that the function works across the dimension.

0:00
Just about every Python data library that you come across is going to accept and

0:04
produce NumPy arrays.

0:06
Let's take a look at a common use case, plotting our data graphically.

0:10
Let's go and get Matplotlib installed.

0:13
So here are my reflection notes, but before we review them, let's multitask.

0:17
We need to install Matplotlib, so let's flip over to our terminal.

0:23
And we will stop the server with Ctrl + C and do I wanna shut this down?

0:27
I'm gonna click yes to shut it down.

0:30
If we go back at the prompt and we're gonna type conda install matplotlib.

0:36
And as that starts happening, we are just gonna go and

0:39
flip over back to our notebook, I'm gonna say OK, it's still up.

0:43
So it says connecting the kernel, that's fine cause the server is down.

0:47
So let's take a look at this.

0:48
So some common mathematical routines are exposed so

0:51
the formula could be abstracted away.

0:54
That was awesome.

0:55
So we saw mean was like that.

0:56
STD or standard deviation was like that.

0:59
A note here of reduction functions take a dimension and it will collapse.

1:03
It will reduce across that into a single value.

1:05
And that axis parameter is one that's a little bit tricky.

1:08
Good thing we made note of that with some examples.

1:11
Awesome, and it works across the dimension.

1:13
So that's the common routines.

1:15
And I did another one,

1:16
another little section here about the universal functions.

1:20
So here we go.

1:21
Universal functions expose a function to reduce an array to a single value.

1:25
And there's also a function called accumulate, which is nice,

1:28
cuz you can see things as it accumulates.

1:31
Let's see how we're doing with our server.

1:36
Shoot.

1:37
Yes, we want that to install.

1:39
I was trying to be a little too fast.

1:40
All right, cool.

1:41
Here, we're good.

1:42
So, let's go ahead and let's start that notebook back up, jupyter notebook.

1:49
And we should be able to just do it over here, if we come back here and

1:54
we say Kernel > Restart & Run All.

1:58
Here we go, back to it.

2:02
If you haven't used matplotlib lib before, it's a wonderful, versatile, and

2:07
quite popular graphing library.

2:09
There is more in the teachers notes if you would like to learn more.

2:11
So I'm gonna come up here into my import and I am going to add matplotlib.

2:17
So I will say import matplotlib.pyplot.

2:23
And keeping things lazy it's called PLT typically as plt.

2:30
Awesome, so what can we do with that?

2:34
Well, what do you say we create a graph that shows the student's GPA's matrix?

2:40
So let's get down to where that was at, here we go.

2:43
Let's get rid of a couple of these.

2:45
[SOUND] And I'm going to come here.

2:48
We should probably convey the way that each student performed over the year.

2:55
Now, choosing which type of graph to get your point across the best

2:59
is an art form in and of itself.

3:01
Now, one effective way that I've seen this expressed

3:05
is by using what is known as a box plot.

3:08
So, what a box plot does, is that it shows the range, the maximum and the minimum and

3:12
the various quartiles and the mean, all in one graph.

3:16
Well, the the best way to explore anything is to just to explore it.

3:19
Let's do it.

3:21
Off of the py plot module, there is a function named box plot, plt.boxplot().

3:29
And it takes an array, so let's give it our array, so

3:32
let's say students_gpas, and then I'm gonna go ahead and do plt.plot().

3:38
That will actually draw it here for us in our notebook.

3:41
Check this out.

3:48
[SOUND] students_gpas is not defined.

3:53
I wonder if that's because I ran the line.

3:56
Let's try that one more time.

3:57
Restart & Run All.

4:03
It looked like it worked.

4:07
Sorry about that.

4:10
Drops us down at the bottom too, so we've got to get back up to where we were.

4:15
There we go, there's our graph.

4:17
Look at that.

4:18
It looks like we're doing the right math, but it's along the years, right.

4:22
So this is 1, 2, 3, 4 if you remember what our, or students GPA's look like.

4:28
Fourth was all 4, and these was 4, 3.2, 3.96, let's just make sure, yeah,

4:32
that's what's happening here.

4:34
So, it's along the years instead of along the students.

4:38
This is also a perfectly fine way of looking at things, but

4:41
it's not what we're after.

4:43
So this bottom whisker here is the minimum and the top whisker is the maximum.

4:48
Here's the interquartile and this is the median here.

4:52
It's a bummer that the box plot is graphing along the columns.

4:57
Wasn't there a way to switch the axis of the array?

5:01
Like you can go from a 4 to 3 to a 3 to 4?

5:04
I'm pretty sure it was on that array manipulation doc that we linked to in our

5:09
notes.

5:10
If you don't remember how to do this, why don't you go and find that?

5:14
Give me a pause and see if you can't find it in the documentation and

5:17
get this graph showing what we want.

5:19
Go ahead, pause me.

5:22
Did you find it?

5:23
It's transposed.

5:24
So .T right?

5:27
On all arrays it will actually transpose and it will switch the order of arrays.

5:30
So lets run that one more time.

5:32
Beautiful, here we go.

5:33
So we got the three students, this is me.

5:36
I was all over the place.

5:37
Quesy doing good, like we said.

5:40
And here's Vlada doing well as well.

5:43
Got so much better from the start.

5:45
So what do you think we could do with our 100 days of code study minutes?

5:50
I know one of the questions that I get often is

5:52
how much time do you actually spend doing it?

5:55
Because sometimes it's hard to stop once you get on a roll.

5:57
[LAUGH] I'm definitely one to overdo it.

6:00
So let's go down to our study a minute, so I'm gonna scroll down here.

6:05
Right about here.

6:07
One way to tackle this is to group your data, right?

6:10
Now there's a type of graph known as histogram

6:13
that will group your data into bins or buckets and then display it.

6:18
It's pretty perfect for what we need actually.

6:21
So remember that we dropped the values that were under 60,

6:24
so we really just want values that are greater than 0.

6:28
So the name of the function is hist, short for

6:33
histogram, not like what a cat does when you try to be nice to it.

6:37
So we're gonna put it here, all of the minutes

6:44
that are greater than 0.

6:46
Actually, why don't you do that?

6:49
So I'm gonna leave a little to do here for

6:55
you, so we're gonna say pass in

7:00
a one dimensional array of all

7:05
the minutes that are greater than zero.

7:11
So you got that, right.

7:12
You can do that.

7:13
Go ahead. Enter this hist function,

7:15
pass in a one dimensional array of all the minutes that are greater than zero.

7:17
Go ahead pause me.

7:19
You got this.

7:21
How'd you do?

7:21
Did you get it?

7:23
So we can index our study_minutes array with a Boolean array, right?

7:30
And we're gonna use broadcasting here,

7:33
cuz we're gonna broadcast this 0 using this comparison operator and

7:37
that will give us a one dimensional array of all the values, and so we can say plot.

7:45
Bam, look at that.

7:48
There's some buckets that got defined by our data, so 25ish,

7:52
maybe 26, 27 times, we were in the 1st range of 60 or so.

7:58
And you could tweak this actually a little bit more, but

8:00
I think that this gets the point across pretty well.

8:02
There's times where I was down here in the threehour range.

8:06
But mostly, it was up here.

8:07
[LAUGH] And then there was even one where we kind of crazy down here.

8:10
I hope that you are starting to see and

8:13
feel comfortable navigating around the wonderful world of NumPy.

8:17
You'll see it everywhere.

8:19
Like everywhere.

8:20
And being able to use it and

8:21
understand the terminology surround it will greatly help your data journey.

8:26
Matplotlib is just one of hundreds of data related libraries

8:30
that work great with NumPy.

8:32
I am excited to see what you do with it.
You need to sign up for Treehouse in order to download course files.
Sign up