What are the benefits of using tools like pgfplots or tikz directly in LaTeX compared to importing PDF/PGF generated by say matplotlib?

This answer believed that the main reason is to match the plots with the rest of the document. It also mentioned that no other plotting packages from R or python can generate graphs that have the same high image quality as those generated by pgfplots in LaTeX directly, but no examples or reasons were given. Is there evidence to back up this claim?

Another related question is that, is there any reason for exporting a plot to PGF instead of PDF? This comment said that the reason is PGF will generate the whole graph in LaTeX while importing to PDF even with usetex will only use LaTeX for the text part of the graph. But if the main reason for plotting in LaTeX is that we can match the plots with the rest of the text document, usetex exactly does that.

My current adopted approach to producing PDF documents with graphs is (i) generate graphs using tools like matplotlib or other plotting packages in a scientific programming language so that I can do computations before plotting or plot repetitive graphs; (ii) export to PDF files and use usetex if I want to match the font with my document; (iii) import the PDF files using \includegraphics and make final adjustments.

This seems to work out just fine and I didn't notice any image quality issues or graph not matching with the rest of the document.

So are there any other reasons to use native plotting utilities like pgfplots in LaTeX that I overlooked?

  • 2
    Personally I do not want match plot and main text fonts, so I hardly seem justified change the simplicity of manage data and plotting in R for a more complex LaTeX solution. Moreover with knitr you can use a tikz device, so the R plot can also match the LateX fonts in this way (example here). Therefore, I use pgfplots or tikz directly only when I do not kown an easier method in R. – Fran Nov 14 at 8:23
  • 2
    One big reason (but not big enough to warrant an answer of its own) is archival value - a document with a tikz picture can be stored as a very small text file (all you need is the tex document), whereas if you are importing figures, the image files can often be much bigger than the tex file in question, meaning the same document takes up significantly more space wherever it is archived (or arXived). – Sean English Nov 14 at 15:37
  • 1
    The sagetex package gives you access to a computer algebra system as well as Python/matplotlib for calculations and/or graphing. This can give computer generated graphs for your LaTeX document or, to make everything pretty, you can force the calculations into tikz/pfgplots. See my answers to graphing Riemann Zeta and Cantor functions for comparison. – DJP Nov 14 at 16:18
  • 1
    PGF/TikZ generates vector graphics, which scale. I'm not sure if an imported matplotlib plot would have this, but I doubt it. And I don't see anyone else mentioning it. – Faheem Mitha Nov 14 at 20:29
  • 1
    Also, there are tools like R's TikZDevice which allow one to have one's cake and eat it too. Generate a plot with R, then use TikZDevice to render it using TikZ. – Faheem Mitha Nov 14 at 20:52
up vote 19 down vote accepted

Welcome to TeX.SE! I think that, as long as you feel comfortable with the way you are producing and embedding the plots, and are happy with the result, this should be the way to go. I personally use TikZ and pgfplots for sufficiently simple plots (after I switched there from pstricks). My main motivation are points that are not in your list:

  1. I do not have to open several files and adjust things.
  2. If I include the graph in a beamer presentation, it automatically comes with the right font as the ambient document. If I understand you correctly, if I were to use the method you suggest I would have to produce a separate pdf for the beamer presentation.
  3. I also use some macros. Even though I do not use many of them, I find it sometimes convenient to define some symbols via a macro. I can use this macro also in my plots and figures.
  4. Even though this is not a major issue, when working on something together with others on a file just works. I do not have to worry that my collaborators may not have downloaded another plot from the repository. Rather, once they have the file, they have all they need to compile the document. Likewise, my collaborators can also add figures and plots in the same way without having to worry about these issues.

