MATLAB had many options for changing the look of plots. See how to modify the line color, markers, and add a grid to the plot.
- [Instructor] So one thing we often want to do to our plots is add a grid. Let's open to our script here. And after our plot command, let's turn the grid on. If we rerun our script, we can see that MATLAB has automatically created a grid for us. One thing I don't like is how our wave ends right after the six, and we can see the little space there. We can force the plot window to scale perfectly to the plot inside of it by turning on axis and setting it to tight. Keep in mind, any of these commands you can hover over and press F1, or right-click and ask for help, and it will give you all kinds of options that you can check out.
So now that we have axis tight in there, we can see the window fits perfectly to the plot. But I also don't like how our wave goes exactly to the top and the bottom of our plot. That looks kind of gross. So let's go ahead and explicitly set the limits on the Y axis. We do this with the command ylim, and it's a function, and the argument that it takes is a two-element vector. So let's put the square brackets inside there, and let's set the lower and upper limits in here.
I'll say, minus 1.5 for the lower limit, and 1.5 for the upper limit. Let's see how that looks. That looks really good, I like that a lot. We can also change the way the curve in the plot looks by using what's called the LineSpec here in our plot function. If we add another comma, we can put in a string that'll set the various ways our curve can look. For example, if I set this as m, it will change the color of our plot to magenta. We could also change that to say yellow, with a y.
Kind of hard to see, but it's still there. We can also change what kind of marker is used for our plot. So I'm going to put in a lowercase o to have the marker change to a circle, and put the color back to magenta. That looks pretty cool. Another trick that we can do is to plot our curve multiple times with different lines or markers. The way we do that is we use the command hold and on. This tells MATLAB that any further plot commands should all be sent to the Figure One without overwriting them.
So I'm going to copy our plot command here. Paste it in there. We'll keep the plot the same. Let's add dash dash, which will do dash line, and let's set it to c for cyan underneath. Now if I press run, we can see a dashed cyan line is included along with our magenta circles. So there are a lot of options that you can put in here to change the way your plot looks. Again, if you put your cursor over plot, hit F1 for help, and you can scroll down and check out the LineSpec, then there's many, many options here for the different type of line styles, markers, colors, et cetera.
- Creating MATLAB variables
- Working with matrix and scalar operations
- Using if statements and loops
- Creating functions
- Importing data
- Building basic plots and 3D plots
- Working with images
- Creating Simulink models