1

I'm trying to generate a scatterplot from points produced by a lua function.

I've simplified this into the following mwe, based on https://tex.stackexchange.com/a/77864/9454:

\documentclass{article}

\usepackage{luacode}
\usepackage{tikz,pgfplots}

\begin{luacode*}
function test()
tex.print([[1 4.3 a]])
tex.print([[2 5.1 a]])
tex.print([[3 5.7 a]])
tex.print([[4 6.3 a]])
tex.print([[5 6.8 a]])
tex.print([[6 7.1 a]])
tex.print([[7 7.2 a]])
tex.print([[8 7.2 a]])
tex.print([[9 7.2 a]])
tex.print([[10 7.2 a]])
tex.print([[11 7.5 a]])
tex.print([[12 7.8 a]])
end
\end{luacode*}

\begin{document}

\begin{tikzpicture}
\begin{axis}[%
scatter/classes={%
    a={mark=o,draw=black}}]
\addplot[scatter,only marks,%
    scatter src=explicit symbolic]%
table[meta=label] {
x y label
\directlua{test()}
};
\end{axis}
\end{tikzpicture}
\end{document}

But instead of the scatter plot, I get:

Package pgfplots Error: Sorry, the requested column number '1' in table '<inline_table>' does not exist!? Please verify you used the correct index 0 <= i < N.

I've tried messing with \pgfplotstableread but I could not get that to work either.

How can I pass the data from test() to the scatter plot?

1
  • I've found a workaround in writing my table to a file using lua's io which I then load the data in from pgfplots, but that's not very sastisfying Feb 28, 2023 at 16:23

1 Answer 1

1

I think, there are expansion issues at work. You could create the whole \addplot macro using lua, which is probably not what you aim for. Something like this would also work (see A 576672):

\documentclass{article}
\usepackage{luacode}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18} 

\begin{luacode*}
function test()
    tex.print("{")
    tex.print("x y label \n")
    tex.print("1 4.3 a \n")
    tex.print("2 5.1 a \n")
    tex.print("3 5.7 a \n")
    tex.print("4 6.3 a \n")
    tex.print("5 6.8 a \n")
    tex.print("6 7.1 a \n")
    tex.print("7 7.2 a \n")
    tex.print("8 7.2 a \n")
    tex.print("9 7.2 a \n")
    tex.print("10 7.2 a \n")
    tex.print("11 7.5 a \n")
    tex.print("12 7.8 a \n")
    tex.print("}")
end
\end{luacode*}

\expandafter\pgfplotstableread\directlua{test()}\loadedtable

\begin{document}

\begin{tikzpicture}
\begin{axis}[scatter/classes={a={mark=o, draw=black}}]
\addplot[scatter, only marks, scatter src=explicit symbolic]
    table[meta=label] {\loadedtable};
\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 .