Basic data plotting in MATLAB

Video Statistics and Information

Video
Captions Word Cloud
Reddit Comments
Captions
hello and welcome to the screencast on basic data plotting MATLAB in this screencast we're going to review how the plot function works for mathematical functions and then apply that process for plotting data we're also going to learn a little bit about how to properly format a data plot and then how to use arrays and the things that we've learned in the array manipulation screencast to make plots from arrays so there's more than one way to plot data in MATLAB but we're gonna try to keep it simple and use the basic plot command that we saw earlier for plotting mathematical functions let's review how that function works first of all I have to if I were going to say plot the graph of y equals x squared I first have to create a vector for inputs for example X equal 1 0.5 to 10 and that would give me a vector of x values from 1 to 10 skipping by 0.5 and then I would have to define a vector for outputs using a function say y equals x squared so again what I've done is I've created two vectors that have the same length you see over there in the workspace there 19 entries long and then I would simply plot my X and my Y we all know how that works there's no reason though why I can't make those x and y vectors manually instead of using functions though and that's the heart of how I can use the plot command to plot data so I'm going to go over here to Wikipedia and I have the Wikipedia page for Johnson County Indiana where I'm currently located and at the bottom of this Wikipedia page are some data from the Census Bureau from 1830 all the way to the present let's suppose I wanted to make a time series plot where I'm plotting the population of Johnson County from 1950 to 2010 well this is another instance where I have inputs and outputs I'm just merely going to create the vectors by hand instead of using a function to make them for me so my inputs are going to go from 1950 to 2010 let's go over here to MATLAB and do that we'll call it year and 1950 to 2010 and I'm skipping by 10 years each so that would be my vector for inputs and now my vector for outputs that doesn't follow a mathematical function so I'm just going to enter that in manually move this over here and scroll down and I will simply enter in those population values by hand I'll call it pop and 1950 was 26 183 then 43 704 the principle behind what I'm doing here is the same as when I was using mathematical functions it says that I don't have a mathematical function to work with I'm just going to enter in a vector for outputs that is of course the same length as the vector for inputs 1 1 5 2 0 9 and then finally plus recent census data 1 3 9 6 5 4 I see that you have seven columns in each and so that's good I wouldn't be able to plot these if I didn't have that so now I can just simply type plot just like I did before and put the inputs which is here and the outputs which is population when I do that I'm going to get a nice little line graph right here so I've mentioned before the vectors in MATLAB you can think of them as like rows of spreadsheets and that's really what we've done here I've created a row or a column of X data and a row or column of Y data in a sort of spreadsheet and just using MATLAB to plot one column of the data on one axis and the other column on the other axis so there's really no difference between using the plot command for functions or for data it's just that we have to enter in things manually sometimes for the data now weird about formatting plots here this is a perfectly okay line plot but for observed values like census data we typically don't use continuous lines like this so I'm going to go back and change the plot here and include some plot options that make this look a little bit better when I am graphing observed data like these seven data points I typically use just markers to represent the data on the graph rather than a straight line so I'm going to go back and report this and say make it a dash line with blue squares and when I do that I have something that looks a little bit more like a data plot these little blue squares of the actual observed data and the lines are just there to show basically a trend and I can go back in and add some more options here for example a title would be a good idea Johnson County population and so on let's look at another example of plotting data this time with a little bit more complicated format of my data over here in the spreadsheet I have a collection of data that shows that tendance for the 1989-1990 season of the National Basketball Association on with the ticket prices for each market I think I would like to make a data plot that has attendance on the horizontal axis and price on the vertical axis so I'm plotting price versus attendance to see if there's any sort of visual relationship between price and attendance now to do this I'm going to evoke a few things we've learned in previous screencasts first of all I'm going to create a MATLAB variable that contains these two columns of data I'm going to go over here to my workspace click on new variable and this is called as NBA right now it has nothing in it I'm going to double click though and bring up the editor and I'll go back over to my spreadsheet and just copy out only the numerical data that I have here copy go to my MATLAB editor paste the excel data N and just to scroll down I see that these two vectors have the same length so I'm going to close this out and I see MBA is a 27 by 2 matrix it's an array now let's try to create our data plot we're going to go over here and remember I wanted to have a tendency on the x-axis the horizontal axis and price on the y axis so I'm going to do all this in an EM file have a little bit more control over what I'm doing now attendance is the first column of my MBA variable so I'm just going to create a separate variable for this called attendance and I'm just going to set it equal to the first column of NBA and remember from the array manipulation screencast that is MBA parentheses colon comma 1 to strip out the first column of the NBA matrix there and price is the second column MBA colon 2 when putting semicolons at the end because I don't want those vectors to echo back to the screen once I strip them out now I'm just simply going to plot attendance versus price and now when I SiC when I run this it's going to ask me to save it I'm let's call it MBA tests or MBA plot is better and when I get the plot up here it's going to look like kind of a mess because these are all disjointed points that aren't necessarily supposed to be connected but MATLAB defaults to connect to show no markers at all and connect all the points with blue lines that's not really the look that I want I think the plot is correct because the data are going to be scattered all over the place you can see that from the spreadsheet but I don't think I want to connect them with ones it doesn't make a lot of sense so I would like to change this plots that there are no lines at all and maybe black squares where the markers are supposed to be so I can do that by going over and inside the plot command pass a plot option to it and then single quotes normally if I wanted to say a dashed line I would put two dashes here or if I wanted a dot dotted line I put a dot here I want no lines I just simply pass it no options here and give it the color that I want say black that's K and then give it the markers style that I want I think I'm going to use squares here so I'm put s so what this is going to do is create a plot with no lines in it and just a bunch of little black squares and this is a nice what we normally call a scatter plot and there is there are other ways to produce scatter plots in MATLAB but we can do all these plotting needs here from this one plot command this is a pretty good plot let's go back and add a few more things to it to make it nice flap title and the attendance versus ticket price they land up at the top and then label the axes X label this is already shown as in in tens of thousands so I don't necessarily specify the units here I'll put attendance and that is measured in people and the y label is the price ticket price and that of course is measured in dollars and it might be helpful to to put a grid on this soft just like grid on throw that up there and now we have a nice looking plot that shows a little bit of a linear trend with a few outliers so some Markinson have very low ticket prices a lot of attendance and vice-versa so here's one that has fairly sort of average attendance but a very high ticket price so let's recap what we learned the screencast basically we learned that the process for plotting data is essentially the same thing as the process for plotting functions I still have to create a vector for inputs and a vector for outputs and then call the plot command using whatever options I want the only difference is I might have to enter in those vectors manually depending on how the data look secondly I can format these data plot the right way to do this is by using markers for the data points and then either no lines whatsoever or very faint dash lines to indicate dis connections between the observed data that's all thanks for watching
Info
Channel: RobertTalbertPhD
Views: 464,152
Rating: 4.9080706 out of 5
Keywords: CamtasiaForMac, visualization, data, plot, graph, scatter, statistics, time, series, Talbert, screencast, howto, tutorial, cmp150
Id: cyxFsSJSxwE
Channel Id: undefined
Length: 9min 10sec (550 seconds)
Published: Tue Feb 22 2011
Related Videos
Note
Please note that this website is currently a work in progress! Lots of interesting data and statistics to come.