功劳应归功劳:我发现了这个精彩的箱线图,我试图将我的数据集放入其中,但失败了。我的主要问题是,Miktex 没有给出任何错误消息,让我可以在搜索中跟进。有人有想法吗?我的徒劳无功并没有让我更接近目标。原始版本效果很好。
\documentclass{minimal}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18} % added by me after an errormessage: "Package pgfplots Warning: running in backwards compatibility mode (unsuitable tick labels; missing features). Consider writing \pgfplotsset{compat=1.18}"
\begin{filecontents}{testdata.dat}
Funny 9 2019 294 615 1818
hilarious 12 1167 301 178 404
hilariouslyfunnylol 11 1954 318 178 389
notsofunny 12 1954 308 183 382
quitefunny 12 1479 303 183 393
bunnyfunny 17 1568 307 187 403
hileriousreligious 14 1178 256 156 339
I 17 1659 285 180 342
ran 14 982 277 185 353
out 9 1032 279 182 351
of 15 1231 276 167 358
ideas 15 1576 260 156 335
damnit 9 2019 314 181 387
\end{filecontents}
\pgfplotsset{
box plot width/.initial=1em,
box plot/.style={
/pgfplots/.cd,
black,
only marks,
mark=-,
mark size=\pgfkeysvalueof{/pgfplots/box plot width},
/pgfplots/error bars/.cd,
y dir=plus,
y explicit,
},
box plot box/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\draw ##1 -- ++(\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##2 -- ++(-\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##1 -- cycle;
},
/pgfplots/table/.cd,
y index=2,
y error expr={\thisrowno{3}-\thisrowno{2}},
/pgfplots/box plot
},
box plot top whisker/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\pgfkeysgetvalue{/pgfplots/error bars/error mark}%
{\pgfplotserrorbarsmark}%
\pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
{\pgfplotserrorbarsmarkopts}%
\path ##1 -- ##2;
},
/pgfplots/table/.cd,
y index=4,
y error expr={\thisrowno{2}-\thisrowno{4}},
/pgfplots/box plot
},
box plot bottom whisker/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\pgfkeysgetvalue{/pgfplots/error bars/error mark}%
{\pgfplotserrorbarsmark}%
\pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
{\pgfplotserrorbarsmarkopts}%
\path ##1 -- ##2;
},
/pgfplots/table/.cd,
y index=5,
y error expr={\thisrowno{3}-\thisrowno{5}},
/pgfplots/box plot
},
box plot median/.style={
/pgfplots/box plot
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis} [enlarge x limits=0.5,xtick=data, box plot width=0.5em]
\addplot [box plot median] table {testdata.dat};
\addplot [box plot box] table {testdata.dat};
\addplot [box plot top whisker] table {testdata.dat};
\addplot [box plot bottom whisker] table {testdata.dat};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
问题在于这些x
值是符号,而不是数字。以下代码可以正常工作(尽管显然需要花点心思来调整)。
当然有一种方法可以自动从表中获取符号名称,但我还没有找到它(目前还没有太多时间)。
\documentclass{minimal}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18} % added by me after an errormessage: "Package pgfplots Warning: running in backwards compatibility mode (unsuitable tick labels; missing features). Consider writing \pgfplotsset{compat=1.18}"
\begin{filecontents}{testdata.dat}
Funny 9 2019 294 615 1818
hilarious 12 1167 301 178 404
hilariouslyfunnylol 11 1954 318 178 389
notsofunny 12 1954 308 183 382
quitefunny 12 1479 303 183 393
bunnyfunny 17 1568 307 187 403
hileriousreligious 14 1178 256 156 339
I 17 1659 285 180 342
ran 14 982 277 185 353
out 9 1032 279 182 351
of 15 1231 276 167 358
ideas 15 1576 260 156 335
damnit 9 2019 314 181 387
\end{filecontents}
\pgfplotsset{
box plot width/.initial=1em,
box plot/.style={
/pgfplots/.cd,
black,
only marks,
mark=-,
mark size=\pgfkeysvalueof{/pgfplots/box plot width},
/pgfplots/error bars/.cd,
y dir=plus,
y explicit,
},
box plot box/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\draw ##1 -- ++(\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##2 -- ++(-\pgfkeysvalueof{/pgfplots/box plot width},0pt) |- ##1 -- cycle;
},
/pgfplots/table/.cd,
y index=2,
y error expr={\thisrowno{3}-\thisrowno{2}},
/pgfplots/box plot
},
box plot top whisker/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\pgfkeysgetvalue{/pgfplots/error bars/error mark}%
{\pgfplotserrorbarsmark}%
\pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
{\pgfplotserrorbarsmarkopts}%
\path ##1 -- ##2;
},
/pgfplots/table/.cd,
y index=4,
y error expr={\thisrowno{2}-\thisrowno{4}},
/pgfplots/box plot
},
box plot bottom whisker/.style={
/pgfplots/error bars/draw error bar/.code 2 args={%
\pgfkeysgetvalue{/pgfplots/error bars/error mark}%
{\pgfplotserrorbarsmark}%
\pgfkeysgetvalue{/pgfplots/error bars/error mark options}%
{\pgfplotserrorbarsmarkopts}%
\path ##1 -- ##2;
},
/pgfplots/table/.cd,
y index=5,
y error expr={\thisrowno{3}-\thisrowno{5}},
/pgfplots/box plot
},
box plot median/.style={
/pgfplots/box plot
}
}
\begin{document}
\begin{tikzpicture}
\begin{axis} [enlarge x limits=0.5,xtick=data, box plot width=0.5em,
x tick label style={rotate=90,anchor=east}, symbolic x coords={
Funny,
hilarious,
hilariouslyfunnylol,
notsofunny,
quitefunny,
bunnyfunny,
hileriousreligious,
I,
ran,
out,
of,
ideas,
damnit,
}
]
\addplot [box plot median] table {testdata.dat};
\addplot [box plot box] table {testdata.dat};
\addplot [box plot top whisker] table {testdata.dat};
\addplot [box plot bottom whisker] table {testdata.dat};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
我发现问题在于,它仍然在使用样本中的“旧” .dat 文件。我重命名了文件,但收到一条错误消息,提示我只能使用数字。尽管更改 Miktex 中的 .dat 文件会更改文件内容 - 当我使用编辑器检查它们时 - Miktex 仍然在使用上次运行的文件内容。这也导致我收到一条错误消息,提示我应将所有内容更改为数字,尽管我已经在文件中进行了更改。解决方案是始终更改文档中所有位置的文件名。Texmaker 5.0.4 和 Miktex 4.9