Matlab2TikZ for Technical Plots

Data plots are at the heart of many technical papers. These are the first things readers look at, and sometimes they are the only thing they remember. Therefore, good articles need consistent, sharp, and expressive visualizations. In my opinion, the best way to create such a plot from Matlab is Matlab2TikZ.

matlab2tikz is a MATLAB script to convert native MATLAB figures to TikZ/Pgfplots figures that integrate seamlessly in LaTeX documents. Instead of integrating the TikZ file in the LaTeX, the plot can be directly converted to pdf in the export.

matlab2tikz has many options, and some are somewhat picky. To this end, here are a few practical guides to plotting with matlab2tikz.

There are many export options for matlab2tikz:

  • standalone (true/false): generates a standalone TikZ which can be compiled on its own
  • height, width: sets the plot dimensions which also ensures the correct font size
  • ylabel near ticks: moves labels closer to the axis
  • ylabel style={...}: all strings can be customized

A standard call I use is

matlab2tikz(filename,'height', '\figureheight', 'width', '\figurewidth',...
            'extraAxisOptions',{'ylabel near ticks','xlabel near ticks',...
            'ylabel style={font=\small\fontfamily{ptm}\selectfont}',...
            'xlabel style={font=\small\fontfamily{ptm}\selectfont}',...
            'legend style={font=\small\fontfamily{ptm}\selectfont},',...
            'ticklabel style={font=\small\fontfamily{ptm}\selectfont}',...
            extraAxisOptions{:} ...
            },...
            'parseStrings',false,'standalone', true, ...
            'height', height, 'width', width)

After converting your Matlab plot to a TikZ, you can directly compile it into a pdf. Before, you need to set the path to your Latex distribution.

% only once per session
setenv('PATH', [getenv('PATH') ':/usr/local/texlive/2020/bin/x86_64-darwin']); 
% generate pdf
plotviatikztopdf(filename)

Let’s apply this with a simple example:

addpath(genpath('matlab2tikz'))

N = 100;
t = 1:N;
y = randn(2,N);

plot(t,y); grid on;
xlabel('Time $t$ (samples)');
ylabel('Density $\rho(t)$');
legend('$\zeta = 0.1$', '$\zeta = 0.1$');

matlab2tikz_sjs('./plots/example.tikz','type','standardSingleColumn');

This browser does not support PDFs. Please download the PDF to view it: Download PDF.

A complete example and the wrapper functions can be downloaded here:

Of course, remember to download the Matlab2TikZ package. For larger plots, TikZ can easily run out of memory. In that case, add to /usr/local/texlive/2019/texmf.cnf:

main_memory = 7000000 
extra_mem_top.pdflatex = 200000000
extra_mem_bot.pdflatex = 400000000

TikZ is also a powerful drawing tool in itself, and you can find plenty examples online.

At first, this might look like extra work, but once you’re polishing your paper, you will highly appreciate the added precision. Happy writing!

Associate Professor for Signal Processing

My research interests include the virtual- and psychoacoustics, physical modeling and the design of virtual worlds.