Pgfplotstable:列数学错误!

Pgfplotstable:列数学错误!

我正在使用以下代码和 pgfplotstable 来生成一个表示其他两列差异的新列。

\pgfplotstableset{create on use/iqr/.style={
    create col/expr={\thisrow{uq}-\thisrow{lq}
    }
  }, create on use/range/.style={%
     create col/expr={\thisrow{uw}-\thisrow{lw}
     }
  }
}

生成的列值中有一些值不正确,偏差 0.1。原始数据来自一个表格,其中数据有 2 位小数。我使用 precision=1 格式化结果列。我需要包含什么才能使简单的减法正确?

MWE 如下:

\documentclass[margin=1cm]{standalone}
\usepackage{pgfplotstable}
\usepackage{booktabs}
\usepackage{multirow}
\pgfplotsset{compat=1.11}
\usepgfplotslibrary{statistics}

\pgfplotstableset{create on use/pliqr/.style={
    %%create col/expr={\thisrow{uq}-\thisrow{lq}
    %%}
  }
}
\pgfplotstableset{create on use/iqr/.style={
    create col/expr={\thisrow{uq}-\thisrow{lq}
    }
  }, create on use/range/.style={%
     create col/expr={\thisrow{uw}-\thisrow{lw}
     }
  }
}

\newcommand{\testdesboxplot}{%
\begin{tikzpicture}
\begin{axis}[%
       y=-\baselineskip, scale only axis,
       width=7.5cm,
       enlarge y limits={abs=0.10},
       boxplot/draw direction=x,
       clip=false, y axis line style={opacity=0}, 
       ytick=\empty,
       xmajorgrids, major grid style=gray!20,
       xtick={0,10,15,20,25,30,35,40,45,50,60},
       xmin=0, xmax=60, axis x line*=bottom,
       axis y line*=left,
       xticklabel pos=upper, tick align=outside,
       xticklabel style={font=\small,major tick length=5pt,
       thick, color=black,tickwidth={2pt}},
]%
\desboxplot{2.9}{8.08}{9.45}{11.35}{15.6}{9.75}{0}
\desboxplot{2.4}{8.50}{12.5}{18.00}{51.3}{13.91}{1}
\desboxplot{6.0}{8.85}{11.6}{19.50}{30.0}{13.795}{2}
\desboxplot{7.7}{9.50}{15.0}{20.50}{41.1}{16.671}{3}
\desboxplot{6.5}{14.0}{16.0}{20.73}{41.1}{18.06}{4}
\desboxplot{7.4}{14.8}{18.5}{25.83}{42.0}{20.825}{5}
\desboxplot{11.5}{16.7}{18.1}{19.88}{31.2}{19.446}{6}
\desboxplot{4.4}{12.2}{15.1}{16.83}{20.2}{14.053}{7}
\desboxplot{6.0}{8.85}{11.1}{13.30}{22.0}{11.914}{8}
\desboxplot{3.5}{8.23}{12.7}{15.9}{17.8}{11.875}{9}
\desboxplot{6.3}{9.4}{12.7}{12.7}{17.5}{11.72}{10}
\desboxplot{2.0}{15.1}{16.5}{20.0}{32.1}{17.311}{11}
\desboxplot{7.0}{18.8}{23.6}{29.17}{53.72}{25.151}{12}
\desboxplot{11.0}{21.75}{26.52}{30.88}{42.1}{26.373}{13}
\desboxplot{10.3}{21.73}{26.9}{31.83}{45.3}{27.117}{14}
\desboxplot{12.2}{17.48}{21.9}{26.98}{40.3}{23.17}{15}
\desboxplot{10.2}{17.75}{21.6}{24.8}{30.5}{21.352}{16}
\desboxplot{12.5}{21.45}{24.8}{32.5}{38.7}{26.18}{17}

\edef\numberofticks{17}
\edef\ciline{0}%%{1}
\edef\desmu{19.06}
\edef\lci{18.3}
\edef\uci{19.8}
%\edef\desmu{25.6}
\edef\numberofrows{18}

\begin{pgfonlayer}{background}
  \fill[color=blue!10] (axis cs:12.2,\numberofticks) rectangle 
                        (axis cs:24.6,\ciline);
  \fill[color=red!10] (axis cs:\lci,\numberofticks) rectangle 
                        (axis cs:\uci,\ciline);
\draw [red,dotted,thick] (axis cs:\desmu,0.0) -- 
                         (axis cs:\desmu,\numberofticks)
   node[anchor=north] at (axis cs:\desmu,18.2){\tiny $ \mu$}
   node[blue,anchor=north] at (axis cs:12.2,18.5)
                                   {\tiny $Q_1$}
   node[blue,anchor=north] at (axis cs:24.6,18.5)
                                   {\tiny $Q_3$};
\draw [blue,dotted,thick] (axis cs:17.8,0.0) -- 
                          (axis cs:17.8,\numberofticks)
   node[blue,anchor=north] at (axis cs:17.8,18.5)
                                   {\tiny $Q_2$};
\end{pgfonlayer}

\end{axis}
\end{tikzpicture}
}%end testdesboxplot definition

