来自 lua 的 pgf 散点图表

来自 lua 的 pgf 散点图表

我正在尝试从 lua 函数产生的点生成散点图。

我根据以下公式将其简化为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}

但我得到的不是散点图,而是:

包 pgfplots 错误:抱歉,表 '<inline_table>' 中请求的列号 '1' 不存在!?请确认您使用了正确的索引 0 <= i < N。

我曾尝试过,\pgfplotstableread但也无法让它工作。

我如何将数据传递test()到散点图?

答案1

我认为,存在扩展问题。您可以\addplot使用 lua 创建整个宏,但这可能不是您想要的。类似这样的方法也可以工作(请参阅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}

在此处输入图片描述

相关内容