pgfplots:如何在/.code=... 中添加计算?

pgfplots:如何在/.code=... 中添加计算?

/.style在或中添加计算的正确语法是什么/.code

这不起作用:

scatter/@pre marker code/.code={
\pgfmathsetmacro\myheight{1.2*\zzz+0.2}
/pgfplots/cube/size z=\myheight
},  % works not

我需要做什么?

在此处输入图片描述

\documentclass[border=10pt, varwidth]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\usetikzlibrary{calc}
\usepackage{pgfplots}

\begin{document}
\pgfplotstableread[col sep=comma,header=true]{
X,   Y,    Z
0,    0,   10
1,    0,   5
4,    1,   0
4,    2,   0
}{\datatable}

\begin{tikzpicture}
\begin{axis}[]
\addplot3[scatter, mark=*, only marks,
mark=cube*, mark size=5,  
nodes near coords*=\coordindex,
visualization depends on={value \thisrow{Z} \as \zzz},
% Works
scatter/@pre marker code/.append style={
/pgfplots/cube/size z=15   
}, % works
% Works not
%scatter/@pre marker code/.code={
%\pgfmathsetmacro\myheight{1.2*\zzz+0.2}
%/pgfplots/cube/size z=\myheight
%},  % works not
] table [x=X, y=Y] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}

答案1

我也不知道为什么会失败。但尝试了几次后,我发现下面的代码是可行的。我认为直接使用 pgf 的数学计算功能可能会存在一些扩展问题。所以我使用 xfp 包进行计算。

\documentclass[border=10pt, varwidth]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\usetikzlibrary{calc}
\usepackage{pgfplots,xfp}

\begin{document}
\pgfplotstableread[col sep=comma,header=true]{
X,   Y,    Z
0,    0,   10
1,    0,   5
4,    1,   0
4,    2,   0
}{\datatable}

\begin{tikzpicture}
\begin{axis}
\addplot3[scatter, mark=*, only marks,
mark=cube*, mark size=5,
nodes near coords*=\coordindex,
visualization depends on={z \as \zzz},
% % Works
scatter/@pre marker code/.append style={
/pgfplots/cube/size z=\fpeval{12*\zzz+0.2}
},
] table [x=X, y=Y] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}

答案2

我找到了密钥处理程序/utils/exec=<code>,该程序在第 82.4.8 节中描述钥匙检查处理人员TikZ-手册(不在 pgfplots 手册中)。

所以

visualization depends on={\thisrow{Z} \as \zzz}, 
scatter/@pre marker code/.append style={
/utils/exec=\pgfmathsetmacro\myheight{2.2*\zzz+0.2},
/pgfplots/cube/size z=\myheight        
}, % works as well

可以使用。

\documentclass[border=10pt, varwidth]{standalone}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}
\usetikzlibrary{calc}
\usepackage{pgfplots}

\begin{document}
\pgfplotstableread[col sep=comma,header=true]{
X,   Y,    Z
0,    0,   10
1,    0,   5
4,    1,   0
4,    2,   0
}{\datatable}

\begin{tikzpicture}
\begin{axis}[]
\addplot3[scatter, mark=*, only marks,
mark=cube*, mark size=5,  
nodes near coords*=\coordindex,
visualization depends on={\thisrow{Z} \as \zzz}, 
% Works
%scatter/@pre marker code/.append style={
%/pgfplots/cube/size z=15   
%}, % works
% Works as well
scatter/@pre marker code/.append style={
/utils/exec=\pgfmathsetmacro\myheight{2.2*\zzz+0.2},
/pgfplots/cube/size z=\myheight        
}, % works as well
] table [x=X, y=Y] {\datatable};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容