\newcommand{\desboxplot}[7]{%
 \addplot[ boxplot prepared={%
  draw position={#7},
  lower whisker={#1}, lower quartile={#2}, median={#3},
  upper quartile={#4}, upper whisker={#5}, average={#6},
  box extend=0.5,  % height of box
  whisker extend=0.5, % height of whiskers
  every box/.style={thin,draw=black,fill=gray!50},
  every whisker/.style={black,thick},
  every median/.style={black,thick},
  every average/.style={/tikz/mark=*,red },
  /pgf/number format/precision=2 } ]
  coordinates {}
  node[left,black] at
  (boxplot box cs: \boxplotvalue{lower whisker},0.5)
  {\tiny\pgfmathprintnumber{\boxplotvalue{lower whisker}}}
  node[right,black] at
  (boxplot box cs: \boxplotvalue{upper whisker},0.5)
  {\tiny\pgfmathprintnumber{\boxplotvalue{upper whisker}}};
} %end of \desboxplot definition

\pgfdeclarelayer{background}
\pgfsetlayers{background,main}

\pgfplotstableread[row sep=crcr]{
yr    ride  mile    lw     lq    med    uq     uw      av\\
1997  20    195.0   2.90   8.08   9.45  11.35  15.60   9.750\\
1998 118   1641.4   2.40   8.50  12.50  18.00  51.30  13.910\\
1999  43    593.2   6.00   8.85  11.60  19.50  30.00  13.795\\
2000  45    750.2   7.70   9.50  15.00  20.50  41.10  16.671\\
2001  63   1137.9   6.50  14.00  16.00  20.78  41.10  18.061\\
2002  36    749.7   7.40  14.83  18.50  25.83  42.00  20.825\\
2003  14    272.3  11.50  16.68  18.13  19.88  31.20  19.446\\
2004  10    140.6   4.40  12.21  15.14  16.83  20.20  14.053\\
2005   7     83.4   6.00   8.85  11.10  13.30  22.00  11.914\\
2006  12    142.5   3.50   8.23  12.70  15.90  17.80  11.875\\
2007   5     58.6   6.30   9.40  12.70  12.70  17.50  11.720\\
2008  36    605.9   2.00  15.10  16.50  20.00  32.10  17.311\\
2009  47   1182.1   7.00  18.80  23.60  29.17  53.72  25.151\\
2010  54   1424.0  11.00  21.75  26.52  30.88  42.10  26.373\\
2011  42   1138.9  10.30  21.73  26.90  31.83  45.30  27.117\\
2012  40    926.8  12.20  17.48  21.90  26.98  40.30  23.170\\
2013  27    576.5  10.20  17.75  21.60  24.80  30.50  21.352\\
2014  26    680.5  12.50  21.45  24.80  32.50  38.70  26.180\\
}\iqrtable

\begin{document}

\pgfplotstablegetrowsof{\iqrtable}
\let\numberofrows=\pgfplotsretval

% Print the table
\pgfplotstabletypeset[columns={yr,ride,mile,av,pliqr,lw,lq,med,%
                               uq,iqr,uw,range},
  % Booktabs rules
  every head row/.style={before row=\toprule,
                         after row=\midrule},
  every last row/.style={after row=[5ex]\bottomrule},
  % Set header name
  columns/yr/.style={string type,column name=Year},
  % Use the ``pliqr'' column to call the \testdesboxplot command 
  %in a multirow cell in the first row, keep empty for all 
  %other rows
  columns/pliqr/.style={
    column name={Dave Strang's Boxplot},
    assign cell content/.code={% use \multirow for Z column:
    \ifnum\pgfplotstablerow=0
    \pgfkeyssetvalue{/pgfplots/table/@cell content}
    {\multirow{\numberofrows}{7.5cm}{\testdesboxplot}}%
    \else
    \pgfkeyssetvalue{/pgfplots/table/@cell content}{}%
    \fi
    }
  },
  % Format numbers and titles
  columns/av/.style={column name=Mean,fixed,
                       fixed zerofill,dec sep align},
  columns/mile/.style={column name=Miles,precision=1,
                    fixed,fixed zerofill,dec sep align},
  columns/ride/.style={column name=Rides,fixed, precision=0,
                    fixed zerofill,dec sep align},
  columns/lw/.style={column name=Min,fixed,precision=1,
                      fixed zerofill,dec sep align},
  columns/lq/.style={column name=Q1,fixed,precision=1,
                      fixed zerofill,dec sep align},
  columns/med/.style={column name=Median,fixed,precision=1,
                      fixed zerofill,dec sep align},
  columns/uq/.style={column name=Q3,fixed,precision=1,
                      fixed zerofill,dec sep align},
  columns/iqr/.style={column name=IQR,fixed,precision=1,
                      fixed zerofill,dec sep align},
  columns/uw/.style={column name=Max,fixed,precision=1,
                      fixed zerofill,dec sep align},
  columns/range/.style={column name=Range,fixed,precision=1,
                        dec sep align},
]{\iqrtable}
%Done!

\end{document}

答案1

数据处理和漂亮打印是 中的不同机制pgfplots。最终结果通过\pgfmathprintnumber变体传递,并且只对结果的表示起作用。换句话说,您看不到舍入值,因为数据是舍入的,但您看到的是以舍入方式呈现的结果。数据保持不变。

这样就可以使排版和内部计算相互排斥。正如注释中所述,precision该键可调整漂亮打印的精度,但不调整数据处理。

因此,数学并没有错。但四舍五入会产生误导。

相关内容