Default Plot Settings in Matlab

November 23, 2016

Note: This post may be out of date and some code may no longer do what is was originally intended. Since completing my PhD, I no longer have access to Matlab and am unable to update the post.

Have you ever applied custom styling to a figure in Matlab only to discover this styling does not transfer to subsequent figures? Unfortunately, plot styling only applies to the figure it was initially applied to. Luckily, an under-documented feature in Matlab allows you to set global properties for your figures. Matlab features a short document (find it here) about setting default properties.

For more details about how to customize the appearance of a plot, see my post Publication Quality Plots with Matlab.

Accessing the Global Plot Settings

If you run get(groot, 'Factory') in the Matlab (groot is the graphics root object), a list of over 1000 properties and their factory default values will print. These are the factory default properties that Matlab will apply to all plots. These settings cannot be directly changed, but we can override them using with the deafult settings.

The default settings can be viewed by running get(groot, 'Default'). This may print nothing initially, or it may print a few properties (on my computer it prints defaultFigurePosition and defaultFigurePaperPositionMode). To set one of these properties, we use set(groot, propertyName, value), where propertyName is one of the properties in the list of factory defaults with factory replaced by Default.

As a simple example, let’s say we wanted the default line width to be 10. We can view the factory default line width with get(groot, 'FactoryLineLineWidth') which should return 0.5. To change the default, we use set(groot, 'DefaultLineLineWidth', 10). If you now run get(groot, 'Default'), you will see the DefaultLineLineWidth property appear with a value of 10. To see this in action, Figure 1 shows the result of the code snippet below, both figures use the new line width of 10.

Figure 1
Figure 1: The default line-width of 10 is applied to both figures.
set(groot, 'DefaultLineLineWidth', 10);

x = linspace(0, 2*pi, 1000);

figure(1);
plot(x, sin(x), x, cos(x));

figure(2);
plot(x, besselj(0, x), x, besselj(1, x));

Example 1

In this example I will work through how you can create a nice theme for your plots that uses a dark background with brightly colored lines. LaTeX is used to render all fonts in the plot giving it a much more professional look than the standard font used by Matlab.

To start, we will set up our base plot (with the factory default settings applied). Here I will create two figures, one of the Bessel function of the first kind for $\nu = 0\dots 4$, and the second a plot of the Legendre polynomials for $n = 1 \dots 5$. The resulting plots can be seen in Figure 2 and was produced with the code snippet below the figure.

Figure 2
Figure 2: Left: Plot of the first five Bessel functions of the first kind. Right: Plot of the first five Legendre polynomials. No custom styling has been applied yet.
% Figure 1
figure(1);

x = linspace(0, 20, 1000);

hold on;
for i = 0:4
    plot(x, besselj(i, x));
end
title('Plot of $J_{\nu}(x)$ for $\nu = 0\dots 4$');

% Figure 2
figure(2);

x = linspace(-1, 1, 1000);

hold on;
for i=1:5
    plot(x, legendreP(i, x));
end
title('Plot of $P_i(x)$ for $i=1 \dots 5$');

Clearly these plots need some work to make them nicer looking. To start, we will change the background color. Two properties need to be changed for this: DefaultFigureColor and DefaultAxesColor. FigureColor controls the background of the figure window, and AxesColor controls the background color of the plot area. We will also turn off the property FigureInvertHardcopy so Matlab does not change these settings when we save our plot.

set(groot, 'DefaultAxesColor', [25, 25, 25]/255);
set(groot, 'DefaultFigureColor', [25, 25, 25]/255);
set(groot, 'DefaultFigureInvertHardcopy', 'off');

Next, we can change the default colors Matlab uses for the lines. The AxesColorOrder property expects an $n \times 3$ matrix of values between 0 and 1 where each row represents an rgb triple. While we are working on the lines, we will set their default width to 3 using DefaultLineLineWidth.

colors = [241, 90, 90;
          240, 196, 25;
          78, 186, 111;
          45, 149, 191;
          149, 91, 165]/255;
set(groot, 'DefaultAxesColorOrder', colors);
set(groot, 'DefaultLineLineWidth', 3);

Because we changed the background color to a dark gray, we will need to change the color of all the text to white so it is visible. The colors for the axes lines will also need to be inverted.

