\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0,
width = 12cm,
height = 5cm,
bar width=20pt,
ylabel={number of participants},
nodes near coords,
% nodes near coords align=below, % places labels inside bars
symbolic x coords={wells,leemput,wang, proposed,wells,leemput,wang, proposed,wells,leemput,wang, proposed},
xtick = data,
enlarge y limits={value=0.2,upper},
legend pos=north west
]
\addplot[fill=blue] coordinates {(wells, 1) (leemput, 2) (wang, 3) (proposed, 4) (wells, 4)(leemput, 2) (wang, 3) (proposed, 4)(wells, 4)(leemput, 2) (wang, 3) (proposed, 4)};
\addplot[fill=red] coordinates {(wells, 3) (leemput, 2) (wang, 8) (proposed, 4)(wells, 4)(leemput, 2) (wang, 3) (proposed, 4)(wells, 4)(leemput, 2) (wang, 3) (proposed, 4)};
\legend{1\%, 5\%)
\end{axis}
\end{tikzpicture}
\end{figure}
答案1
如果您可以使用表格来存储数据,则可以从该表中加载 xticklabels:
\begin{filecontents*}{mydata.dat}
wells 1 3
leemput 2 2
wang 3 8
proposed 4 4
wells 4 4
leemput 2 2
wang 3 3
proposed 4 4
wells 4 4
leemput 2 2
wang 3 3
proposed 4 4
\end{filecontents*}
\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0,
width = 12cm,
height = 5cm,
bar width=5pt,
ylabel={number of participants},
nodes near coords,
xticklabel style={rotate=90},
xtick = data,
table/header=false,
xticklabels from table={mydata.dat}{[index]0},
enlarge y limits={value=0.2,upper},
legend pos=north west
]
\addplot table[x expr=\coordindex,y index=1]{mydata.dat};
\addplot table[x expr=\coordindex,y index=2]{mydata.dat};
\legend{1\%, 5\%}
\end{axis}
\end{tikzpicture}
\end{document}
或者您可以使用内联表
\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
ybar,
ymin=0,
width = 12cm,
height = 5cm,
bar width=5pt,
ylabel={number of participants},
nodes near coords,
xticklabel style={rotate=90},
xtick = data,
table/header=false,
table/row sep=\\,
xticklabels from table={
wells\\lemput\\wang\\proposed\\
wells\\lemput\\wang\\proposed\\
wells\\lemput\\wang\\proposed\\
}{[index]0},
enlarge y limits={value=0.2,upper},
legend pos=north west
]
\addplot table[x expr=\coordindex,y index=0]{1\\2\\3\\4\\4\\2\\3\\4\\4\\2\\3\\4\\};
\addplot table[x expr=\coordindex,y index=0]{3\\2\\8\\4\\4\\2\\3\\4\\4\\2\\3\\4\\};
\end{axis}
\end{tikzpicture}
\end{document}
结果和上面一样。