6

This is slightly an opinion question, so I will try to phrase it in a controlled way.

I am a great fan of pgfplots and LaTeX in general, but recently I have been thinking about their practicality and real-life use cases. To be more specific, I have been looking at alternative plotting systems for scientific data. My current approach - since I code in python usually - is that I do extraploratory data visualization with python (matplotlib but I will definitely move to seaborn or something else) and when I need to plot publication grade figures I export my data using python in a controlled way, putting data in a separate .dat file so pgfplots can get it easily, and in general setting up the data in a very precise way, since doing calculations in LaTeX is a serious no-no compared to python or R. I also found I have to manually limit the number of points in the data file, because pgfplots is slow and heavy compared to e.g. matplotlib.

So I think this drives my point home, that the process is a bit tedious.

My question: What is the raison d'être of pgfplots in data visualisation? To be more specific: what are the use-cases where pgfplots shines compared to its alternatives?

fbence
  • 1,585
  • "doing calculations in LaTeX is a serious no-no compared to python or R" → You name it. If you want a fluent integration (for simpler tasks) use pgfplots and if you want correct and efficient computation results use an external tool. It's a question of taste and aims. – TeXnician Apr 04 '18 at 08:24
  • @TeXnician I personally find exporting the data of final figures acceptable practice, since even using say matplotlib, when you are doing the final tweaking, you do not want to crunch data unecessarily every time you modify something. Are you saying that pgfplots's only benefit is that I can make a document where the plot is made in situ? Since you pretty much always need separate figure files anyway, that seems to me like a very-very marginal use case. – fbence Apr 04 '18 at 08:33
  • I think you are making a mistake assuming pgfplots and matplotlib are eqaully valid choices for data-visualization. On rare occations I did use pgfplots to construct graphs datapoint by datapoint manually. For visualizing actually gathered data I would never ever use pgfplots. I used to use gnuplot for this task, just because it was the first tool I ever used for this and it never let me down ... as long as there are only a few datapoints. – Bananguin Apr 04 '18 at 09:26
  • It's a rather personal opinion, but gnuplot is nice, but I think it is ugly and it is also a separate program. I sometimes use it for quick and dirty plots of datafiles but never for anything serious. But anyhow, you are saying that you would not really use it to plot data. Then what would you use pgfplots for? – fbence Apr 04 '18 at 12:28
  • I wasn't happy with gnuplot output, either so I adopted a hybrid approach. By using the sagetex package you get a CAS plus Python for accurate calculations which are then given to pgfplots to make it look better. See, for example, my answer here in plotting the prime function or here for drawing the Dragon's curve fractal. – DJP Apr 04 '18 at 14:58
  • I didn't know what sage is before googling, but incidentally, I stumbled upon matplotlib2tikz which outputs matplotlib images as a pgfplots compatible tikzpicture code, which can then be used to tweak the actual output. – fbence Apr 04 '18 at 15:08

1 Answers1

15

Lately, I'm more involved with Python development (as being a member of the dev team for SciPy, writing packages for Python etc.) and I have been using TikZ based tools for over a decade now I can offer my own perspective.

First of anything, you cannot do serious numerical computations with TeX. You can do, to some extent, with pgfplots with its internal fpu format which tries to be IEEE-compliant as much as it can. But still, it is not at a production-ready state. Given that it is maintained by a single person, I think that is already incredible to have a plot patch and you can get a surface plot within TeX. Also, the amount of data you can squeeze in for a single PDF page in terms of resources doesn't even come close to satisfactory for today's standards. The visualizations you have mentioned often originate from massive datasets and very sophisticated computations for which TeX allows none. Besides, every day a new interactive tool emerge and I am a huge fan of bokeh package in Python. If I'm not typesetting anything that is my go-to tool these days.

This has been discussed multiple times on this site but as a summary: TeX is old, obscure, and orphaned. If compared to contemporary tools such as Python or R, there is nothing good left in its arsenal in terms of programming convenience. Mostly it is a collection of annoying token collection and expansion.

And this is perfectly fine because it is set to do one single thing: typesetting pages. And it does it amazingly, there is no contest, it is still the best. But in my opinion, this should be the fault of others instead of the success of TeX. Because it is really a shame of other parties especially academia being the major consumer of TeX for not placing TeX on a professional "modern" foothold and hence cutting its ties to all advances in programming languages.

Then why would you bother using pgfplots? The main reason is linked to the above discussion. TeX is the game in town when it comes to typesetting and an integrated plotting tool is essential to have matching graphics. I'm skipping the argument why it looks ridiculously bad if you copy/paste from other sources which is the very reason why these tools exist. Back in the day, PSfrag was attempting a similar thing by just replacing the text parts via its TeX-typeset version within EPS images. But that wasn't sufficiently successful. With tools like TikZ and pgfplots you have an extremely convenient API to PostScript objects to insert vector graphics. But let me emphasize, this is only to generate visualizations that match the rest of the document. These packages are not meant for generic-use graphic software. In fact, TikZ recursion is actually based on this fact.

"TikZ ist kein Zeichenprogram" (German for: TikZ is not a software for drawing.)

These tools are meant for visually stunning document typesetting. And if the data you have, is within the capabilities of pgfplots none of those packages in R or Python would match the image quality that you would get from pgfplots. I have typeset my thesis with TikZ and pgfplots which have images with carefully downsampled data just to be able to use these tools and the results are amazing. But I was using pythontex to reach out to my Python script and do the heavy lifting there and using those results. Instead of typing 195 lines in matplotlib and getting a mediocre line plot, I do 10 lines of pgfplots and I get a great result in terms of visual quality.

For more powerful graphics, we also have PSTricks, Asymptote, METAPOST but they don't come with the convenience of TikZ API which is unfortunate. One can only hope.

percusse
  • 157,807
  • 1
    Are you saying, that alternatives like matplotlib, seaborn or ggplot do not make as nice outputs as pgfplots or that it is very hard to tinker with them to get them what you would like to do? – fbence Apr 04 '18 at 12:00
  • @fbence Yes, for my taste, that is correct that pgfplots has nicer output. And also it is very very tedious to get the others to the level of pgfplots. – percusse Apr 04 '18 at 12:01
  • You mention two other things, I will brake them up into two comment: "the amount of data you can squeeze in for a single PDF page in terms of resources doesn't even come close to satisfactory for today's standards": You mean the pdf format is not capable of storing enough data compared to other vector graphical formats? – fbence Apr 04 '18 at 12:10
  • "TeX is old, obscure and orphaned. If compared to contemporary tools such as python or R, there is nothing good left in its arsenal in terms of programming convenience. Mostly it is a collection of annoying token collection and expansion." Isn't the lualatex project about solving this issue? – fbence Apr 04 '18 at 12:11
  • TeX has its own internal resources which LuaTeX help to increase massively but still you can hit the limits pretty easily. If you increase too much then you have to wait a few years for compilation so practically it's not solved. Also theoretically there is a PDF spec that limits the number of objects though it is really difficult to reach via TeX alone. Also LuaTeX doesn't change the way TeX works. – percusse Apr 04 '18 at 13:18
  • +1: Nice summary. I edited the text a bit - hope that's ok! – Dr. Manuel Kuehner Nov 26 '21 at 22:42