Pretty plots: Automatically create TikZ source code for plots in LaTeX with Matlab

sin(x) -- blue, cos(x) -- orange, dashed

Example TikZ plot from Matlab code

LaTeX …
I have just finished the first chapter draft for my thesis. As you’d expect from a mathematician, I write my thesis in LaTeX (or $latex LaTeX$). In the evenings, when I couldn’t concentrate enough for creating text that would make sense to anyone, I instead played around with the plots I wanted to include.

… and Matlab don’t work too well together
I used to create all my plots in Matlab (MathWorks), save them as eps (for latex) and png (for pdflatex) and include those in the LaTeX file via includegraphics. This causes a few annoyances. The size of the included pictures is decided in LaTeX, but rescaling has an effect on everything, including the text in the labels and legend. This meant that for every type of plot (and every decision to use a different scaling) I need to fiddle with the font size in the Matlab figures, until it fits and create all those files again. In addition, although Matlab understands some LaTeX, there were some symbols it has problems with. Finally, the font in the labels is never the same as in the text.

Enter TikZ
I recently heard about this on TeX StackExchange. It’s a way to code your pictures like you code everything else in LaTeX. Unlike pstricks, which I had used in the past and can hardly remember, it works natively with pdflatex. In this way, you hand over the responsibility for any text in your plot to LaTeX. Great! To get started with TikZ, there are an extensive manual, a minimal introduction, loads of answered questions on TeX StackExchange and more.

Automate
To plot a function in TikZ, you interpolate it linearly through many points. That’s a lot of source code per function. There will be loads of plots in my thesis, each showing several functions. Oh, and want axes, too. Naturally, this calls for automation. I wrote two functions in Matlab that would automatically create the tikz code for the axes and a function plot. The plot at the beginning of this post was created with the following code (and cut to size).

Matlab code
x=(0:0.1:7); sinx=sin(x); cosx=cos(x);
fid=fopen('tikzcodefrommatlab.tex','w');
tikzaxes(fid,[0 1 7],[-1 0.5 1],'$x$','$y$','2em');
tikzfunctionplot(fid,x,sinx,'blue,ultra thick','sin(x)');
tikzfunctionplot(fid,x,cosx,'orange,dashed,ultra thick','cos(x)');
fclose(fid);

LaTeX source
documentclass[a4paper,12pt]{article}
usepackage{tikz}
begin{document}
begin{tikzpicture}[yscale=1.5] input{tikzcodefrommatlab}
end{tikzpicture}
end{document}

Related Links

How do you make sure that your plots and figures fit with the rest of your document? And does anyone have a good idea what to do with 3D plots?

Let me know in the comments below.

Update: I’ve added further links to packages that were pointed out to me: matlab2tikz and PGFplots. I haven’t properly tried them out yet, but especially pgfplots seems awesome.

6 thoughts on “Pretty plots: Automatically create TikZ source code for plots in LaTeX with Matlab

  1. Pgfplots (aka TikZ) can also produce 3D plots, but compiling take some time. I usually us the externalization feature of TikZ to overcome this inconvenience.

  2. That’s nice to hear. I guess I’ll have to dig deeper into the tikz manual and tex.stackexchange.com in that case.

    Thank you!

  3. Thanks for this post! I’m about to start my write-up and this will be a huge help. I’ve had a quick play with your code and matlab2tikz and I have to say, I prefer your code: matlab2tikz doesn’t seem to handle the axes labelling well. But then again, this is early days and I probably haven’t worked out how to use it completely.

    A couple of suggestions: in tikzaxes.m, I suggest you add a {} after centering on line 64 otherwise it bugs out. Your example works because of the $y$ math environment preventing the y being concatenated with centering.

    It would also be nice to have some sort of plotmark functionality included in your code.

    Good luck with the write-up!

  4. Thanks for the suggestions, J-P! Today I looked through manual for pgfplots, which Thomas pointed out. That package is awesome. It can do all types of plots. Use that: http://pgfplots.sourceforge.net/ I only needed to figure out how to update LaTeX packages to get rid of some weird error messages.

    I’ll probably adapt my code for 2d plots to pgfplots format soon (and then post an update). For 3d plots I’m planning to inlcude exported matlab plots and fit the axes around it (p. 43-48 of the pgfplots manual). Otherwise they’d probably melt my computer.

    Good luck to you, too!

  5. You’re welcome! 🙂 I did use pgfplots in the end, which I strongly recommend. I should write another post on that …

    Best wishes,
    Almut

Comments are closed.