How to Make Better Plots in MATLAB: Plot Wrapper

This is third and final of a series of posts on plotting in MATLAB. This series is focusing on suggestions and considerations to get your plot looking “just right” for your paper or presentation. In the first post, I talked about how to improve the look of curves. In the second post, I talked about fixing up the text, including axis labels and legends. Here, I will talk about using a plot wrapper to automate most of your work. I will also talk about exporting your figure to whatever format you need.


Where were we? At the end of the previous post, our last version of the plotting function was the following:

Which produces the following figure:

Figure with legend based on ghost curves.We did a bit more tweaking with the figure to add some annotations, but they’re a bit harder to automate so I won’t discuss them here.

Wrapping it Up

Now, we want to make the plot wrapper. This will be a custom function that will apply our common figure changes and that we can call for any plot we want. I previously wrote about wrapper functions here.

We want as much flexibility as we can so that the wrapper will be useful. The input arguments will be:

  1. Handle to the existing axes
  2. Data to plot
  3. Changes to apply to the wrapper’s default settings

Below is a sample plot wrapper. It has 3 subfunctions to set the default properties for the figure, axes, and curve. 2 other subfunctions are used to change structure and object settings. These last 2 subfunctions use MATLAB’s ability to reference structure fields like they are an array.

A few comments on this plot wrapper:

  1. It can be called with empty data to just build the figure and axes without plotting anything
  2. You don’t need to include any of the figure, axes, or label arguments if you already have the axes handle from a previous call (or elsewhere)
  3. You can pass your own custom structures for the figure, axes, and curve properties. As long as the structures have fields with the proper naming convention, then you can change (almost) any property this way. Disclaimer: I haven’t seen this work well when properties are also structures.

Now let’s try a simple script to build the figure we have above:

This script produces the following:

Figure generated by script using plot wrapper
Figure generated by script using plot wrapper

The script using the wrapper is no shorter than the full plotting script above. But, it’s easier to copy and modify. If you make a large number of figures, then having a good wrapper should reduce your workload.


Now to finish off by describing how to export your figures. I’m not going to re-invent the wheel here. I highly recommend using export_fig (found here on the MathWorks file exchange). It does a great job at reproducing what you see on your screen. It also works well with various file formats. I’ve used it to make pdf, eps, and jpeg images (including the ones in these posts).

Is it the End?

This short series shows an end-to-end workflow of plotting in MATLAB. There will be more posts on specific “tricks”.

Leave a Reply

Fill in your details below or click an icon to log in: Logo

You are commenting using your account. Log Out /  Change )

Google photo

You are commenting using your Google account. Log Out /  Change )

Twitter picture

You are commenting using your Twitter account. Log Out /  Change )

Facebook photo

You are commenting using your Facebook account. Log Out /  Change )

Connecting to %s

This site uses Akismet to reduce spam. Learn how your comment data is processed.