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
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
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
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.
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.
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:
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.
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
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.
Finally, we can make Matlab use LaTeX to render all text. This will give the figure a much more professional feeling.
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.
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 (
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.
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.