0:00

[MUSIC]

[MUSIC]

Welcome to class, in this lecture we're going to talk about Plotting,

Plotting is a process in which you take a collection of data.

And make a picture that allows us to visualized the structure of that data.

For this class we use Plotting to understand the performance of

your program.

In particular, we'll look at the relationship between the size of

the input that you provided the program and the running time of the program.

This will help us structure the difference between good and bad programs.

So before we talk about how to plot,

let's talk little bit more about why we should like to build plots.

There's a old saying, a picture is worth a thousand words.

Let me give you an example here we see a picture and

this is a plot of Scott's aggravation level as a function of

the number of global variables that you use in your program.

Actually, I just made that up.

This is really just a plot of Global Temperature versus time.

And it's clear that this picture has inspired a lot

more than 1,000 words of discussion.

We're going to use Plotting here to understand large amounts of data and

organize it in a way where we can visualize the trend and

the behavior of the data without actually having to

wade through a massive amounts of individual numerical values.

I think as your career in Computer Science advances,

you'll find that Plotting is going to be a very powerful tool.

All right, what we'll do now is we'll look at some example code for

creating plots inside CodeSkulptor.

This will code, we'll use the simple module that we've built for CodeSkulptor.

There's lots of very similar modules that available for Desktop Python, so

anything you learn here should be applicable to Desktop Python very easily.

Simpleplot takes as input a list of points.

So it's going to be something that looks like this, where each point is

represented by a two pole that consists of an x0 and y0 for that point.

In our example,

we're going to plot points generated from the graphs of three functions.

A function that doubles the number, a function that squares the number, and

a function that raises 2 to a particular power.

2:29

All right.

The heart of our example code is the function create_plots.

This function is going to take.

The functions double, square and x, and

compute us at a points that lie on the grass of these functions.

The parameters begin and stride are going to control the position and

spacing of the x-coordinates of these points.

The x-coordinate will start at begin end at end, and

be spaced using this parameter stride.

This is very much like what range expression would be included in Python.

The difference here, is the stride doesn't have to be an integer.

This code actually builds a list with the corresponding x-coordinates, here.

Once we have this list, x cords.

We can then use three list comprehensions here to compute

the corresponding y-values for these x-values and build list of points

corresponding to graph of each function, so that's what takes place here.

To illustrate how these three list comprehensions work,

I've added a print statement that actually prints out double plot,

the list of points generated by this statement right here.

3:39

So let's run that.

And what you can see here is we have a list of tuples with a property that each

tuple consists of two numbers, an x and a y-coordinate.

Where the y-coordinate is actually twice the x-coordinate.

So this really is the graph of two times a number.

To turn that list of points into a plot,

I'm going to call the plot lines method from the simple plot module.

You can go up here and

use the Docs to actually find out the syntax of how plot lines works.

It takes a collection of parameters.

The most important thing is it allows you to give a list of, list of points.

So we're going to plot all three of these functions simultaneously.

So if I run that, what I see here is, I see a nice plot

that consists of the plot for the function double.

It's this yellow plot here.

Then I see a plot of the function square here.

And then I see the plot of this function exp, the Exponential function.

So between zero and two, all these functions look fairly similar, but

notice that I can actually change the range of the plots here.

This is why we have this parameter here.

What I'm going to do, is I'm going to extend the range of

the functions that we're plotting here to give it from 0 to 5.

5:06

So if I do that.

What comes out is I now see that well,

the linear function is actually staying here fairly low.

But the quadratic function and

the exponential function are actually staying very similar.

Let's keep going, let's plot a little larger range.

So let's plot this to 10.

And now when we look at our plot, we've actually gained some valuable information.

What we can see here is that the linear function is

somehow less than this quadratic function, but it's just a little bit less.

But this exponential function is growing very, very fast, it's a lot larger.

And so by looking at this plot we gain some valuable information about Linear,

Quadratic and Exponential Functions that somehow Exponential Functions grow a lot

faster than Quadratic and Linear functions.

And so for example later in the class, we're looking at various methods for

solving problems, we're going to try to focus on things where the running time is

kind of linear in the input size, or quadratic in the input size.

We're going to avoid things where the running time is exponential in

the input size.

Those are just not going to be tractable in practice.

So this is kind of a first start at building some plotting code and

learning to analyze the behavior of sets of points and functions.

We're going to do this more and more as we go through the class.

I'll see you next lecture.