Uniform figures styles in your thesis using Matlab

If there is one thing that I learned from writing a doctoral thesis, it is how much of a pain managing data and figures can be. For example, you may perform a simulation in year 1, an experiment in year 4, and when you write up the journal paper or the thesis chapter in which you compare the two, you need to fetch the right figures and have them look professional, printable, and preferably in a uniform style. Moreover, the figures need to be adaptable to different uses, such as poster presentations, a conference talk or a printed publication. In this article, I will outline the method I have developed for myself for achieving this in Mathworks Matlab and LaTeX.

For graphs and figures, the requirements mentioned before translate to:

  • the image is scalable, such that I don’t have to generate it at different resolutions; 
  • the image can be edited after it has been generated so that I can tweak its looks, if necessary, without rerunning the expensive simulation;
  • the image has to be black-and-white proof, i.e., if your image is printed or copied in black-and-white or copied on a photocopier, it should still be legible;
  • and the styling has to be uniform. 

Scalable figures that can easily be tweaked

To achieve this, I make all my figures using vector graphics. Unlike bitmap images that store color information for each pixel of the frame, vector images store the information on how to draw the picture from basic elements such as lines, dots and other geometric shapes. This makes vector images scalable and easy to deal with for many different print sizes. It also allows the image elements to be edited easily in programs such as Inkscape or Adobe Illustrator.

To have even more control over the figure in post-production, I do not only save the vector file, but also the Matlab figure itself in its own proprietary file format or as a script that generates the figure for me without running the entire simulation or data processing routine. For this, I use a little script that automatically saves three files for each figure: a low-resolution PNG bitmap for previewing and e-mailing, an EPS vector file printed publications and a Matlab Figure file or script for generation of the script.

Black-and-white proof

In order to make my images black-and-white proof, I apply some ‘best practices’ for print. For each figure, I make sure that there is at least one, and preferably two layers of redundancy for distinguishing data sets in the same figure. Each data set has its own color, marker, and line style. For my style, I use an adapted gray scale mapping to pick the line colors for two reasons: it avoids the problem of different colors translating to the same gray scale value and it avoids having to adapt the figure for print in case there are print charges for color figures (some journals charge $125 or more for a single color figure, while black-and-white images are printed for free). You could opt for a color map, that takes this into account, but I have not come across one that works well for lines or scatter plots too.

The “colormap” that I use is called “grayish” and is based on the “gray” colormap available in Matlab. It does, however, exclude white and has a settable maximum intensity. This takes care of most rendering issues (white features are not visible on a white background, light lines don’t print well).

If you are interested reading more about colormaps, have a look at these articles: 

Uniform style

To achieve a uniform style, I wrote a Matlab function that I apply to every figure. It scans the figure for all graphs, and within each graph for all line and scatter plots. It then applies the same settings to each graph. 

How it works

When creating a figure in Matlab, the figure object has a unique figure handle that can be used to reference it. This handle can be passed into a function to get access to the figure settings directly. Within each figure, every object (line, dot, bar, etc) is given a handle too, which makes it possible to identify individual objects and modify their properties.

To learn how Matlab handles can be retrieved, read Matlab: How to obtain all the axes handles in a figure handle?, which was the starting point for me.

Each figure at least has an axes. To identify all axes, such as plots, subplots and inset plots, we can use

allAxesInFigure = findall(figureHandle,'type','axes');

We can iterate over these axes, to modify the looks of each plots. For example, switch on the box, change the axes colors, font or font size.

To find all lines within each plot, we can then find all handles that are inside the axis we are working on using:

hline = findobj(ax, 'type', 'line');

where ax is the handle of the axis.

You can download the code at the end of this blog post. You can use it simply as follows:

% Generate some example data
x = linspace(0, 10);
y1 = x.^2;
y2 = x.^(3/2);
y3 = x.^(1/2) + x.^2;

% Generate a figure as usual. Just make sure to fetch the figure handle while you are at it. 
f = figure; 
plot(x,y1, x,y2, x,y3); 
xlabel('x'); ylabel('y'); 
legend("x^2", "x^{3/2}", "x^{1/2} + x^2")

% Apply the figure style.
uniformFigureStyle(f);

Executing this, should result in something that looks like:

Example figure after applying the styling function.

I have tweaked the looks to my needs and likings, but I think it can be a good starting point for your own style. Feel free to change it however you like. Also, note that the released version only works for line plots and scatter plots, as those are the ones I use all the time.

Download and license

To download the code, click here.

The code is officially owned by The Netherlands Organisation of Applied Scientific Research TNO, as they own all intellectual property that is generated under the umbrella of my PhD. This code is, however, free to use and is covered by an MIT license.

Copyright 2019 The Netherlands Organisation for Applied Scientific Research TNO

Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the “Software”), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:

The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.

THE SOFTWARE IS PROVIDED “AS IS”, WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.