Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

How draw group line works? I tryed to use this example, but when chaged the index it gives me an error: Undefined control sequence \end{axis}

CODE:

\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}
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

The problem occurred because I had hard-coded the name of the data table in the draw group lines style to be \datatable. I've edited the code a bit: Now draw group line takes four arguments: The name of the table, the name of the column with the group attribute, the value of the group attribute for the current group, and the label. The offset is now controlled using a separate key group line offset which is set to a default of 4ex, and the length by which the group lines are extended is set using group line extend, which has a default of 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}
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.