![シンボリック x 座標を使用して ybar pgfplots に注釈を追加する方法](https://rvso.com/image/286364/%E3%82%B7%E3%83%B3%E3%83%9C%E3%83%AA%E3%83%83%E3%82%AF%20x%20%E5%BA%A7%E6%A8%99%E3%82%92%E4%BD%BF%E7%94%A8%E3%81%97%E3%81%A6%20ybar%20pgfplots%20%E3%81%AB%E6%B3%A8%E9%87%88%E3%82%92%E8%BF%BD%E5%8A%A0%E3%81%99%E3%82%8B%E6%96%B9%E6%B3%95.png)
プロット内の特定の y バーに注釈、または矢印を追加したいのですが、シンボリック 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
これは 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}