In this lesson, we're going to talk about how we can graph data with MongoDB.
MongoDB's flexible data model as expressive query language.
used by idea scientists and analysts, matplotlib.
first we need to talk about how we pass data to matplotlib.
it almost always expects the data to be passed in via one or more arrays.
So, for this example, here I've created an array with values 1-5.
And how do you pass these arrays to matplotlib?
It's pretty easy if we want to create a scatter plot.
we then go ahead and set up our subplots so we can actually access our axes.
and then finally, we go ahead and show the plot.
matplotlib figures out these ranges of values on both axes and plots our coordinates.
it forms a nice little X squared curve.
And, we can do this exact same thing for data stored in MongoDB.
and then we can go ahead and write a query.
it's runtime against its rating.
So, let's create a query where we return only documents that have both of these values.
And then I'm going to go ahead and just project out just those two fields.
and then run the query, and turn into a list.
both our metacritic and runtime values.
not a single array or pairs of values.
and similarly, we're going to do the same thing for the ratings.
we can now go ahead and just pass this matplotlib.
You can see that I'm using some additional parameters now.
so this way we can kind of see where lots of points overlap.
And then also, I'm giving a title and labeling my axes.
as you can see here, there is a bit of correlation.
Movies that are very long in runtime seem to do pretty well on ratings.
which is kind of an interesting correlation.
I want to kind of show you more of the functionalities that you can do with matplotlib.
The same process can be applied to 3D plots where we want to compare three values.
between year and either of these variables.
turning it into a list of values.
and then setting our labels.
add_subplot and passing in a 3D projection parameter.
and what we're really seeing here is that there aren't very many years of data.
we don't really have any runtime data or any rating data for these values.
come off that kind of look like cat whiskers.
median, and first and third quartiles for a given set of values.
these box plots representing each day in a week.
days that people watch movies on mflix across each day of the week.
To do this, we're going to use this watching ping's dataset.
seconds that someone is watching a movie on mflix, we recorded that.
And that's what each of these documents kind of looks like.
who was watching, and then a time stamp for when they were watching.
a distribution of what hour of the day people are watching movies.
But first, we're going to transform our data a little bit.
So, here we use the aggregation framework.
pushing each document into an array field called pings.
so that Sunday is first and Saturday is last.
going to contain a sample of the pings for that day of the week.
not an array of dictionaries of array of dictionaries.
we want to get to an array of arrays.
for each pings. And now we have that matrix.
another array containing the hourOfDay for each ping.
and the inner array is the hour of the day.
We can now take this matrix and very simply pass it to the box plot function.
each representing a day of the week.
but people are still watching a lot of movies during the week.
And as you can see, people watch movies later in the day on the weekends.
There's lots of people watching movies late at night.
our data and we don't really see any of these during the week.
in the day on average relative to during the week.
these different kinds of plots using matplotlib in MongoDB.
work well with matplotlib to get the..