我想添加注释,或者更好的是,向图中特定的 ybar 添加箭头,但我无法处理符号 x 数据。我使用 \pgfplotsinvokeforeach 读取变量数据,因此没有明确定义 x 值。每当我尝试使用例如 \node[coordinate,pin=right:{Tasmania entry to NEM}] at (axis cs:2004/5,190) {}; 指定坐标时,我都会收到错误(不是浮点值),因此我不得不使用数值 0,这样可以将我的注释大致放在正确的位置。问题是 - 如何指定正确的节点,以便将其放置在所需栏的顶部?工作示例如下 - 但我想将注释放在 2005/6 y 栏的顶部和左侧。
\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}
答案1
这是一个可行的解决方案。这里添加了一个图钉,以便可以清楚地指向条形标签。
代码
\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}