Having said this, let me also mention that there are sufficiently complicated plots which I do not do with pgfplots or TikZ. After all, LaTeX is not a computer algebra system.

  • 2
    doesn't it add also the possibility to get e.g. arrows go from the text (or text margin) to some node in the pgf/TikZ picture? this is possible already even including graphics but you need to figure out absolute coordinates. With nodes from TikZ your document can evolve while maintaining the fancy decorations. – jfbu Nov 14 at 10:10
  • @jfbu Yes, this is another valid point (even though this is not one of the main reasons why I personally use pgfplots). – marmot Nov 14 at 11:50

Welcome to TeX.SE! I think there is not a default answer to your question per se. It more likely depends on the fact how you perceive TeX itself. If you merely view it as a system to format your text and structure your document, you are very likely to adopt the workflow to generate plots using your programming language than a system that it tightly integrated with TeX.

The viewpoint I, as a PhD student in signal processing, usually take instead is the following: I use TeX as a means of formatting and presenting my whole work and results in an appropriate fashion. This means that not only to I typeset my thoughts and arguments in TeX but also the simulation results and visualizations I can come up with.

That being said, I usually proceed the following way when it comes to plots:

  1. I write a whole bunch of simulation scripts that usually produce several MB of files containing some multidimensional simulation results and their parameters. These scripts tend to take very long computation wise and generate a whole bunch of data.
  2. I write an a lot smaller script that does the simulation data processing and this script produces two things. First it produces some very ugly plots, which only serve me as means of quickly debugging the data and the simulations in 1. And second at least one *.csv file, which usually only is a few KB only containing the data and parameters I actually want to plot. This can stem from averaging or applying some other kind of statistics to the data from 1.
  3. I use this *.csv file to generate plots using PGF by loading the data from it, which I can easily plug into a poster template, a beamer template or a paper template and a nicely composed selection of macros and color schemes make everything look very integrated with the rest of the document at hand.

What are the advantages of this approach you might ask and very rightfully so, since it seems to introduce one additional layer of complexity. The reasons for me doing this are in ascending order of importance

  1. It makes simulation data analysis more transparent, since it separates data generation from data evaluation.
  2. It makes my workflow more flexible, since the separation of simulation and evaluation allows one of the two to change without affecting the other. Especially when simulations take long, it is of tremendous value that the post-processing is decoupled from them.
  3. It forces me to think about the data structures within my simulation, which results in a more structured workflow itself.
  4. The decoupling of the post-processing (generating the *.csv-files) from the plotting yields again a more flexible workflow. After having generate all the data you need, you never ever have to touch it. It is just there and you can focus in TeX on the visualization.
  5. All data visualization parameters, so the parameters of your plots are at one place – in the TeX code – making it easier not to loose your mind.

Summarizing, it simply gives you more freedom, while also enforcing a more streamlined but structured workflow and puts things where they should be (IMHO), since programming is for data generation and TeX is amazing at making things look beautiful! :-)

EDIT: When collaborating with others it also is of tremendous value. If you only hand out raw or processed simulation results (which you can handle with git or any other similar tool quite nicely), you make it easier for others to modify the presentation of the data, since they do not need to run any code external to TeX to get things going.

  • 1
    Thanks for offering a new perspective. However, I think most of your points serve more to justifying the separation of simulation/computation from plotting than justifying plotting inside TeX. For example, I might well use one large script to do the simulation and get raw data, and use another small python script for plotting. I can also reuse the latter script for any future plotting tasks, to make my plots more consistent. Collaboration will also not be a problem as long as they also have python installed. – Junnan Nov 14 at 10:10
  • 1
    Well, yes and no. In the moment you are deciding what data to present you are often not in the position to know how it will be type set. You might have some simulation results and you are writing a paper and then you have to compose a poster, some slides or both. In this case you have to change your plots accordingly, since generally posters and slides use different font, have different requirements in presentation, since the audience perceives your work differently, so some line styles, arrangements and font sizes might not work in these cases. These adaptions should be handled by TeX. – Labello Nov 14 at 10:43

The main reason is to match the text, as you already said, but also the arrow tips or other similar symbols.

Another reason (valid in my case) is "not to have another tool for plotting graphs available".

