TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. Join them; it only takes a minute:

Sign up
Here's how it works:
  1. Anybody can ask a question
  2. Anybody can answer
  3. The best answers are voted up and rise to the top

I recently discovered the part of pgfplots that allows you to add external graphics with the \addplot graphics command. I have a matlab plot converted to tikz with matlab2tikz. Problem is that the plot contains A LOT of data points and I don't want to process them every time I compile my document.

My question is as follows: Is it possible to externalize the curve of the plot only, such that I'm able to alter the axes/legend/title at any time without processing all the curve data again (i.e. I compile the complete tikz picture once, then externalize the curve only as a PDF file, such that that PDF can be used later with the \addplot graphics.)? Maybe this is possible with the \addplot graphics command but I can't find out how.

share|improve this question
The disadvantage of Christian Feuersänger's procedure is that by generating a png-file you lose the advantages of vector graphics. I suggest using standalone graphics as a step before including: matlab2tikz(MyPgfFigure.tex,'standalone',true) – strpeter Jan 22 '14 at 21:51

The answer to your question "Is it possible ..." is a clear "Yes".

You export your curve by means of matlab methods (for example with axis off; print -dpng myfile.png), then you integrate it to your tikz picture by means of \addplot graphics and afterwards you can safely

  • change axis labels
  • change axis ticks
  • change axis limits (!)
  • add custom annotations
  • add more plots directly in latex
  • etc.

at any time.

The \addplot graphics command assumes that you have some graphics (either vector graphics or bitmap graphics) and that some information about its underlying coordinate system is known (like the logical coordinate at the lower left and upper right of the image). In addition, it assumes knowledge about the bounding box of the image.

The details of "How can I map my logical coordinates from matlab to the resulting image and how do I provide them to pgfplots?" and "How can I analyze the bounding box and communicate its values to pgfplots?" are both discussed in length in the pgfplots manual which has its own section on \addplot graphics.

See http://pgfplots.sourceforge.net/pgfplots.pdf , section "4.2.8 Using External Graphics as Plot Sources". It contains detailed examples (also using matlab code).

The whole things works even for 3d axes, although the 2d case is considerably easier.

share|improve this answer

Your Answer


By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.