问题

问题

问题

我想node near coords根据标签所在栏的高度更改其颜色。也就是说,如果标签在栏外,我想使用与背景不同的颜色;如果标签在栏内,我想使用不同的颜色。

代码

例如

\documentclass[]{standalone}

\usepackage{pgfplots}

\makeatletter
\pgfplotsset{%
  every node near coord/.style={
    check for zero/.code={ % If meta=0, make the node a coordinate (which doesn't have text)
      \pgfmathfloatifflags{\pgfplotspointmeta}{0}{
          \pgfkeys{/tikz/coordinate}
      }{}
    },
    check for zero, color=white, text opacity=1, font=\footnotesize, inner 
    ysep=1pt,
  },%
  % Bottom nodes 
  calculate full offset/.code={
      \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
      \pgfmathsetmacro\testmacro{((\pgfplotspointmeta-\pgfkeysvalueof{/pgfplots/ymin})*10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength}
      \pgfkeys{/pgf/fpu=false}
  },%
  nodes near coords bottom/.style={
      every node near coord/.append style={
          /pgfplots/calculate full offset,
          yshift=-\testmacro,
          anchor=south
          %rotate=90, anchor=west%I need the rotate text here and not in the 
          %general style, as the ybar redefines the style
      }
  },%
}
\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
  ybar, 
  nodes near coords={\pgfmathprintnumber[fixed zerofill,precision=0]{\pgfplotspointmeta}},
  nodes near coords bottom,
  enlarge y limits=false,
  ymin=0,
]
\addplot plot coordinates {(1,1) (2,51) (3,5) (4,3) (5,6) (6,4)};
\end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

如果您看到, 位于1条形图高度之间。因此,我需要以某种方式访问​​并测试其所在条形图的高度,以将颜色从 更改whiteblack

问题

我该怎么做?有没有选项pgfplots可以访问这些信息?

答案1

这添加了color code键来测试 y 值是否小于 2;如果是,则标签为黑色;否则,标签为白色。

\documentclass[]{standalone}

\usepackage{pgfplots}

\makeatletter
\pgfplotsset{%
  every node near coord/.style={
    check for zero/.code={ % If meta=0, make the node a coordinate (which doesn't have text)
      \pgfmathfloatifflags{\pgfplotspointmeta}{0}{
          \pgfkeys{/tikz/coordinate}
      }{}
    },
    color code/.code={%Added code to set node color based on the height of the bar
        \pgfkeys{/pgf/fpu=true}
        \pgfmathparse{\pgfplotspointmeta-2}%will be negative if less than 2
        \pgfmathfloatifflags{\pgfmathresult}{-}{\pgfkeys{/tikz/color=black}}{\pgfkeys{/tikz/color=white}}},
    check for zero, color code, text opacity=1, font=\footnotesize, inner 
    ysep=1pt,
  },%
  % Bottom nodes 
  calculate full offset/.code={
      \pgfkeys{/pgf/fpu=true,/pgf/fpu/output format=fixed}
      \pgfmathsetmacro\testmacro{((\pgfplotspointmeta-\pgfkeysvalueof{/pgfplots/ymin})*10^\pgfplots@data@scale@trafo@EXPONENT@y)*\pgfplots@y@veclength}
      \pgfkeys{/pgf/fpu=false}
  },%
  nodes near coords bottom/.style={
      every node near coord/.append style={
          /pgfplots/calculate full offset,
          yshift=-\testmacro,
          anchor=south
          %rotate=90, anchor=west%I need the rotate text here and not in the 
          %general style, as the ybar redefines the style
      }
  },%
}
\makeatother

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
  ybar, 
  nodes near coords={\pgfmathprintnumber[fixed zerofill,precision=0]{\pgfplotspointmeta}},
  nodes near coords bottom,
  enlarge y limits=false,
  ymin=0,
]
\addplot plot coordinates {(1,1) (2,51) (3,5) (4,3) (5,6) (6,4)};
\end{axis}
\end{tikzpicture}
\end{document}

结果

相关内容