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}

ここに画像の説明を入力してください

関連情報