Pgfplot-将平均值添加到直方图(条形图)

Pgfplot-将平均值添加到直方图(条形图)

我有许多格式的数据文件count: frequency of count

1:  3691
2:  1492
3:  651
4:  922
5:  818
6:  1475
7:  335
8:  286
9:  165
10:     152
11:     208
12:     197
13:     137
14:     159
15:     87
16:     84
17:     40
18:     20
19:     37
20:     16
21:     38
22:     25
23:     17
24:     9
25:     18
26:     21
27:     4
28:     6
29:     3
30:     1
31:     5
33:     1
34:     1
35:     4
36:     3
37:     1
38:     2
39:     8
40:     3
42:     1
44:     1
46:     1
47:     12
52:     2
58:     1
83:     1
88:     1

我想将其绘制为计数直方图(我可以做到)和平均计数(我做不到)。这是目前所拥有的:

\documentclass[]{standalone}

\usepackage{tikz}
\usepackage{pgfplots}

\begin{document}

\maketitle 

\begin{tikzpicture}
    \begin{axis}[
        xtick distance=10
    ]
        \addplot[ybar interval] table [x, y, col sep=colon] {results/data.txt};

        % this line is sort of what I want but I want the 7 to be calculated automatically as the average count 
        %\draw[] (axis cs:7, \pgfkeysvalueof{/pgfplots/ymin}) -- (axis cs:7, \pgfkeysvalueof{/pgfplots/ymax});

    \end{axis}
\end{tikzpicture}

\end{document}

我发现这个例子但数据格式不同,所以我不确定如何使其适应我的情况。

这是我想要的一个例子:在此处输入图片描述

谁能告诉我如何自动计算平均计数并将其添加到我的图中?

答案1

我不知道您想如何计算平均值,因此这里有一个如何从表格中计算平均值的示例。

请注意,任何时候都不允许任何值超过 16383.9999。否则您将得到尺寸太大错误。为了防止这种情况,您可以在求和时将某个值除以某个常数,然后再乘以平均值。

代码:

编辑:结果的表和宏名称现在作为参数传递。

\documentclass[]{article}
\usepackage{pgfplots}

\pgfplotstableread[col sep=colon]{pgfplots-histo-average.dat}\datatable

% old code, table and macros for results fixed
%\newcommand*{\GetAverage}{%
%    \pgfmathsetmacro\dtcsum{0}%
%    \pgfmathsetmacro\dtfsum{0}%
%    \pgfmathsetmacro\dtcfsum{0}%
%    \pgfmathsetmacro\dtcnt{0}%
%    \pgfplotstableforeachcolumnelement{[index]0}\of\datatable\as\cellValue{%
%        \pgfplotstablegetelem{\pgfplotstablerow}{[index]1}\of\datatable
%        \pgfmathsetmacro{\dtfreq}{\pgfplotsretval}
%        % now: \cellValue = count; \dtfreq = frequency
%        % calculate sums
%        \pgfmathsetmacro{\dtcsum}{\dtcsum + \cellValue}%
%        \pgfmathsetmacro{\dtfsum}{\dtfsum + \dtfreq}%
%        % dividing \cellValue by 100 prevents dimension to large error
%        \pgfmathsetmacro{\dtcfsum}{\dtcfsum + (\cellValue / 100 * \dtfreq)}%
%        \pgfmathsetmacro{\dtcnt}{\dtcnt + 1}%
%    }
%    % calculate the averages
%    \pgfmathsetmacro{\dtcavg}{\dtcsum / \dtcnt}%
%    \pgfmathsetmacro{\dtfavg}{\dtfsum / \dtcnt}%
%    \pgfmathsetmacro{\dtcfavg}{\dtcfsum / \dtcnt * 100}%
%}
%\GetAverage

% new code, table and macros for results as parameters
\newcommand*{\GetAverage}[4]{%
    \pgfmathsetmacro\dtcsum{0}%
    \pgfmathsetmacro\dtfsum{0}%
    \pgfmathsetmacro\dtcfsum{0}%
    \pgfmathsetmacro\dtcnt{0}%
    \pgfplotstableforeachcolumnelement{[index]0}\of#1\as\cellValue{%
        \pgfplotstablegetelem{\pgfplotstablerow}{[index]1}\of#1
        \pgfmathsetmacro{\dtfreq}{\pgfplotsretval}
        % now: \cellValue = count; \dtfreq = frequency
        % calculate sums
        \pgfmathsetmacro{\dtcsum}{\dtcsum + \cellValue}%
        \pgfmathsetmacro{\dtfsum}{\dtfsum + \dtfreq}%
        % dividing \cellValue by 100 prevents dimension to large error
        \pgfmathsetmacro{\dtcfsum}{\dtcfsum + (\cellValue / 100 * \dtfreq)}%
        \pgfmathsetmacro{\dtcnt}{\dtcnt + 1}%
    }
    % calculate the averages
    \pgfmathsetmacro{#2}{\dtcsum / \dtcnt}%
    \pgfmathsetmacro{#3}{\dtfsum / \dtcnt}%
    \pgfmathsetmacro{#4}{\dtcfsum / \dtcnt * 100}%
}
\GetAverage{\datatable}{\dtcavg}{\dtfavg}{\dtcfavg}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[
        xtick distance=10
    ]
        \addplot[ybar interval] table [x, y] {\datatable};

        % this line is sort of what I want but I want the 7 to be calculated automatically as the average count 
        \draw[blue] (axis cs:\dtcavg, \pgfkeysvalueof{/pgfplots/ymin}) -- 
                    (axis cs:\dtcavg, \pgfkeysvalueof{/pgfplots/ymax});

    \end{axis}
\end{tikzpicture}

Average of column count is: \dtcavg

Average of column frequency is: \dtfavg

Average of count $\cdot$ frequency is: \dtcfavg
\end{document}

在此处输入图片描述

相关内容