我只对 pgfplots xbar 示例做了很少的修改,但它完全失败了:
编辑:删除了原始示例,它仅包含一个缺少的逗号。
我真的需要为每个条目定义一个吗symbolic y coords
?我的完整列表大约有 40 个条目。
编辑:我用 pgfplotstable 实现了答案,但结果看起来不对:
\pgfplotstableread[header=false, col sep=comma]{ % Read data table.
% First row doesn't have column names, hence the "header=false"
0.34,LaTeX Kernel
0.36,pre document class
0.48,document class
0.54,template packages
0.64,encoding (documents)
0.72,encoding (files)
0.88,Fonts
0.98,Packages: Base
1.01,Packages: Bugfix
1.03,relsize
1.13,Math
1.43,Math (using LaTeX 3)
2.64,pgf/tikz
2.72,siunitx
2.81,Symbols
2.85,Tables
3.01,Text
3.04,Quotes (csquotes)
3.3475,Bibliography (biblatex)
3.365,Figures
3.48,Captions
3.58,Index
3.745,Glossary
3.85,{Verbatim, Listings}
3.91,Fancy
3.92,Layout
4.015,Head and Foot
4.03,Headings
4.33,PDF
4.46,Additional
4.55,Style.tex
6.57,Document
}\compilationtimes
\begin{tikzpicture}
\begin{axis}[
xbar, xmin=0,
width=12cm, height=10cm, enlarge y limits=0.5,
xlabel={compilation time / seconds},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
yticklabels from table={\compilationtimes}{1} % Get tables from second column of data table
]
\addplot table [
y expr=-\coordindex, % Use negative coordinate index as y coordinate
x index=0 % Use first column as x coordinate
] {\compilationtimes};
\end{axis}
\end{tikzpicture}
答案1
Peter 的回答非常准确,可以消除错误消息。我只想添加一个答案,解释如何解决“手动定义所有符号坐标”问题:
我建议不要使用symbolic y coordinates
,也不要使用\addplot coordinates
,两者都非常麻烦。相反,使用将数据读入数据表\pgfplotstableread
。这样您可以更轻松地将其重新用于不同的绘图,以及用于名为的函数y tick labels from table={<table name>}{<column>}
。该函数从数据表(或文件)读取条目以用作刻度标签。如果您使用y expr=-\coordindex
(或者y expr=\coordindex
如果您希望将第一个数据点绘制在图的底部)和ytick=data
,则刻度标签和数据点将完美匹配,如果您选择稍后更改行的名称,则只需在一个地方(在数据表中)进行更改。
为了确保所有条形图均适合绘图且不重叠,一种方法是通过使用 设置 y 单位向量,让绘图区域随着条形图的数量垂直延伸y=<length>
,其中<length>
应大于bar width
。此外,enlarge y limits
应设置为类似于abs=0.5
在图的顶部和底部添加半个单位长度的值。设置enlarge y limits=0.5
(不带abs
)会使轴范围增加 50%,这很少是您想要的。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots, pgfplotstable}
\pgfplotstableread[header=false, col sep=comma]{ % Read data table.
% First row doesn't have column names, hence the "header=false"
0.34,LaTeX Kernel
0.36,pre document class
0.48,document class
0.54,template packages
0.64,encoding (documents)
0.72,encoding (files)
0.88,Fonts
0.98,Packages: Base
1.01,Packages: Bugfix
1.03,relsize
1.13,Math
1.43,Math (using LaTeX 3)
2.64,pgf/tikz
2.72,siunitx
2.81,Symbols
2.85,Tables
3.01,Text
3.04,Quotes (csquotes)
3.3475,Bibliography (biblatex)
3.365,Figures
3.48,Captions
3.58,Index
3.745,Glossary
3.85,{Verbatim, Listings}
3.91,Fancy
3.92,Layout
4.015,Head and Foot
4.03,Headings
4.33,PDF
4.46,Additional
4.55,Style.tex
6.57,Document
}\compilationtimes
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xbar, xmin=0,
width=12cm,
bar width=2ex, y=3ex, % Set the y unit vector, that way, the plot will stretch to accommodate all bars
enlarge y limits={abs=0.75},
xlabel={compilation time / seconds},
ytick=data,
nodes near coords, nodes near coords align={horizontal},
yticklabels from table={\compilationtimes}{1} % Get tables from second column of data table
]
\addplot table [
y expr=-\coordindex, % Use negative coordinate index as y coordinate
x index=0 % Use first column as x coordinate
] {\compilationtimes};
\end{axis}
\end{tikzpicture}
\end{document}
答案2
您缺少了,
之前的尾随字符ytick=data
。更正后,我们得到:
代码:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xbar, xmin=0,
width=12cm, height=3.5cm, enlarge y limits=0.5,
xlabel={compilation time / seconds},
symbolic y coords={LaTeX Kernel,pre document class},% <--- Missing comma here
ytick=data,
nodes near coords, nodes near coords align={horizontal},
]
\addplot coordinates {
(0.34,LaTeX Kernel)
(0.36,pre document class)
};
\end{axis}
\end{tikzpicture}
\end{document}