Another advantage is that all lines will have the same width. Graphs produced by external tools are often scaled during the inclusion in the document to fit them in the available textwidth or similar. This will change the width of their lines.

  • :) Sorry for not explaining it clearly in the first place! I think this points really fits best into your answer because you already mention arrow tips etc. which will give a consistent design throughout the document. – samcarter Nov 21 at 10:17

PGFPlots has quite a few advantages. It does not only provide pretty graphs and tables, but also a bit of handling of data (csv reader, and a possiblity to embed the data direct in the document), such that you're sure that your tables and plots always show the same data. It also can calculate regressions and a lot more (see their examples gallery).

It is one nice way to do such things.

It also mentioned that no other plotting packages from R or python can generate graphs that have the same high image quality as those generated by pgfplots in LaTeX directly, but no examples or reasons were given. Is there evidence to back up this claim?

No. I created nice looking documents by embedding PDF/EPS generated with gnuplot and matplotlib (python). Some of them look more modern than pgfplots tables. Still it requires a bit more complicated build process (I use makefiles for this) and more experimenting until everything matches, padding and margin are nice and the fonts look the same.

PGFPlots is "just" a LaTeX package and follows the same philosophy, that you should not need to handle the details, but just provide the data and it will produce something nice looking.

For other functions of PGFPlots and TikZ than tables and plots I often prefer using SVGs (use inkscape for high-quality PDF export). They are not automatically generated, but you have more control over the look without using many TeX commands.

Something I used for the first time recently when writing a large and complicated report with numerous tables and graphs.

All the data was contained in spreadsheets and was adapted frequently. I exported the data from the spreadsheets using python. The python scripts directly created pgfplots syntax. I still have to create a makefile to re-extract data when something has changed before compiling the Latex file, but that is a detail.

My old way of doing this was to generate data files, GNUplot script files, run GNUplot and include the graphs in my Latex document.

Creating directly PGFplots skips two steps and simplify maintenance: avoid separate data files and the maintenance of the GNUplot files. The latter is by no means a small task, as it is harder to get the GNUplot output right to match the Latex document. When the graph syntax is in the Latex file it is much easier to develop, adjust and maintain.

Also quite relevant was that I had to include many graphs in my presentation with the same contents. It was done very quickly to include the same Tikz pictures in my Beamer slides. Although for correct presentation I had to include this in the presentation:

\begin{tikzpicture}
\begin{scope}[scale=0.6]
%code goes here
\end{scope}
\end{tikzpicture}

The scale is quite right while keeping everything else in the correct sizes. In a report the graphs need to be somewhat smaller. (Don't ever use shrink in Beamer!)

Last but not least, I had expected PGFplots to get slow on large data collections. It does not. Whereas I still have some PDFs to include from, created from SVG. That really slows down Latex compilation.

Separating input files (for generating PSTricks diagrams) from main input file (for writing the main document) is my recommended best practice because the diagram can be compiled separately to yield PDF outputs, so

  • compiling the main input file importing the PDF diagram become much much faster.

  • we can reuse the same diagram across many projects with consistent settings.

  • the main input file become much much readable and easy to maintain because the cluttering diagram code no longer exist. I call it separation of concern!

Diagrams created with PSTricks can use the same font used in the main input file by creating a private auxiliary package that is only for font inclusion for either diagram input files or main input file.

PGFPLOTS enables very precise control of line weights and the positioning of labels on charts, as well as exact control of the fonts used. For my purposes, this is great for plotting curves from various equations; and, as an opinion, the results can reach the quality formerly associated with engineering draftsmen (and largely lost when the world switched to Excel and its cousins). It's also nice that the charts are vectorized and infinitely scalable, and that (under Lualatex) one can integrate Lua code (or C code via FFI) into the computation and plotting of complex formulae.

For really simple drawings, PGFPLOTS is a lot more work than pasting up and exporting from PowerPoint.

Your Answer

By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

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