在 \addplot 中声明的 Pgfplot 坐标

在 \addplot 中声明的 Pgfplot 坐标

我正在尝试绘制具有不同数据集(不超过 4 个)的 .csv 中的数据。我添加了一些额外的输入参数仅用于调试,目前文档如下所示:

\documentclass[border=5pt]{standalone}

\usepackage{xcolor}
\usepackage{pgfplots,pgfplotstable}

\pgfplotsset{compat=1.18}

\newcommand{\plotCenterDelta}[6][0cm]{
  % store the number of data points
  \pgfplotstablegetrowsof{#5}
  \pgfmathsetmacro{\N}{int(\pgfplotsretval-1)}

  % draw invisible points on the lower left corners of the error rectangles
  % using the previously defined style
  \addplot [xshift={0.5*\pgfplotbarwidth}, only marks, forget plot,] table [
      x expr=\thisrow{#2},
      y expr=\thisrow{#3}+\thisrow{#4},
  ] {#5}
    % set a coordinate on each data point
    \foreach \i in {0,...,\N} {
        coordinate [pos=\i/\N] (ll \i)
        node [anchor=south,pos=\i/\N] {ll \i}
    }
  ;
  % draw invisible points on the upper right corners of the error rectangles
  % using the previously defined style
  \addplot [xshift={-0.5*\pgfplotbarwidth}, only marks, forget plot,] table [
      x expr=\thisrow{#2},
      y expr=\thisrow{#3}-\thisrow{#4},
  ] {#5}
    % (same as for the lower left corners)
    \foreach \i in {0,...,\N} {
        coordinate [pos=\i/\N] (hh \i)
        node [anchor=north,pos=\i/\N] {hh \i}
    }
  ;
  % draw the error rectangles with the help of the created coordinates
  \pgfplotsforeachungrouped \i in {0,...,\N} {
    \edef\temp{\noexpand%
      \path[preaction={clip,postaction={line width=3pt, #6}}] ([xshift=#1]ll \i) rectangle ([xshift=#1]hh \i);
    }\temp
  }
}

\begin{document}

\begin{tikzpicture}
\pgfplotstableread{
    x   y       y_delta
    1   0.5     0.15
    2   0.3     0.1
    3   0.2     0.3 
    4   0.2     0.3 
}{\filea}
\pgfplotstableread{
    x   y       y_delta
    1   0.9     0.15
    2   0.7     0.2
    3   0.1     0.5
    4   0.2     0.3 
}{\fileb}
\begin{axis}[bar width=0.35cm,]
  \plotCenterDelta[-0.175cm]{x}{y}{y_delta}{\filea}{draw=blue,fill=blue!20}
  \plotCenterDelta[0.175cm]{x}{y}{y_delta}{\fileb}{draw=red,fill=red!20}
\end{axis}
\end{tikzpicture}
\end{document}

当两个文件的大小相同时,它可以正常工作,但当 \fileb 的行少于第一行时,就会出现问题。

答案1

描述

问题是, 不是\N本地的,代码稍后执行。因此,\N使用 的最后状态。这也可以看出,如果您从 中删除一行并\filea注释掉,它可以正常工作,但如果将被设置为 3 ,它就无法正常工作。\plotCenterDelta\fileb\fileb\N

改编

  • 添加了另一个参数#7= name,用于使其\N唯一,通过使用\csname N#7\endcsname其作为参数名称
  • 打印两个变量的值仅用于测试和比较
  • 更改了角点的代码,使其符合评论中的描述,方法是将其添加#1xshift内部\addplot
  • etoolbox添加了要使用的包\csuse{N#7},而不是\csname N#7\endcsname
  • 命令中添加参数说明\plotCenterDelta

代码

\documentclass[border=5pt]{standalone}

\usepackage{xcolor}
\usepackage{pgfplots,pgfplotstable}
\usepackage{etoolbox}

\pgfplotsset{compat=1.18}

\newcommand{\plotCenterDelta}[7][0cm]{
    % [#1] = xshift
    %  #2  = name of column x
    %  #3  = name of column y
    %  #4  = name of column y_delta 
    %  #5  = filename
    %  #6  = tikz options
    %  #7  = name
    %
    % store the number of data points
    \pgfplotstablegetrowsof{#5}
    \expandafter\pgfmathsetmacro\csname N#7\endcsname{int(\pgfplotsretval-1)}
    
    % just for test / comparison:
    \pgfmathsetmacro{\N}{int(\pgfplotsretval-1)}
    \node[yshift=-#1] (a) at (1,0) {#7 \csuse{N#7} \N};
    
    % draw invisible points on the lower left corners of the error rectangles
    % using the previously defined style
    \addplot [xshift={0.5*\pgfplotbarwidth+#1}, only marks, forget plot,] table [
        x expr=\thisrow{#2},
        y expr=\thisrow{#3}+\thisrow{#4},
    ] {#5}
    % set a coordinate on each data point
    \foreach \i in {0, ..., \csuse{N#7}} {
        coordinate [pos=\i/\csuse{N#7}] (ll \i)
        node [right, pos=\i/\csuse{N#7}] {ll \i}
    }
    ;
    % draw invisible points on the upper right corners of the error rectangles
    % using the previously defined style
    \addplot [xshift={-0.5*\pgfplotbarwidth+#1}, only marks, forget plot,] table [
        x expr=\thisrow{#2},
        y expr=\thisrow{#3}-\thisrow{#4},
    ] {#5}
    % (same as for the lower left corners)
    \foreach \i in {0, ..., \csuse{N#7}} {
        coordinate [pos=\i/\csuse{N#7}] (hh \i)
        node [below, pos=\i/\csuse{N#7}] {hh \i}
    }
    ;
    % draw the error rectangles with the help of the created coordinates
    \pgfplotsforeachungrouped \i in {0, ..., \csuse{N#7}} {
        \edef\temp{\noexpand%
            \path[preaction={clip,postaction={line width=3pt, #6}}] (ll \i) rectangle (hh \i);
        }\temp
    }
}

\begin{document}

\begin{tikzpicture}
\pgfplotstableread{%
    x   y       y_delta
    1   0.5     0.15
    2   0.3     0.1
    3   0.2     0.3 
    4   0.2     0.3 
}{\filea}
\pgfplotstableread{
    x   y       y_delta
    1   0.9     0.15
    2   0.7     0.2
    3   0.1     0.5
}{\fileb}
\begin{axis}[bar width=0.35cm,]
  \plotCenterDelta[-0.175cm]{x}{y}{y_delta}{\filea}{draw=blue,fill=blue!20}{a}
  \plotCenterDelta[0.175cm]{x}{y}{y_delta}{\fileb}{draw=red,fill=red!20}{b}
\end{axis}
\end{tikzpicture}
\end{document}

结果

在此处输入图片描述

相关内容