我有一个包含在文档中的标记列表.tex
,其中包含begin{filecontents*}
命令。
到目前为止,我绘制了标记频率的直方图。
我想添加平均值。
你能帮助我吗?这是我目前的代码,改编自这个答案
\documentclass[a4paper, landscape]{article}
\usepackage{filecontents}
\begin{filecontents*}{commadata.dat}
8,5
13,5
6,5
8,5
10
8
5,5
2,5
5,5
7
20
12
18,5
5,5
9,5
3,5
0
7
7
5,5
10
3,5
7
14,5
7
10,5
16,5
10,5
6,5
8
14
8,5
2,5
5
9,5
8
10
9,5
9
8,5
4,5
5,5
\end{filecontents*}
%%%%%%%%%%
% Layout %
%%%%%%%%%%
\def\myHMargin{0.85cm}
\def\myVMarginTop{7mm}
\def\myVMarginBottom{7mm}
\usepackage[hmargin={\myHMargin,\myHMargin},
vmargin={\myVMarginTop,\myVMarginBottom},
headsep=5mm,
footskip=\dimexpr(\baselineskip+4mm),
includefoot,
includehead,
twoside]{geometry}
\pagestyle{empty}
\usepackage{xstring}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\newcommand*{\ReadOutElement}[4]{%
\pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
\let#4\pgfplotsretval
}
%----------------------------------------------------------------------------
\begin{document}
%----------------------------------------------------------------------------
% grid style
\pgfplotsset{grid style={dotted,gray}}
\begin{center}
\fbox{Mean value : $\displaystyle\frac{\ ??? \ }{20}$}
\begin{tikzpicture}
\pgfplotstableread[/pgf/number format/read comma as period]{commadata.dat}\datatable
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval}
% \edef\myxmax{0}%
% \foreach \nY in {1,...,\numrows}
% {\ReadOutElement{\datatable}{\the\numexpr\nY-1}{0}{\Current}%
% \StrSubstitute{\Current}{,}{.}[\mytemp]
% \pgfmathtruncatemacro{\myx}{\mytemp}
% \pgfmathtruncatemacro{\myxmax}{max(\myxmax,\myx)}
% \xdef\myxmax{\myxmax}
% }
\edef\myxmax{20}
\foreach \X in {0,...,\myxmax}
{\expandafter\xdef\csname mypile\X\endcsname{0}}
\foreach \nY in {1,...,\numrows}
{\ReadOutElement{\datatable}{\the\numexpr\nY-1}{0}{\Current}%
\StrSubstitute{\Current}{,}{.}[\mytemp]
\pgfmathtruncatemacro{\myx}{\mytemp}%
\edef\currentval{\csname mypile\myx\endcsname}
\pgfmathtruncatemacro{\mycur}{\currentval+1}
\expandafter\xdef\csname mypile\myx\endcsname{\mycur}
}
\begin{axis}[ymajorgrids,
ymin=0,% ymax=6.2,
xmin=-1,
%ytick={0,2,...,6},
xtick={0,1,...,20},
xticklabels={\small $0$-$1$, \small $1$-$2$, \small $2$-$3$, \small $3$-$4$, \small $4$-$5$, \small $5$-$6$, \small $6$-$7$, \small $7$-$8$, \small $8$-$9$, \small $9$-$10$, \small $10$-$11$, \small $11$-$12$, \small
$12$-$13$, \small $13$-$14$, \small $14$-$15$, \small $15$-$16$, \small $16$-$17$, \small $17$-$18$, \small $18$-$19$, \small $19$-$20$, \small $\geq 20$},
minor y tick num = 0,
%area style,
width=28cm,
height=10cm,
axis lines*=left,
%bar width=0.2cm,
y axis line style = {->},
x axis line style = {->},
tick align = outside,
tick pos = left
]
\pgfplotsinvokeforeach{0,...,\myxmax}{%
\edef\currentval{\csname mypile#1\endcsname}
\pgfmathtruncatemacro{\mycur}{\currentval}
\addplot[ybar, fill=black!20, draw=black!40, ycomb, line width=8mm, color=blue!50] coordinates {(#1,\mycur)};
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
答案1
像这样吗?
\documentclass[a4paper, landscape]{article}
\usepackage{filecontents}
\begin{filecontents*}{commadata.dat}
8,5
13,5
6,5
8,5
10
8
5,5
2,5
5,5
7
20
12
18,5
5,5
9,5
3,5
0
7
7
5,5
10
3,5
7
14,5
7
10,5
16,5
10,5
6,5
8
14
8,5
2,5
5
9,5
8
10
9,5
9
8,5
4,5
5,5
\end{filecontents*}
%%%%%%%%%%
% Layout %
%%%%%%%%%%
\def\myHMargin{0.85cm}
\def\myVMarginTop{7mm}
\def\myVMarginBottom{7mm}
\usepackage[hmargin={\myHMargin,\myHMargin},
vmargin={\myVMarginTop,\myVMarginBottom},
headsep=5mm,
footskip=\dimexpr(\baselineskip+4mm),
includefoot,
includehead,
twoside]{geometry}
\pagestyle{empty}
\usepackage{xstring}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\newcommand*{\ReadOutElement}[4]{%
\pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
\let#4\pgfplotsretval
}
%----------------------------------------------------------------------------
\begin{document}
%----------------------------------------------------------------------------
% grid style
\pgfplotsset{grid style={dotted,gray}}
\begin{center}
%\fbox{Mean value : $\displaystyle\frac{\ ??? \ }{20}$}
\begin{tikzpicture}
\pgfplotstableread[/pgf/number format/read comma as period]{commadata.dat}\datatable
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval}
\edef\myxmax{20}
\foreach \X in {0,...,\myxmax}
{\expandafter\xdef\csname mypile\X\endcsname{0}}
\edef\Total{0}
\foreach \nY in {1,...,\numrows}
{\ReadOutElement{\datatable}{\the\numexpr\nY-1}{0}{\Current}%
\StrSubstitute{\Current}{,}{.}[\mytemp]
\pgfmathtruncatemacro{\myx}{\mytemp}%
\pgfmathsetmacro{\Total}{\Total+\myx}
\xdef\Total{\Total}
\edef\currentval{\csname mypile\myx\endcsname}
\pgfmathtruncatemacro{\mycur}{\currentval+1}
\expandafter\xdef\csname mypile\myx\endcsname{\mycur}
}
\begin{axis}[ymajorgrids,
ymin=0,% ymax=6.2,
xmin=-1,
%ytick={0,2,...,6},
xtick={0,1,...,20},
xticklabels={\small $0$-$1$, \small $1$-$2$, \small $2$-$3$, \small $3$-$4$, \small $4$-$5$, \small $5$-$6$, \small $6$-$7$, \small $7$-$8$, \small $8$-$9$, \small $9$-$10$, \small $10$-$11$, \small $11$-$12$, \small
$12$-$13$, \small $13$-$14$, \small $14$-$15$, \small $15$-$16$, \small $16$-$17$, \small $17$-$18$, \small $18$-$19$, \small $19$-$20$, \small $\geq 20$},
minor y tick num = 0,
%area style,
width=28cm,
height=10cm,
axis lines*=left,
%bar width=0.2cm,
y axis line style = {->},
x axis line style = {->},
tick align = outside,
tick pos = left,
title={Mean value : $\pgfmathparse{\Total/\numrows}
\displaystyle\frac{\Total}{\numrows}=\pgfmathprintnumber{\pgfmathresult}$},
]
\pgfplotsinvokeforeach{0,...,\myxmax}{%
\edef\currentval{\csname mypile#1\endcsname}
\pgfmathtruncatemacro{\mycur}{\currentval}
\addplot[ybar, fill=black!20, draw=black!40, ycomb, line width=8mm, color=blue!50] coordinates {(#1,\mycur)};
}
\end{axis}
\end{tikzpicture}
\end{center}
\end{document}
或者,如果你想要平均占用人数,
\documentclass[a4paper, landscape]{article}
\usepackage{filecontents}
\begin{filecontents*}{commadata.dat}
8,5
13,5
6,5
8,5
10
8
5,5
2,5
5,5
7
20
12
18,5
5,5
9,5
3,5
0
7
7
5,5
10
3,5
7
14,5
7
10,5
16,5
10,5
6,5
8
14
8,5
2,5
5
9,5
8
10
9,5
9
8,5
4,5
5,5
\end{filecontents*}
%%%%%%%%%%
% Layout %
%%%%%%%%%%
\def\myHMargin{0.85cm}
\def\myVMarginTop{7mm}
\def\myVMarginBottom{7mm}
\usepackage[hmargin={\myHMargin,\myHMargin},
vmargin={\myVMarginTop,\myVMarginBottom},
headsep=5mm,
footskip=\dimexpr(\baselineskip+4mm),
includefoot,
includehead,
twoside]{geometry}
\pagestyle{empty}
\usepackage{xstring}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{lmodern}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\newcommand*{\ReadOutElement}[4]{%
\pgfplotstablegetelem{#2}{[index]#3}\of{#1}%
\let#4\pgfplotsretval
}
%----------------------------------------------------------------------------
\begin{document}
%----------------------------------------------------------------------------
% grid style
\pgfplotsset{grid style={dotted,gray}}
\begin{center}
%\fbox{Mean value : $\displaystyle\frac{\ ??? \ }{20}$}
\begin{tikzpicture}
\pgfplotstableread[/pgf/number format/read comma as period]{commadata.dat}\datatable
\pgfplotstablegetrowsof{\datatable}
\pgfmathtruncatemacro{\numrows}{\pgfplotsretval}
\pgfplotstablegetcolsof{\datatable}
\pgfmathtruncatemacro{\numcols}{\pgfplotsretval}
\edef\myxmax{20}
\foreach \X in {0,...,\myxmax}
{\expandafter\xdef\csname mypile\X\endcsname{0}}
\edef\Total{0}
\foreach \nY in {1,...,\numrows}
{\ReadOutElement{\datatable}{\the\numexpr\nY-1}{0}{\Current}%
\StrSubstitute{\Current}{,}{.}[\mytemp]
\pgfmathtruncatemacro{\myx}{\mytemp}%
\pgfmathsetmacro{\Total}{\Total+\myx}
\xdef\Total{\Total}
\edef\currentval{\csname mypile\myx\endcsname}
\pgfmathtruncatemacro{\mycur}{\currentval+1}
\expandafter\xdef\csname mypile\myx\endcsname{\mycur}
}
\begin{axis}[ymajorgrids,
ymin=0,% ymax=6.2,
xmin=-1,xmax=21,
%ytick={0,2,...,6},
xtick={0,1,...,20},
xticklabels={\small $0$-$1$, \small $1$-$2$, \small $2$-$3$, \small $3$-$4$, \small $4$-$5$, \small $5$-$6$, \small $6$-$7$, \small $7$-$8$, \small $8$-$9$, \small $9$-$10$, \small $10$-$11$, \small $11$-$12$, \small
$12$-$13$, \small $13$-$14$, \small $14$-$15$, \small $15$-$16$, \small $16$-$17$, \small $17$-$18$, \small $18$-$19$, \small $19$-$20$, \small $\geq 20$},
minor y tick num = 0,
%area style,
width=28cm,
height=10cm,
axis lines*=left,
%bar width=0.2cm,
y axis line style = {->},
x axis line style = {->},
tick align = outside,
tick pos = left,
% title={Mean value : $\pgfmathparse{\Total/\numrows}
% \displaystyle\frac{\Total}{\numrows}=\pgfmathprintnumber{\pgfmathresult}$},
]
\pgfmathsetmacro{\Occ}{0}
\pgfplotsinvokeforeach{0,...,\myxmax}{%
\edef\currentval{\csname mypile#1\endcsname}
\pgfmathtruncatemacro{\mycur}{\currentval}
\pgfmathtruncatemacro{\Occ}{\Occ+\currentval}
\xdef\Occ{\Occ}
\addplot[ybar, fill=black!20, draw=black!40, ycomb, line width=8mm, color=blue!50] coordinates {(#1,\mycur)};
}
\end{axis}
\node[draw,above] at (current axis.north) {Mean value :
$\displaystyle\frac{\Occ}{20}$};
\end{tikzpicture}
\end{center}
\end{document}
答案2
我会用不同的方式来做。
\begin{filecontents*}{\jobname.dat}
8,5
13,5
6,5
8,5
10
8
5,5
2,5
5,5
7
20
12
18,5
5,5
9,5
3,5
0
7
7
5,5
10
3,5
7
14,5
7
10,5
16,5
10,5
6,5
8
14
8,5
2,5
5
9,5
8
10
9,5
9
8,5
4,5
5,5
\end{filecontents*}
\documentclass[varwidth=40cm,border=4]{standalone}
\usepackage{xparse}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\pgfplotsset{grid style={dotted,gray}}
\ExplSyntaxOn
\NewDocumentCommand{\readmarks}{mm}
{% #1 = file name, #2 = upper limit
\colas_readmarks:nn { #1 } { #2 }
}
\NewExpandableDocumentCommand{\getmarks}{m}
{
0\prop_item:Nn \l_colas_readmarks_prop { #1 }
}
\NewExpandableDocumentCommand{\getaverage}{m}
{
\fp_eval:n
{
(0 \int_step_function:nN { #1 } \__colas_readmarks_add:n )/(#1)
}
}
\cs_new:Nn \__colas_readmarks_add:n
{
+0\prop_item:Nn \l_colas_readmarks_prop { #1 }
}
\ior_new:N \g__colas_readmarks_stream
\prop_new:N \l_colas_readmarks_prop
\tl_new:N \l__colas_readmarks_item_tl
\cs_generate_variant:Nn \prop_put:Nnn { Nxx }
\cs_generate_variant:Nn \prop_item:Nn { Ne }
\cs_new_protected:Nn \colas_readmarks:nn
{
\prop_clear:N \l_colas_readmarks_prop
\ior_open:Nn \g__colas_readmarks_stream { #1 }
\ior_map_inline:Nn \g__colas_readmarks_stream
{
\__colas_readmarks_item:nn { #2 } { ##1 }
}
}
\cs_new_protected:Nn \__colas_readmarks_item:nn
{% #1 = upper limit, #2 = item
\tl_set:Nn \l__colas_readmarks_item_tl { #2 }
\tl_replace_once:Nnn \l__colas_readmarks_item_tl { , } { . }
\prop_put:Nxx \l_colas_readmarks_prop
{
\fp_eval:n { \l__colas_readmarks_item_tl < #1 ? floor(\l__colas_readmarks_item_tl) : 20 }
}
{
\int_eval:n
{
1 +
0\prop_item:Ne \l_colas_readmarks_prop
{
\fp_eval:n { \l__colas_readmarks_item_tl < #1 ? floor(\l__colas_readmarks_item_tl) : 20 }
}
}
}
}
\ExplSyntaxOff
\begin{document}
\readmarks{\jobname.dat}{20}
\centering
\fbox{Mean value: $\getaverage{20}$}
\begin{tikzpicture}
\begin{axis}[
ymajorgrids,
ymin=0,
% ymax=6.2,
xmin=-1,
% ytick={0,2,...,6},
xtick={0,1,...,20},
xticklabels={
\small $0$-$1$, \small $1$-$2$, \small $2$-$3$, \small $3$-$4$,
\small $4$-$5$, \small $5$-$6$, \small $6$-$7$, \small $7$-$8$,
\small $8$-$9$, \small $9$-$10$, \small $10$-$11$, \small $11$-$12$,
\small $12$-$13$, \small $13$-$14$, \small $14$-$15$, \small $15$-$16$,
\small $16$-$17$, \small $17$-$18$, \small $18$-$19$, \small $19$-$20$,
\small $\geq 20$
},
minor y tick num = 0,
% area style,
width=28cm,
height=10cm,
axis lines*=left,
% bar width=0.2cm,
y axis line style = {->},
x axis line style = {->},
tick align = outside,
tick pos = left
]
\pgfplotsinvokeforeach{0,...,20}{%
\addplot[
ybar,
fill=black!20,
draw=black!40,
ycomb,
line width=8mm,
color=blue!50
] coordinates {(#1,\getmarks{#1})};
}
\end{axis}
\end{tikzpicture}
\end{document}
读取文件并填充属性列表。然后我们可以用更简单的方式检索值。
答案3
不清楚您是否想要平均频率或只是 x 变量的平均值。对于第二种情况:
\documentclass[a4paper]{article}
\begin{document}
<<data,echo=F>>=
df <- read.csv(file="commadata.dat",head=F, dec=",", sep=";")
@
The mean is \(\frac{\Sexpr{sum(df$V1)}}{\Sexpr{length(df$V1)}}=
\Sexpr{sum(df$V1)/length(df$V1)}\)
\bigskip
<<histogran,echo=F,dev="tikz">>=
x <- round(mean(df$V1),2)
hist(df$V1, breaks=21,col="tan1", xaxp = c(0, 20, 20), ylim=c(0,8),
main="", xlab="",ylab="")
arrows(x,7.8,x,0, length=0.15, col =4, code=1,lwd=2)
text(x,8,paste("$\\overline{\\mbox{x}}=$",x))
@
\end{document}