set(groot, 'DefaultTextColor', [1, 1, 1]);
set(groot, 'DefaultAxesXColor', [1, 1, 1]);
set(groot, 'DefaultAxesYColor', [1, 1, 1]);

Finally, we can make Matlab use LaTeX to render all text. This will give the figure a much more professional feeling.

set(groot, 'DefaultTextInterpreter', 'LaTeX');
set(groot, 'DefaultAxesTickLabelInterpreter', 'LaTeX');
set(groot, 'DefaultAxesFontName', 'LaTeX');
set(groot, 'DefaultLegendInterpreter', 'LaTeX');

The result of making these changes can be seen in Figure 3. Clearly this is a much more attractive plot than in Figure 2, and these settings will be applied to all subsequent figures. The full Matlab code can be found below Figure 3.

Figure 3
Figure 3: Left: Plot of the first five Bessel functions of the first kind. Right: Plot of the first five Legendre polynomials. The factory default style has been overridden to give the plots a more professional look.
% Set the default plot properties
colors = [241, 90, 90;
          240, 196, 25;
          78, 186, 111;
          45, 149, 191;
          149, 91, 165]/255;

set(groot, 'DefaultAxesColor', [25, 25, 25]/255);
set(groot, 'DefaultFigureColor', [25, 25, 25]/255);
set(groot, 'DefaultFigureInvertHardcopy', 'off');

set(groot, 'DefaultAxesColorOrder', colors);
set(groot, 'DefaultLineLineWidth', 3);

set(groot, 'DefaultTextInterpreter', 'LaTeX');
set(groot, 'DefaultAxesTickLabelInterpreter', 'LaTeX');
set(groot, 'DefaultAxesFontName', 'LaTeX');
set(groot, 'DefaultLegendInterpreter', 'LaTeX');


% Figure 1
figure(1);

x = linspace(0, 20, 1000);

hold on;
for i = 0:4
    plot(x, besselj(i, x));
end
title('Plot of $J_{\nu}(x)$ for $\nu = 0\dots 4$');

% Figure 2
figure(2);

x = linspace(-1, 1, 1000);

hold on;
for i=1:5
    plot(x, legendreP(i,x));
end
title('Plot of $P_i(x)$ for $i=1 \dots 5$');

Resetting the Factory Default Properties

If you decide you want to reset the properties back to the Matlab factory defaults, you may notice that the standard plot clear commands (clf('reset'), and cla reset) do not clear the properties. Running set(groot, 'Default', struct()) will clear all custom default settings you have applied and the factory settings will be used.

My Default Settings

Here are the default settings I typically use. A sample plot with only these settings applied can be seen in Figure 4. You can add these to a startup file so they are automatically applied every time you open Matlab.

set(groot, 'DefaultLineLineWidth', 3, ...
           'DefaultTextInterpreter', 'LaTeX', ...
           'DefaultAxesTickLabelInterpreter', 'LaTeX', ...
           'DefaultAxesFontName', 'LaTeX', ...
           'DefaultLegendInterpreter', 'LaTeX', ...
           'DefaultAxesLineWidth', 1.5, ...
           'DefaultAxesFontSize', 16, ...
           'DefaultAxesBox', 'on', ...
           'DefaultAxesColor', [1, 1, 1], ...
           'DefaultFigureColor', [1, 1, 1], ...
           'DefaultFigureInvertHardcopy', 'off', ...
           'DefaultFigurePaperUnits', 'inches', ...
           'DefaultFigureUnits', 'inches', ...
           'DefaultFigurePaperPosition', [0, 0, 5, 3.09], ...
           'DefaultFigurePaperSize', [5, 3.09], ...
           'DefaultFigurePosition', [0.1, 0.1, 4.9, 2.99]);
Figure 4
Figure 4: Sample plots with my default plot appearance settings applied.

Final Thoughts

It takes a little bit of work to setup your own default plot settings in Matlab. Hopefully in the future Matlab will implement themes similar to how some of the popular plotting libraries work (ggplot, Plotly, Seaborn, etc.). I hope this article was helpful; if you have any tips, tricks, suggestions or questions, make sure to leave a comment below.