TeX - LaTeX Asked by burny on September 25, 2021
I am writing a fairly large article with a lot of different plots.
They range from simple ones like abs(x)
or x^2
to high-polynomials with their first and second derivative, and nonlinearities (sigmoid, tan, etc.). Sometimes they are also in 3 dimensions.
I am using pgfplot within tikzpicture and then gnuplot. For the easy plots, this is straightforward. However, the more complex plots (that may include non-continuity points), the gnuplot expression becomes harder and harder to understand and maintain.
For some plots, I need randomly created points anyway (created once, and then fixed), which need to be "classified" according to a function.
For all plots, I have (or could create) Python scripts to output a coordinates table that would be possible to use within the pgfplot table import functionality. This may seem overkill for x^2 but is really necessary for the more complex plots. And with the higher polynomials, I don’t have to do the derivates manually.
This works for plots that are points or lines in 2D but not in 3D because the number of points for the imported table exceeds LaTeX’s memory (x and y have 1000 samples each, which results in 1,000,000 coordinates).
As I have the functions in Python anyway, I could also use matplotlib (or seaborn, etc.) to create pdfs with the plot. This has the disadvantage that the plots do not look like they would when drawing with LaTeX/gnuplot (different colors, styles, etc.), so I would have to maintain the styles in two different places.
Is there a best practice for this type plotting? I want one type of plot (either only gnuplot or only Python) and not a mixture of different methods.
I had a similar problem. What I did is "externalise" the generation of the plots. You do this by including
usepgfplotslibrary{external}
tikzexternalize
In your preamble. You have to call Latex with the additional options: -shell-escape. This will cause the compiler to start from scratch for each tikz-picture so your compilation will no longer accumulate memory consumption. There will be an individual file for each picture and they will be nicely combined into one document as well.
This being said, 1M points is still too much. And frankly, I don't think it is necessary to have a nice quality plot. You should reduce that anyway. Play around with the number of points (You have it in Python anyway so it should not be that hard.) I try to find the limit by trial and error. You might want to create and debug the picture in a separate document in order to get the error-log.
Answered by Michel Janssens on September 25, 2021
I went through this problem years ago. In my cases I wanted to make sure I my tikz/pgfplots
Now simple functions, such as what you'd see in a high school are easily done with template 1: I posted Tikz plot x^a*cos(ln(x)). You'll see there are many settings where the axis environment is set. The line commented out %grid = both,%grid for major ticks (every integer) and minor ticks
is preventing graph paper background.
After experimenting a bit I found that the sagetex
package was capable of the issue you are concerned with: handling more complicated functions. The documentation is found on CTAN here; the package gives you access to a computer algebra system (CAS), called SAGE, along with Python programming. From the CAS you get more complicated functions such as erf, riemann zeta, fourier, and so on. The SAGE documentation on built in functions is here. You also get accurate calculations, the ability to compute derivatives and integrals, and the ability to choose the points you graph. Since SAGE is not part of your LaTeX distribution it needs to be installed on your computer or you need to open a free Cocalc account. The free account is the easiest way to get up and running.
After pushing the Build
button in Cocalc there is a 3 step process compilation process that goes on: first LaTeX is run. Second, SAGE runs on the content it needs to. Third, LaTeX is run on the final product consisting of LaTeX plus SAGE content. In order to get through compilation without errors I've found it easiest to create a raw string in Python which will take any calculations needed (such as coordinates in a plot) and create the text that will run in LaTeX. Template 2: for 2d plotting requiring SAGE is used in many posts on this site; for example: how to plot reciprocal of Gamma function in Latex. Notice that I imported the function with from sage.functions.other import gamma1
and used that in two ways. It's first plotted as a sageplot
which is showing you the plot created by SAGE. The second plot shows the result when the coordinates are pushed through pgfplots to get the second plot shown. It's similar to the first template except now I'm putting the code through a string so SAGE can do the typesetting. Since my axis environment says output += r"grid = none,"
there will be no graph paper. In the post Plotting implicitly in Tikz you'll see the same basic template used to plot implicitly. Note the graph paper background comes from output += r"grid = both,"
. In addition to these 2 templates for plotting 2d, there is template 3: for 3d plots requiring SAGE in Tikz plotting 3d. Once you're comfortable with the process you can easily use SAGE as the engine for other plots not using the templates discussed. For example the post
buf_size=1000000 pdflatex -synctex=1 -interact=nonstopmode MyFile.tex
.In summary, you can use the sagetex
package along with the three templates to create 2d/3d plots with the same look and feel, regardless of how complicated there are. The use of Python keeps code simpler to read and revise and even lets you force the plotting of specific points. You can easily increase the size of the buffer to handle more data. SAGE can handle differentiation as well as integration. You can also use matplotlib and SAGE plots if you want. There are other examples posted on this site you can search for as well.
What's best practice? I don't know that has an answer to satisfy everyone. However, the sagetex
package can handle the problems you have mentioned.
Answered by DJP on September 25, 2021
Get help from others!
Recent Answers
Recent Questions
© 2024 TransWikia.com. All rights reserved. Sites we Love: PCI Database, UKBizDB, Menu Kuliner, Sharing RPP