绘制组线错误

绘制组线错误

如何绘制组线有用吗?我尝试使用例如,但是当更改索引时,它会给我一个错误: 未定义控制序列 \end{axis}

代码:

\documentclass[conference]{IEEEtran}
\usepackage{lipsum} %<-- For dummy text
\usepackage{pgfplots}


\newcounter{groupcount}
\pgfplotsset{
    draw group line/.style n args={5}{
        after end axis/.append code={
            \setcounter{groupcount}{0}
            \pgfplotstableforeachcolumnelement{#1}\of\datatable\as\cell{%
                \def\temp{#2}
                \ifx\temp\cell
                    \ifnum\thegroupcount=0
                        \stepcounter{groupcount}
                        \pgfplotstablegetelem{\pgfplotstablerow}{[index]0}\of\datatable
                        \coordinate [yshift=#4] (startgroup) at (axis cs:\pgfplotsretval,0);
                    \else
                        \pgfplotstablegetelem{\pgfplotstablerow}{[index]0}\of\datatable
                        \coordinate [yshift=#4] (endgroup) at (axis cs:\pgfplotsretval,0);
                    \fi
                \else
                    \ifnum\thegroupcount=1
                        \setcounter{groupcount}{0}
                        \draw [
                            shorten >=-#5,
                            shorten <=-#5
                        ] (startgroup) -- node [anchor=north] {#3} (endgroup);
                    \fi
                \fi
            }
            \ifnum\thegroupcount=1
                        \setcounter{groupcount}{0}
                        \draw [
                            shorten >=-#5,
                            shorten <=-#5
                        ] (startgroup) -- node [anchor=north] {#3} (endgroup);
            \fi
        }
    }
}
\pgfplotstableread{
%Res. Number    Res A.  Res.B   Group
1               0.5     4.224   1
2               0.6     4.124   1
3               0.7     4.067   1
4               0.8     4.88    1
5               0.9     4.391   1
6               1       4.273   1
7               1.1     5.506   2
8               1.2     5.391   2
9               1.3     5.52    2
10              1.4     5.418   2
11              1.5     5.285   2
12              1.6     5.194   2
13              0.5     6.224   3
14              0.6     6.124   3
15              .7      6.067   3
16              0.8     6.88    3
17              0.9     6.391   3
18              1       6.273   3
19              1.1     8.506   4
20              1.2     8.391   4
21              1.3     8.52    4
22              1.4     8.418   4
23              1.5     8.285   4
24              1.6     8.194   4
}\sindezoito

\title{Some article title}

\author{The Author}

\begin{document}
\maketitle
\begin{abstract}
\lipsum[2]
\end{abstract}
\lipsum[1-6]


\begin{figure*}\centering
\begin{tikzpicture}
\begin{axis}[ybar stacked, compat=newest, %Better label placement
enlarge y limits=false,
ymin=0,
enlarge x limits = .1,
bar width=12pt,
legend style={at={(0.5,-0.15)},anchor=north,legend columns=0},
x tick label style={rotate=45,anchor=east},
ylabel={Power $(mW)$},
xtick=data,
scale only axis, % The height and width argument only apply to the actual axis
height=5cm,
width=\textwidth-\widthof{100}-0.1cm,
nodes near coords=\rotatebox{90}{\pgfmathprintnumber\pgfplotspointmeta},
axis lines*=left,
y axis line style={opacity=0},
yticklabels={\empty},
ytick style={draw=none},
cycle list={
    {fill=black!60,draw=black!60},
    {fill=black!40,draw=black!40},
    {fill=black!20,draw=black!20}
},
axis on top,
major grid style=white,
ymajorgrids,
xticklabels =   {BS,BS-A,HP,HP-A,LP,LP-A,BP,BP-A,HP,HP-A,LP,LP-A,
                 BS,BS-A,HP,HP-A,LP,LP-A,BP,BP-A,HP,HP-A,LP,LP-A},
legend style={
      font=\footnotesize,
      cells={anchor=west},
      legend columns=5,
      at={(0.5,-0.2)},
      anchor=north,
      /tikz/every even column/.append style={column sep=0.2cm}
    },    
        draw group line={[index]3}{1}{30c - 180}{-3.5ex}{7pt},
        draw group line={[index]3}{2}{50c - 180}{-3.5ex}{7pt},
        draw group line={[index]3}{3}{30c - 350}{-3.5ex}{7pt},
        draw group line={[index]3}{4}{50c - 350}{-3.5ex}{7pt},           
    ]
\addplot table[x index=0,y index=1] \sindezoito;
\addplot table[x index=0,y index=2] \sindezoito;
\legend{Switching, Internal Cell}
\end{axis}
\end{tikzpicture}
\caption{Power results examples forum.}
\label{fig:0.18.sin}
\end{figure*}

\lipsum[1]
\end{document}

答案1

出现问题的原因是,我已将样式中的数据表名称硬编码draw group lines\datatable。我对代码进行了一些编辑:现在draw group line需要四个参数:表的名称、具有组属性的列的名称、当前组的组属性值和标签。偏移量现在使用单独的键控制,该键group line offset设置为默认值4ex,并且组线延伸的长度使用 来设置group line extend,其默认值为3pt

\documentclass[border=4mm]{standalone}
\usepackage{pgfplots}


\newcounter{groupcount}
\pgfplotsset{
    group line offset/.initial=4ex,
    group line extend/.initial=3pt,
    draw group line/.style n args={4}{
        after end axis/.append code={
            \setcounter{groupcount}{0}
            \pgfplotstableforeachcolumnelement{#2}\of#1\as\cell{%
                \def\temp{#3}
                \ifx\temp\cell
                    \ifnum\thegroupcount=0
                        \stepcounter{groupcount}
                        \pgfplotstablegetelem{\pgfplotstablerow}{[index]0}\of#1
                        \coordinate [yshift=-\pgfkeysvalueof{/pgfplots/group line offset}] (startgroup) at (axis cs:\pgfplotsretval,0);
                    \else
                        \pgfplotstablegetelem{\pgfplotstablerow}{[index]0}\of#1
                        \coordinate [yshift=-\pgfkeysvalueof{/pgfplots/group line offset}] (endgroup) at (axis cs:\pgfplotsretval,0);
                    \fi
                \else
                    \ifnum\thegroupcount=1
                        \setcounter{groupcount}{0}
                        \draw [
                            shorten >=-\pgfkeysvalueof{/pgfplots/group line extend},
                            shorten <=-\pgfkeysvalueof{/pgfplots/group line extend}
                        ] (startgroup) -- node [anchor=north] {#4} (endgroup);
                    \fi
                \fi
            }
            \ifnum\thegroupcount=1
                        \setcounter{groupcount}{0}
                        \draw [
                            shorten >=-\pgfkeysvalueof{/pgfplots/group line extend},
                            shorten <=-\pgfkeysvalueof{/pgfplots/group line extend}
                        ] (startgroup) -- node [anchor=north] {#4} (endgroup);
            \fi
        }
    }
}
\pgfplotstableread{
%Res. Number    Res A.  Res.B   Group
1               0.5     4.224   1
2               0.6     4.124   1
3               0.7     4.067   1
4               0.8     4.88    1
5               0.9     4.391   1
6               1       4.273   1
7               1.1     5.506   2
8               1.2     5.391   2
9               1.3     5.52    2
10              1.4     5.418   2
11              1.5     5.285   2
12              1.6     5.194   2
13              0.5     6.224   3
14              0.6     6.124   3
15              .7      6.067   3
16              0.8     6.88    3
17              0.9     6.391   3
18              1       6.273   3
19              1.1     8.506   4
20              1.2     8.391   4
21              1.3     8.52    4
22              1.4     8.418   4
23              1.5     8.285   4
24              1.6     8.194   4
}\sindezoito

\begin{document}
\begin{tikzpicture}
\begin{axis}[ybar stacked, compat=newest, %Better label placement
enlarge y limits=false,
ymin=0,
enlarge x limits = .1,
bar width=12pt,
x tick label style={rotate=90,anchor=east},
ylabel={Power $(mW)$},
xtick=data,
scale only axis, % The height and width argument only apply to the actual axis
height=5cm,
width=\textwidth-\widthof{100}-0.1cm,
nodes near coords=\rotatebox{90}{\pgfmathprintnumber\pgfplotspointmeta},
axis lines*=left,
y axis line style={opacity=0},
yticklabels={\empty},
ytick style={draw=none},
cycle list={
    {fill=black!60,draw=black!60},
    {fill=black!40,draw=black!40},
    {fill=black!20,draw=black!20}
},
axis on top,
major grid style=white,
ymajorgrids,
xticklabels =   {BS,BS-A,HP,HP-A,LP,LP-A,BP,BP-A,HP,HP-A,LP,LP-A,
                 BS,BS-A,HP,HP-A,LP,LP-A,BP,BP-A,HP,HP-A,LP,LP-A},
legend style={
      font=\footnotesize,
      cells={anchor=west},
      legend columns=5,
      at={(0.5,-0.4)},
      anchor=north,
      /tikz/every even column/.append style={column sep=0.2cm}
    },
        group line offset=8ex,
        draw group line={\sindezoito}{[index]3}{1}{30c - 180},
        draw group line={\sindezoito}{[index]3}{2}{50c - 180},
        draw group line={\sindezoito}{[index]3}{3}{30c - 350},
        draw group line={\sindezoito}{[index]3}{4}{50c - 350},           
    ]
\addplot table[x index=0,y index=1] \sindezoito;
\addplot table[x index=0,y index=2] \sindezoito;
\legend{Switching, Internal Cell}
\end{axis}
\end{tikzpicture}
\end{document}

相关内容