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.

Using pgfplot (for the first time), I try to create a chart with horizontal bars, sort of. See the figure.

enter image description here

I know how to do it, but because the code is quite verbose and I have lots of data I've factored out the repetitive parts in a macro called \labeledRange{beginpos}{endpos}{label}. I have also used a counter (vertposition) to get rid of manually specifying the vertical position (as I want it to increment for each range). However, the counter doesn't behave as I expect when used in a \node command in the code below. On the figure above, the labels LABEL1 and LABEL 2 should be near the blue and red bar respectively.

How can I get this to work?

(And, as a tiny other problem, how can I get rid of the 'pin' line?)

\documentclass{article}
\usepackage{pgfplots}     
\begin{document}

%\labeledRange{start}{end}{label}
\newcounter{vertposition}
\newcommand{\labeledRange}[3]{
  \addplot coordinates {(#1,\arabic{vertposition}) (#2,\arabic{vertposition})};
  \node[coordinate, pin=right:{#3}] 
          at (axis cs:#2,\arabic{vertposition}) {};
  \stepcounter{vertposition}
  }

\begin{figure}[ht]
  \centering
  \begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=100] %,ytick=\empty]

      %using my macro -> label position is wrong
      \labeledRange{10}{20}{LABEL 1}
      \labeledRange{60}{70}{LABEL 2}

      %without macro -> works fine
      \addplot coordinates {(20,3) (50,3)};
      \node[coordinate, pin=right:{LABEL 3}] 
          at (axis cs:50,3) {};

    \end{axis}

  \end{tikzpicture}
\end{figure}

\end{document}
share|improve this question
add comment

1 Answer

up vote 6 down vote accepted

TikZ commands like \node, \draw and \path within an axis environment aren't executed immediately, but collected and then executed after all the plots are done. This is necessary to make things like the axis cs: coordinate system work (because later plots could still alter the axis ranges, the coordinate system isn't fixed until all plots have been specified). Because of this, all your labels use the same last value of the counter.

Instead of using a separate \node command, you can insert node ... (without the \) before the ; of your \addplot command. That way, the node will automatically be placed at the end of the plot.

The thin lines are caused by your use of pin. You could instead use label, which works like pin but without the connecting line. Or, much better in this case, avoid the use of pin and label altogether and just use the node directly.

\documentclass{article}
\usepackage{pgfplots}     
\begin{document}

%\labeledRange{start}{end}{label}
\newcounter{vertposition}
\newcommand{\labeledRange}[3]{
  \addplot coordinates {(#1,\arabic{vertposition}) (#2,\arabic{vertposition})} node [black,anchor=west] {#3};
  \stepcounter{vertposition}
  }

\begin{figure}[ht]
  \centering
  \begin{tikzpicture}
    \begin{axis}[xmin=0,xmax=100] %,ytick=\empty]

      %using my macro
      \labeledRange{10}{20}{LABEL 1}
      \labeledRange{60}{70}{LABEL 2}

      %without macro
      \addplot coordinates {(20,3) (50,3)};
      \node[coordinate, label=right:{LABEL 3}] 
          at (axis cs:50,3) {};

    \end{axis}

  \end{tikzpicture}
\end{figure}

\end{document}
share|improve this answer
    
Brilliant answer (lightning fast, background info, alternatives, and addressing both my problems) ! –  Rabarberski Aug 20 '12 at 14:46
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.