带有 \ifnum 和 \pgfkeysvalueof{/data point/y} 的条件子句

带有 \ifnum 和 \pgfkeysvalueof{/data point/y} 的条件子句

我遇到了以下问题。我的目标是用 tikz 构建一个条形图。使用函数显示堆叠 ybar 中条形的总和,我想在 x 轴下方为一个特定总和添加上标。

        show sum below/.style={
            /pgfplots/scatter/@post marker code/.append code={%
                \node[font=\footnotesize,
                at={(normalized axis cs:%
                    \pgfkeysvalueof{/data point/x},%
                    0)%
                },
                anchor=north,
                ] { 
                    \ifnum\pgfmathparse{\pgfkeysvalueof{/data point/y}}=2,500
                        {\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}\textsuperscript{$3$}};
                    \else
                        {\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
                    \fi};
            },
        },

在此代码中,x 轴下方的总和显示基本上是相应条的最高点。

不幸的是,我在将 /数据点/y 移交给 if 子句时遇到了问题。

有什么想法可以解决这个问题以便为条件建立正确的输入?

下面是一个最小工作示例:

\documentclass[12pt]{article}
\usepackage[utf8]{inputenc}
\usepackage{threeparttable}
\usepackage{tikz}
\usetikzlibrary{arrows,decorations.pathmorphing,backgrounds,fit,positioning,shapes.symbols,chains, calligraphy}
\usepackage{pgfplots, pgfplotstable}
    \pgfplotsset{
        compat=1.14,
    }

\begin{document}



\begin{figure}[th]
     \centering
     \begin{threeparttable}[t]
     \begin{tabular}{@{}c@{}}
        \begin{tikzpicture}
        
        
        \edef\mylst{"1963", "1964"}

        
      \pgfplotsset{
        
        show sum below/.style={
            /pgfplots/scatter/@post marker code/.append code={%
                \node[font=\footnotesize,
                at={(normalized axis cs:%
                    \pgfkeysvalueof{/data point/x},%
                    0)%
                },
                anchor=north,
                ] { 
                    \ifnum\pgfmathparse{\pgfkeysvalueof{/data point/y}}=375
                        {\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}\textsuperscript{$3$}};
                    \else
                        {\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
                    \fi};
            },
        },
        
    }


\begin{axis}[
            ybar stacked, 
            xticklabels={{1963}, {1964}},
            symbolic x coords = {{1963}, {1964}},
            axis y line       = none,
            axis x line       = none,
            nodes={text width=25mm, text depth=,
            align=center, font=\tiny},
            nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
            scale only axis,
        ]
            

            % Data 1
            \addplot[text=black
                ] coordinates {
                ({1963}, 50) 
                ({1964}, 125)
            };
            % Data 2
            \addplot[text=black
                ] coordinates {
                ({1963}, 250) 
                ({1964}, 250)
            };
            % Total 
            % Here point meta=explicit symbolic to hide the helping values 1e-500 as 0 leads to hiding sums
            \addplot[show sum below, point meta=explicit symbolic] coordinates {
                ({1963}, 1e-500) 
                ({1964}, 1e-500) 
            };
            
            
        \end{axis}


        \end{tikzpicture}
     \end{tabular}
     
     \begin{tablenotes}\footnotesize
     \item[$1$] Comment numero uno
   \end{tablenotes}
\end{threeparttable}%
\end{figure}

\end{document}

答案1

您不能使用\ifnum\pgfmathparse{...}=375,因为\pgfmathparse无法通过扩展起作用。

您还应该将数据转换为可以实际使用的格式。

% first parse the number
\pgfmathfloatparsenumber{\pgfkeysvalueof{/data point/y}}
% then store in \pgfmathresult the value in fixed point notation
\pgfmathfloattofixed{\pgfmathresult}
% store 1 if equal and 0 otherwise in \pgfmathresult
\pgfmathparse{\pgfmathresult==375 ? 1 : 0}
% now look whether we have equality or not
\ifnum\pgfmathresult=1

你会成功的。

在第一次调用中,的连续值\pgfmathresult将是

1Y3.0e2]
300.00000000
0

在第二次通话中

1Y3.75e2]
375.000000000
1

完整代码

\documentclass[12pt]{article}

\usepackage{threeparttable}
\usepackage{tikz}

\usetikzlibrary{
  arrows,
  decorations.pathmorphing,
  backgrounds,
  fit,
  positioning,
  shapes.symbols,
  chains,
  calligraphy
}

\usepackage{pgfplots, pgfplotstable}

\pgfplotsset{
  compat=1.14,
}

\begin{document}



\begin{figure}[htp]
\centering

