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-manual에는 없습니다).

그래서

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}

여기에 이미지 설명을 입력하세요

관련 정보