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.

Â