\begin{threeparttable}[t]
\begin{tabular}{@{}c@{}}
  \begin{tikzpicture}
  \def\mylst{"1963", "1964"}
  \pgfplotsset{
    show sum below/.style={
      /pgfplots/scatter/@post marker code/.append code={
        \node[
          font=\footnotesize,
          at={(normalized axis cs:\pgfkeysvalueof{/data point/x},0)},
          anchor=north,
        ]{
          \pgfmathfloatparsenumber{\pgfkeysvalueof{/data point/y}}
          \pgfmathfloattofixed{\pgfmathresult}
          \pgfmathparse{\pgfmathresult==375 ? 1 : 0}
          \ifnum\pgfmathresult=1
            \pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}\textsuperscript{$3$};
          \else
            \pgfmathprintnumber{\pgfkeysvalueof{/data point/y}};
          \fi
        };
      },
    },
  }
  \begin{axis}[
    ybar stacked, 
    xticklabels={{1963}, {1964}},
    symbolic x coords = {{1963}, {1964}},
    axis y line       = none,
    axis x line       = none,
    nodes={
      text width=25mm,
      text depth=,
      align=center,
      font=\tiny
    },
    nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
    scale only axis,
  ]
  % Data 1
  \addplot[text=black] coordinates {
    ({1963}, 50) 
    ({1964}, 125)
  };
  % Data 2
  \addplot[text=black] coordinates {
    ({1963}, 250) 
    ({1964}, 250)
  };
  % Total 
  % Here point meta=explicit symbolic to hide the helping values 1e-500 as 0 leads to hiding sums
  \addplot[show sum below, point meta=explicit symbolic] coordinates {
    ({1963}, 1e-500) 
    ({1964}, 1e-500) 
  };
  \end{axis}
  \end{tikzpicture}
\end{tabular}
     
\begin{tablenotes}\footnotesize
  \item[$1$] Comment numero uno
\end{tablenotes}
\end{threeparttable}

\end{figure}

\end{document}

在此处输入图片描述

答案2

问题是数据值\pgfkeysvalueof{/data point/y}以浮点格式存储。(此外,\ifnum\pgfmathparse无论如何都不能用于这种组合。)为了使用,\ifnum您希望它是一个纯整数。所以你需要转换它。下面的代码可以做到这一点,但可能不是最简单的方式。

\pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
\pgfmathsetmacro\myy{\pgfkeysvalueof{/data point/y}}%
\pgfkeys{/pgf/fpu=false}%

此外,在 pgf 中进行比较通常很有优势,因为这也适用于非整数。

\pgfmathtruncatemacro\itest{\myy==375}%

\itest然后就可以安全地在语句中使用结果了\ifnum

\documentclass[12pt]{article}
\usepackage{threeparttable}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}

\begin{document}



\begin{figure}[th]
     \centering
     \begin{threeparttable}[t]
     \begin{tabular}{@{}c@{}}
        \begin{tikzpicture}
        
        
        \edef\mylst{"1963", "1964"}

        
      \pgfplotsset{        
        show sum below/.style={
            /pgfplots/scatter/@post marker code/.append code={%
                \node[font=\footnotesize,
                at={(normalized axis cs:%
                    \pgfkeysvalueof{/data point/x},%
                    0)%
                },
                anchor=north,
                ] {\begingroup
                    \pgfkeys{/pgf/fpu,/pgf/fpu/output format=fixed}%
                    \pgfmathsetmacro\myy{\pgfkeysvalueof{/data point/y}}%
                    \pgfkeys{/pgf/fpu=false}%
                    \pgfmathtruncatemacro\itest{\myy==375}%
                    \ifnum\itest=0
                        {\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}\textsuperscript{$3$}};
                    \else
                        {\pgfmathprintnumber{\pgfkeysvalueof{/data point/y}}};
                    \fi
                   \endgroup    
                    };
            },
        },
        
    }


\begin{axis}[
            ybar stacked, 
            xticklabels={{1963}, {1964}},
            symbolic x coords = {{1963}, {1964}},
            axis y line       = none,
            axis x line       = none,
            nodes={text width=25mm, text depth=,
            align=center, font=\tiny},
            nodes near coords={\pgfmathprintnumber\pgfplotspointmeta},
            scale only axis,
        ]
            

            % Data 1
            \addplot[text=black
                ] coordinates {
                ({1963}, 50) 
                ({1964}, 125)
            };
            % Data 2
            \addplot[text=black
                ] coordinates {
                ({1963}, 250) 
                ({1964}, 250)
            };
            % Total 
            % Here point meta=explicit symbolic to hide the helping values 1e-500 as 0 leads to hiding sums
            \addplot[show sum below, point meta=explicit symbolic] coordinates {
                ({1963}, 1e-500) 
                ({1964}, 1e-500) 
            };
            
            
        \end{axis}


        \end{tikzpicture}
     \end{tabular}
     
     \begin{tablenotes}\footnotesize
     \item[$1$] Comment numero uno
   \end{tablenotes}
\end{threeparttable}%
\end{figure}

\end{document}

相关内容