我想绘制一个包含多个条形图的图表,每个条形图包含不同的系列。这就是我已经拥有的。在这种情况下,Alg 1、Alg3、...、Alg 7 将是我的多个条形图,参数 1、2、3 将是我的多个系列。但在这种情况下,所有系列都有相同的名称,我希望每个条形图都有不同的名称系列。
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\makeatletter
\pgfplotsset{
calculate offset/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
\pgfkeys{/pgf/fpu=false}
},
every node near coord/.style={
/pgfplots/calculate offset,
yshift=-\testmacro
},
}
\pgfplotstableread{
0 1 2 4
1 7 3 10
2 5 2 13
3 11 7 2
4 12 8 16
5 7 1 2
}\dataset
\begin{tikzpicture}
\begin{axis}[ybar,
width=17cm,
height=10cm,
ymin=0,
ymax=20,
ylabel={Iterations},
xtick=data,
xticklabels = {
Alg1,
Alg3,
Alg4,
Alg5,
Alg6,
Alg7
},
xticklabel style={yshift=-13ex},
major x tick style = {opacity=0},
minor x tick num = 1,
minor tick length=0ex,
every node near coord/.append style={
anchor=east,
rotate=90
}
]
\addplot[draw=black,fill=black!20, nodes near coords=parameter1] table[x index=0,y index=1] \dataset;
\addplot[draw=black,fill=black!40, nodes near coords=parameter2] table[x index=0,y index=2] \dataset;
\addplot[draw=black,fill=black!60, nodes near coords=parameter3] table[x index=0,y index=3] \dataset;
\end{axis}
\end{tikzpicture}
\end{document}
我拥有的:
答案1
这里有一个解决方案,我定义了一个\mycount
限制为六个步骤并产生的计数器A,B,....,F
,您也可以使用此方法按字母顺序绘制 \coordindex创建你的计数器。
计数器在内部用于nodes near coords
获取所需的编号,键保存的是同伴name node
的名称。 node near coords
#1-\coordindex
您的代码
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\newcounter{i}
\setcounter{i}{1}
\def\mycount{\Alph{i}\ifnum\value{i}<6
\stepcounter{i}
\else
\setcounter{i}{1}
\fi\kern-1ex}
\begin{document}
\makeatletter
\pgfplotsset{
calculate offset/.code={
\pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
\pgfmathsetmacro\testmacro{(\pgfplotspointmeta *10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength)}
\pgfkeys{/pgf/fpu=false}
},
every node near coord/.style={
/pgfplots/calculate offset,
yshift=-\testmacro,
},
name node/.style={
every node near coord/.append style={
name=#1-\coordindex
}}
}
\pgfplotstableread{
0 1 2 4
1 7 3 10
2 5 2 13
3 11 7 2
4 12 8 16
5 7 1 2
}\dataset
\begin{tikzpicture}
\begin{axis}[ybar,
width=17cm,
height=10cm,
ymin=0,
ymax=20,
ylabel={Iterations},
xtick=data,
xticklabels = {
Alg1,
Alg3,
Alg4,
Alg5,
Alg6,
Alg7
},
xticklabel style={yshift=-13ex},
major x tick style = {opacity=0},
minor x tick num = 1,
minor tick length=0ex,
every node near coord/.append style={
anchor=north,font=\scriptsize
}
]
\addplot[draw=black,fill=black!20,name node=1, nodes near coords=\mycount 1] table[x index=0,y index=1] \dataset;
\addplot[draw=black,fill=black!40,name node=2, nodes near coords=\mycount 2] table[x index=0,y index=2] \dataset;
\addplot[draw=black,fill=black!60,name node=3, nodes near coords=\mycount 3] table[x index=0,y index=3] \dataset;
\end{axis}
\foreach \i in{1,...,3}{
\foreach \j in{0,1,...,5}{
\node[anchor=east,rotate=90] at (\i-\j){parameter\kern0.5ex};}}
\end{tikzpicture}
\end{document}
输出