带有 y 误差和坐标附近节点的条形图

带有 y 误差和坐标附近节点的条形图

我必须将一些数据绘制成条形图。这些数据用 处理pgfplotstable,因此我可以绘制数据的平均值和误差。现在我想打印nodes near coords每个条形图;我得到的唯一结果就是一条错误消息。请参阅下面带有数据的代码。我使用 LuaLaTeX 进行编译。

TL;DR-版本:当nodes near coordsy error相同的一起使用时,代码将无法编译axis

\documentclass{standalone}

\usepackage[detect-all=true]{siunitx}
\sisetup{
    output-decimal-marker={,},
    exponent-product=\cdot,
    text-micro={\fontfamily{mdbch}\textmu},
    math-micro=\muup
}

\usepackage{tikz}
\usepackage{pgfplots,pgfplotstable}

%% Code chunk for statistics starts here...
\newcommand{\calcrowmean}{
    \def\rowmean{0}
    \pgfmathparse{\pgfkeysvalueof{/pgfplots/table/summary statistics/end index}-\pgfkeysvalueof{/pgfplots/table/summary statistics/start index}+1}
    \edef\numberofcols{\pgfmathresult}
    % ... loop over all columns, summing up the elements
    \pgfplotsforeachungrouped \col in {\pgfkeysvalueof{/pgfplots/table/summary statistics/start index},...,\pgfkeysvalueof{/pgfplots/table/summary statistics/end index}}{
        \pgfmathparse{\rowmean+\thisrowno{\col}/\numberofcols}
        \edef\rowmean{\pgfmathresult}
    }
}
\newcommand{\calcstddev}{
    \def\rowstddev{0}
    \calcrowmean
    \pgfplotsforeachungrouped \col in {\pgfkeysvalueof{/pgfplots/table/summary statistics/start index},...,\pgfkeysvalueof{/pgfplots/table/summary statistics/end index}}{
        \pgfmathparse{\rowstddev+(\thisrowno{\col}-\rowmean)^2/(\numberofcols-1)}
        \edef\rowstddev{\pgfmathresult}
    }
    \pgfmathparse{sqrt(\rowstddev)}
}
\newcommand{\calcstderror}{
    \calcrowmean
    \calcstddev
    \pgfmathparse{sqrt(\rowstddev)/sqrt(\numberofcols)}
}

\pgfplotstableset{
    summary statistics/start index/.initial=1,
    summary statistics/end index/.initial=4,
    create col/mean/.style={
        /pgfplots/table/create col/assign/.code={% In each row ... 
            \calcrowmean
            \pgfkeyslet{/pgfplots/table/create col/next content}\rowmean
        }
    },
    create col/standard deviation/.style={
        /pgfplots/table/create col/assign/.code={% In each row ... 
            \calcstddev
            \pgfkeyslet{/pgfplots/table/create col/next content}\pgfmathresult
        }
    },
    create col/standard error/.style={
        create col/assign/.code={% In each row ... 
            \calcstderror
            \pgfkeyslet{/pgfplots/table/create col/next content}\pgfmathresult
        }
    }
}
%%...code chunk for statistics ends here

\usepackage{filecontents}
\begin{filecontents*}{data.csv}
    ch  1   2   3   4   5   6   7   8   9   10
    1   528 531 526 529 539 532 524 532 534 528
    2   477 506 454 477 462 458 473 510 480 512
    3   494 457 502 499 466 474 445 471 501 483
    4   525 511 515 524 530 505 527 511 504 528
    5   566 577 579 573 554 579 582 582 583 574
\end{filecontents*}

\begin{document}
    \pgfplotstableset{
        create on use/mean/.style={create col/mean},
        create on use/stddev/.style={create col/standard deviation},
        create on use/stderror/.style={create col/standard error}
    }
    \begin{tikzpicture}
    \begin{axis}[
    ybar,
    ymin=0,
    title={1.4.1a Gleichförmigkeit der Masse},
    /pgf/number format/use comma,
    width=\linewidth,
    grid=major,
    grid style={dashed,gray!30},
    x label style={at={(axis description cs:.5,-.025)},anchor=north},
    y label style={at={(axis description cs:.05,.5)},anchor=south},
    xlabel=Charge,
    ylabel=Masse~{[\si{\gram}]},
    xtick={1,...,5},
    xticklabels={R1,R2,R3,R4,R5},
    legend cell align=left,
    legend pos=north west,
    ]

    \addplot[
    fill=blue,
    draw=black,
    error bars/.cd,
    y dir=both,
    y explicit,
    ]
    table [x=ch,y=mean,y error=stderror] {data.csv};
    \addlegendentry[align=left]{Mittelwerte der Tablettenmasse\\mit Standardabweichung};
    \end{axis}
    \end{tikzpicture}
\end{document}

相关内容