3

Is it possible to let TikZ save and in subsequent compilation runs use the plot points generated with the PGF math library?

A (simple) example of such a function plotted that way is:

\begin{tikzpicture}
  \draw plot (\x,{sin(2*pi*\x r)});
\end{tikzpicture}

Here, the PGF math library is used to calculate sin(2*pi*\x r). For complex expressions, this is quite time-intensive. I assume this time could be saved if the plot points were available as is the case when using a gnuplot ‘function’ plot. Then (if the plot is given an id) the plot points generated by gnuplot are saved to a .table file, so that they need not be regenerated unless the plot parameters have changed.

2
  • The big problem is that TeX doesn't really support arrays. A Tikz array is just a comma delimited text string. Dec 9, 2013 at 2:15
  • 3
    May be this answer helps you: tex.stackexchange.com/a/60370/1952. You can create a data file computed with pgfplotstable and use it afterwards to plot it.
    – Ignasi
    Dec 9, 2013 at 9:09

1 Answer 1

5

You can use \pgfplotstablenew command from pgfplotstable (which comes with pgfplots) to create a file with your plot points and some conditional to create it when you want. Next example creates the file when doesn't exist.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots, pgfplotstable}

\begin{document}

\IfFileExists{myplot.dat}{}{
\pgfplotstablenew[
    create on use/plot1/.style={
        create col/expr={sin (2*pi*\pgfplotstablerow)}
    },
    columns={plot1}
]
{700}
\loadedtable
\pgfplotstablesave{\loadedtable}{myplot.dat}
}


  \begin{tikzpicture}
   \begin{axis}[
        line join=bevel,
        no markers,
        table/x expr={\coordindex},
        xmin=0,
        enlarge x limits=false,
        ymin=-1.5,
        ymax=1.5,
    ]
   \addplot[red] table [y expr={\thisrow{plot1}}] {myplot.dat};
\end{axis}

\end{tikzpicture}

\end{document} 

enter image description here

You must log in to answer this question.

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