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?

  • 4
    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, 2018 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). Nov 14, 2018 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, 2018 at 16:18
  • 2
    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. Nov 14, 2018 at 20:52
  • 2
    @FaheemMitha matplotlib->pdf is vector graphics (and matplotlib can export svg if you want to tweak in something like inkscape, which then allows font-matching)
    – Chris H
    Nov 15, 2018 at 10:36

8 Answers 8


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.

  • 3
    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.
    – user4686
    Nov 14, 2018 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).
    – user121799
    Nov 14, 2018 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, 2018 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, 2018 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. Nov 21, 2018 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.


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.


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:

%code goes here

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.


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?

With respect R (phyton ignorant here!), there are a huge evidence of its ability to make of almost any type of graphs. As a small sample, the R graph gallery seem enough.

With respect to the printing quality, R can make graphs in bitmap formats (png, jpeg, bmp, tiff) than of course could have a poor quality when zoomed excessively in LaTeX or in another format, but the grDevices documentation states than R can also make vectorial images in xfig, ps, wmf, pdf and that other packages can include other devices outputting SVG and PGF/TiKZ, and even TeX/PicTeX files "(of historical interest only)".

To be practical, let's focus in PDF format: Are vectorial images that can be included directly in LaTeX documents, so the printing quality will by only limited by the printer resolution in dpi. But also can include LaTeX expressions in labels of R plots using R functions as TeX().

The only question then could be that sans serif default font of labels and axis of R plots does not match with the default roman "Computer Mode" font of LaTeX text. This could be regarded as a good "feature" (I really like a different font for plots) or a terrible "bug". In the last case, one solution could be change the PDF font type in R. In my Linux system, that could be one of ...

> names(pdfFonts())
 [1] "serif"                "sans"                 "mono"                
 [4] "AvantGarde"           "Bookman"              "Courier"             
 [7] "Helvetica"            "Helvetica-Narrow"     "NewCenturySchoolbook"
[10] "Palatino"             "Times"                "URWGothic"           
[13] "URWBookman"           "NimbusMon"            "NimbusSan"           
.... etc .....

So I can make:

> par(family="Palatino") # For example 
> plot(c(1,2,3),main="Plot with Times font")

This is only a example. It is also possible use any TT or OTF font in other ways. In any case, if that font does not match exactly with the LaTeX font, of course you can also change the LateX font, even to the same TT or OTF font using xelatex or lualatex.

But if the look and feel do not match still enough, or simply you do want to deal with fonts in one or both sides, a simpler solution is use knitr with the tikz device. The main reason of knitr is be better than Sweave embedding R source in LaTeX documents.

For those that do not know what I'm talking about, just explain that this mixed document (LateX+R) usually have the .Rnw extension (mean "R noweb" format) and can be exported by R to a pure LaTeX document (the .tex version) where the R code is replaced by the R output (optionally with echo of source R code, beautifully highlighted) that can be processed by LaTeX compilers as usually (with Rstudio editor all the process is just press one button). This means that if the data changed, just a new export-compilation of the document will update all plots, and always in concordance with any tables and text results generated automatically by R.

With respect to the examples of R plots, there are many in this, somo site using knitr and the tikz device, as this 2D plot and these 3D (or 4D) plots (sorry for the autocites).

Beside be scalable images with LaTeX fonts, that LaTeX+R approach with a powerful statistical environment behind the scenes is a main reason to go with R plots, as this allow a reproducible research.

With respect python (phyton ignorant here!) I can say only that knitr is able to manage chunks of python (see Can we import Python file in latex? ) in a .Rnw file, so probably similar considerations can be made about their plots.


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.

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .