我在使用 pgfplots 时遇到了问题。以下代码是由我正在使用的软件自动生成的。
\documentclass {standalone}
\errorcontextlines 10000
\usepackage{pgfplots, pgfplotstable}
\pgfplotsset{compat=1.10}
\pgfplotsset{single xbar legend/.style={legend image code/.code={\draw[##1,/tikz/.cd,bar width=6pt,bar shift=0pt,xbar] plot coordinates {(0.8em,0pt)};}}}
\begin{document}
\definecolor{fillbkgeleWjets}{rgb}{0.200000,0.600000,0.200000}
\definecolor{fillbkgeleZjets}{rgb}{0.600000,0.800000,0.600000}
\definecolor{fillbkgeletopttbar}{rgb}{1.000000,0.800000,0.200000}
\definecolor{fillbkgeletopsingletop}{rgb}{0.800000,0.600000,0.200000}
\definecolor{fillbkgeleDiBoson}{rgb}{0.400000,0.600000,0.800000}
\definecolor{fillsigeleSherpa}{rgb}{0.600000,0.800000,1.000000}
\definecolor{drawsigeleSherpa}{rgb}{1.000000,0.000000,0.000000}
\begin{tikzpicture}
\pgfplotstableread[col sep=comma]{
label,{dataele},{bkgeleWjets},{bkgeleZjets},{bkgeletopttbar},{bkgeletopsingletop},{bkgeleDiBoson},{sigeleSherpa}
{afterPresel},{0.605647},{0.265529},{0.0879903},{0.0320347},{0.0050964},{0.000518178},{0.00318414}
{mT > 40GeV w/ dphi/mjj cuts},{0.540969},{0.39966},{0.0377578},{0.0124966},{0.00496115},{0.000191084},{0.00396388}
}\datatable
\begin{axis}[
height=3*1.5em+2*5pt, % this line is faulty, replace the '3' with '4' have it render without problem
enlarge y limits={abs=5pt},
xmin=0,
tick align=outside,
axis x line*=bottom,
axis y line*=left,
every y tick/.style={draw=none},
xbar stacked,
y dir=reverse,
scaled x ticks=false,
x tick label style={/pgf/number format/fixed},
enlarge x limits=false,
bar width=10pt,
ytick=data,
yticklabels from table={\datatable}{label},
]
\addplot[] table [x=dataele, y expr=\coordindex] {\datatable};
\addplot[fill=fillbkgeleWjets] table [x=bkgeleWjets, y expr=\coordindex] {\datatable};
\addplot[fill=fillbkgeleZjets] table [x=bkgeleZjets, y expr=\coordindex] {\datatable};
\addplot[fill=fillbkgeletopttbar] table [x=bkgeletopttbar, y expr=\coordindex] {\datatable};
\addplot[fill=fillbkgeletopsingletop] table [x=bkgeletopsingletop, y expr=\coordindex] {\datatable};
\addplot[fill=fillbkgeleDiBoson] table [x=bkgeleDiBoson, y expr=\coordindex] {\datatable};
\addplot[fill=fillsigeleSherpa,draw=drawsigeleSherpa] table [x=sigeleSherpa, y expr=\coordindex] {\datatable};
\end{axis}
\end{tikzpicture}%
\end{document}
似乎height
参数的确切值导致了问题 - 在代码中用 替换3
导致4
渲染正常,而当前版本产生以下错误 - 似乎 pgfplots 内部的一些数学运算产生了infty
结果,然后无法将其归类为数字。
! Missing number, treated as zero.
<to be read again>
i
\pgfplots@y@inverseveclength ->i
nfty
\pgfplotsconvertunittocoordinate ...gth\endcsname
\pgf@xa \edef \pgfmathresu...
\pgfplots@scaling@apply@enlargelimits@by@dimension ...
\edef \pgfplots@loc@TMPa {...
\pgfplots@initsizes ...argelimits@by@dimension {y}
\ifpgfplots@threedim \pgfp...
\pgfplots@determinedefaultvalues ...ots@initsizes
\pgfplots@check@and@apply@...
\endpgfplots@environment@opt ...minedefaultvalues
\pgfplots@BEGIN@cell@pictu...
\end #1->\csname end#1\endcsname
\@checkend {#1}\expandafter \endgroup \if@e...
l.44 \end{axis}
有趣的是,用 替换3
会2
导致不同的错误,这对我来说更有意义:
! Package pgfplots Error: Error: Plot height `30.00003pt' is too small. This ca
nnot be implemented while maintaining constant size for labels. Sorry, label si
zes are only approximate. You will need to adjust your height..
由于文档是自动生成的,因此我只能对 的值的内容进行有限的控制height
。目前,该值的生成方式如下:
(Number of barcharts + 1 [for axis labels]) * (lineheight [user input, default 1.5em]) + 2 * margin [user input, default 5pt])
如果有必要,我可以将此计算更改为任何合理的计算,但我希望保持这种计算方式,以便用户可以独立于条形图数量来控制绘图的大小(因为程序每次都会输出多个这样的文档,这些文档的条形图数量不同,对于相同的用户输入,这些条形图应该具有一致的缩放比例)。如果 pgfplots 中有一个选项允许这样做,我很乐意放弃当前程序,转而采用任何可行的方法。
尽管我更希望找到一种能够在所有情况下成功呈现文档的解决方案,但我已经对适用于合理范围的条形图(1-20)中的合理默认值的某些东西感到满意。
答案1
默认情况下,当您height
为轴指定 时,PGFPlots 会假设该高度的某个部分是标签所需的。对于较小的height
值,这可能会导致绘图区域太小。如果您需要精细控制绘图大小,则应设置scale only axis
。这样,该height
值仅指绘图区域本身。