3

I would like to add an annotation, or better, an arrow to a particular ybar in a plot, but I am struggling with the symbolic x data. I've used \pgfplotsinvokeforeach to read in variable data, and so haven't explicitly defined the x values. Whenever I try to specify the coordinates with eg \node[coordinate,pin=right:{Tasmania entry to NEM}] at (axis cs:2004/5,190) {}; I get an error (not a floating point value), so I have had to use the numeric value of 0 which places my comment roughly in the right place. The question is - how to specify the correct node so that it will be placed at the top of the desired bar? The working example is below - but I would like to place the comment at the top and left of the 2005/6 y bar.

\documentclass{article}
\usepackage{pgfplotstable}
\usepackage{tikz}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\pgfplotsset{
    select row/.style={
        x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
    }
}
\pgfplotstableread[col sep=comma,header=false]{
1999/00,167.1
2000/1,172.5
2001/2,175
2002/3,179.3
2003/4,184.4
2004/5,188
2005/6,201.7
2006/7,208.3
2007/8,210.2
2008/9,210.5
2009/10,209.8
2010/11,207.5
2011/12,203.4
2012/13,198.2
%2013/14(YTD),97
}\datatable
\begin{tikzpicture}[scale=0.8]
  \begin{axis}[
    %title=Australia's Primary Energy Consumption by sector - 2012,
        ybar, bar shift=0pt,
        enlarge y limits=0.1,
        %xmin=0,
        xtick={0,...,13},
        xticklabels from table={\datatable}{0},
        ymajorgrids = true,
        bar width=3mm, 
        width=12cm, height=9cm, 
        xlabel={year},
    ylabel={TWh},
        x tick label style={font=\footnotesize,rotate=45, anchor=east},
         nodes near coords align={horizontal},
  ]
\pgfplotsinvokeforeach{0,...,13}{
    \addplot table [x expr=\coordindex, select row=#1] {\datatable};
}
\node[coordinate,pin=right:{Tasmania entry to NEM}] at (axis cs:0,190) {};

\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

  • That's perfect. I presume the '5' (axis cs:5,190) refers to the 6th bar, so I could move the pin by changing this? – icenov Mar 16 '14 at 8:31
4

This is one possible solution. Here a pin is added so that the label for bar is clearly pointed.

enter image description here

Code

\documentclass[border=10pt]{standalone}%{article}
\usepackage{pgfplotstable}
\usepackage{tikz}
\usetikzlibrary{arrows,positioning}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\pgfplotsset{
select row/.style={
    x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
}
}
\pgfplotstableread[col sep=comma,header=false]{
1999/00,167.1
2000/1,172.5
2001/2,175
2002/3,179.3
2003/4,184.4
2004/5,188
2005/6,201.7
2006/7,208.3
2007/8,210.2
2008/9,210.5
2009/10,209.8
2010/11,207.5
2011/12,203.4
2012/13,198.2
%2013/14(YTD),97
}\datatable
\begin{tikzpicture}[scale=0.8]
  \begin{axis}[
    %title=Australia's Primary Energy Consumption by sector - 2012,
    ybar, bar shift=0pt,
    enlarge y limits=0.1,
    %xmin=0,
    xtick={0,...,13},
    xticklabels from table={\datatable}{0},
    ymajorgrids = true,
    bar width=3mm, 
    width=12cm, height=9cm, 
    xlabel={year},
    ylabel={TWh},
    x tick label style={font=\footnotesize,rotate=45, anchor=east},
     nodes near coords align={horizontal},
   ]
\pgfplotsinvokeforeach{0,...,13}{
\addplot table [x expr=\coordindex, select row=#1] {\datatable};
}
\node[pin={[pin distance=1cm,pin edge={<-,>=stealth'},shift={(-1.2cm,0.5cm)}]
Tasmania entry to NEM}] at (axis cs:5,190) {};
\end{axis}
\end{tikzpicture}
\end{document}

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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