Pgfplots-如何使附加输入覆盖轴内的主图?

Pgfplots-如何使附加输入覆盖轴内的主图?

我得到这个输出:

在此处输入图片描述

您会注意到红色矩形位于轴的“后面”,而我希望它位于轴的“上面”(因此它覆盖了标签)。

代码中有问题的行(下面的 MWE)是: \node[red] at (axis cs: 1.5, -6){0.5};

我怎样才能将它放到最前面?注意:我知道你可以将它带到轴环境之外,但这会阻止我无法使用axis cs

梅威瑟:

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}

\begin{document}

\begin{tikzpicture}

\pgfplotstableread[col sep=comma]{
currency, dep
IDR, 14.4
THB, 5.8
TWD, 2.9
SGD, 2.3 
KRW, 2.1 
PHP, 2.0
MYR, 0.5
CNY, 0.3 
}\charttwentyone

\begin{axis}[
font = \footnotesize,
width = 7cm, height = 7cm,
xbar, bar width=2mm,
axis lines=left,
enlarge y limits=0.1,
%
% x ticks style
xmin = 0.01, xmax = 15.8,
xtick distance = 2,
%
% y axis ticks and style
ytick=data, table/y expr = -\coordindex,     
yticklabels from table={\charttwentyone}{currency}, 
axis y line shift={\pgfkeysvalueof{/pgfplots/xmin}},       
yticklabel shift={-\pgfkeysvalueof{/pgfplots/axis y line shift}}, 
]
%
% done with the axis, now the plots
\addplot [fill, nodes near coords, draw opacity = 0]
table [x=dep]  {\charttwentyone};
\draw[fill,red] (axis cs: 0.9,-6.5) rectangle (axis cs: 2,-5.5);
\node[red] at (axis cs: 1.5, -6){0.5};
\end{axis}
\end{tikzpicture}

\end{document}

答案1

这更像是一个技巧,而不是直接的解决方案。我补充说

coordinate style/.condition=
  {\coordindex==6}{fill=red, text opacity=0}

到环境选项axis。这里\coordindex存储表行的当前索引(从 0 开始),请参阅pgfplots手动的,第 4.3.4 节数学表达式和文件数据

\documentclass[border=5pt]{standalone}
\usepackage{tikz}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.17}

\begin{document}

\begin{tikzpicture}
  \pgfplotstableread[col sep=comma]{
    currency, dep
    IDR, 14.4
    THB, 5.8
    TWD, 2.9
    SGD, 2.3 
    KRW, 2.1 
    PHP, 2.0
    MYR, 0.5
    CNY, 0.3 
  }\charttwentyone
  
  \begin{axis}[
    font = \footnotesize,
    width = 7cm, height = 7cm,
    xbar, bar width=2mm,
    axis lines=left,
    enlarge y limits=0.1,
    %
    % x ticks style
    xmin = 0.01, xmax = 15.8,
    xtick distance = 2,
    %
    % y axis ticks and style
    ytick=data, 
    table/y expr = -\coordindex,     
    yticklabels from table={\charttwentyone}{currency}, 
    axis y line shift={\pgfkeysvalueof{/pgfplots/xmin}},       
    yticklabel shift={-\pgfkeysvalueof{/pgfplots/axis y line shift}},
    coordinate style/.condition=
      {\coordindex==6}{fill=red, text opacity=0},
  ]
  
    % done with the axis, now the plots
    \addplot [fill, nodes near coords] table [x=dep]  {\charttwentyone};    
  \end{axis}
\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容