![如何使用符號 x 座標向 ybar pgfplots 新增註釋](https://rvso.com/image/286364/%E5%A6%82%E4%BD%95%E4%BD%BF%E7%94%A8%E7%AC%A6%E8%99%9F%20x%20%E5%BA%A7%E6%A8%99%E5%90%91%20ybar%20pgfplots%20%E6%96%B0%E5%A2%9E%E8%A8%BB%E9%87%8B.png)
我想在圖中的特定 ybar 中添加註釋,或者更好的是添加箭頭,但我正在努力處理符號 x 資料。我已經使用 \pgfplotsinvokeforeach 來讀取變數數據,因此沒有明確定義 x 值。每當我嘗試使用例如 \node[coordinate,pin=right:{塔斯馬尼亞進入 NEM}] 指定座標時 (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}