This is the first of a few posts on plotting in MATLAB. I’ll be describing more than the mechanics of calling the plotting (and related) functions. This series will present suggestions and considerations to get your plot looking “just right” for your paper or presentation. By the end of the series, you will have your own plotting function that can automate as much as possible. I’m focusing on 2D plots generated by the MATLAB “plot” function, which is the most common in MATLAB, but most of the ideas also apply to the other plotting functions, too (like “histogram”), or even to plotting in general (via MATLAB or elsewhere). In this first post, I’m talking about improving the curves themselves. The next post describes annotations and other text.
Every curve plotted in a figure should stand out and be easy to read, whether in color or in black-and-white. We’ll jump right to generating a figure with some data, and then I’ll go over some suggested changes to the default appearance. I assume that you already know what you want to plot (figuring that out would be a series on its own!).
A Sample Plot
I’m going to start with a sample end result. We’re not going the whole way in this first post, but you get an idea of where we’re headed.
The figure above is clean, clear, and would be as easy to read in black-and-white as it is in color. Now, let’s try to create it. We’ll start with the following simple script:
The script above uses the simplest call to “plot” possible. We create 4 arrays of data: 2 “smooth” curves and 2 “noisy” curves. All 4 arrays are plotted at once in a single call to plot. If no figure is currently open, then a new figure is created and the 4 lines are added with default parameters. The result is below:
So this is our first attempt, and it’s rough. You could go to the figure editor to start changing properties, and this is useful to find parameters that you like. However, in the long run you have to repeat everything if you recreate the plot. We’re going to focus on changing the settings via the plotting script.
Aside: MATLAB uses American spelling of the word “Color”, so here I will too.
To start improving the figure, we’re going to do a couple of things. First, we’re going to modify how we create the figure in the first place so that we have more control:
- We call “figure” to create a blank figure
- We call “axes” to place a set of axes on the figure we created
- In the call to “axes” we will also increase the font size and enable grids for the x- and y-axis
- We call “hold” and set it to ‘on’. This way, we can call “plot” multiple times and each new curve won’t replace the existing curves
- We have a separate call to “plot” for each of the 4 curves, and we specify to plot on the axes that we created. For now, the benefit of separate calls is just visual. But this will become more useful in a future post when we will generalize the code.
The first curve property that we will tweak is the color. The default MATLAB curve colors are generally not ideal for papers or presentations. I find them to be too bright, so we’ll increase the contrast. There are multiple ways to specify colors. I find it convenient to specify RGB triplets for the ‘Color’ property in the call to plot. One way to do this is to pass a vector of three doubles with values between 0 and 1. So, for example, default blue is [0 0 1], black is [0 0 0], yellow is [0 1 1], and so on. To get darker variants, decrease the values. For now, the only color that I will make noticeably darker is green.
With all of these changes, the plotting script is now
And the figure is now this:
Of course, we still can’t see all the lines yet. We’ll get there …
Markers and Lines
So far we can only see 2 lines. I want to see the smooth lines as solid curves, and just have markers for the noisy curves. I also want the lines to be a bit thicker. All of these changes are easy to get by setting the right plot properties. The plotting script is now
And we get the following plot:
Unfortunately, the markers have made the curves very crowded. You also can’t notice that we made the solid curves thicker. We still have changes to do.
Limits and Skipping
We’re going to make a final few changes that will let us see the data more easily:
- We’re going to make the x-axis log scale with the “set” function. I recommend log scales if you’re trying to see data over 2 or more orders of magnitude. It would also be acceptable to set the y-axis to log scale here.
- We’re going to control the number of data points plotted. Linear sampling is straightforward. However, the x-axis will be log scale. So, we should have sample indices that increase logarithmically. We can get these using the “logspace” function, which we surround with “round” and “unique” to only get integer indices that don’t repeat.
We now have the following plot script:
Note that we’re now sampling all curves logarithmically via the “plotInd” vector. We didn’t need to do this for the smooth curves. The following plot is created:
So now the curves are in good shape. You might find it helpful to play around with the settings in the figure editor. You can then update the script with the settings you want.
The next post in this series describes adding more polish to other parts of the figure. This includes text formatting and adding annotations.