如何将 y 刻度的标签放置在条形图内?

如何将 y 刻度的标签放置在条形图内?

我一直在尝试寻找如何放置 y 刻度普通的特别的在条形图内而不是条形图的左侧。

以下是最近一次尝试的代码:

\begin{tikzpicture}
 \begin{axis}[
  %axis lines={left, },% xtick=\empty
  %axis lines={left, right},
  %legend style={draw=none},
  %
  %/pgfplots/axis y line*=box|left|middle|center|right|none 219
  %giving up on removing top and right lines%%%
  compat=newest,
  title=Gears Rxf,
  xbar, xmin=0,
  bar width= 1.0cm,
  width=12cm, height=5cm, enlarge y limits=0.5,
  xlabel={Points},
  %==========
  y=2.5cm,
  symbolic y coords={Special,Normal},
  ytick={Debugging, Normal}, 
  y tick label style={rotate=0,anchor=east},
  %yticklabel style={inner ysep=0pt, anchor=south east},
  nodes near coords, nodes near coords align={horizontal},
 ]
 \addplot[color=gray,fill] coordinates {(2.31,Normal)};
 \addplot[color=black,fill] coordinates {(2.58,Special)};
 \end{axis}
\end{tikzpicture}

实际的 实际图形 Gears Rx


期望 所需图形 Gears Rx

答案1

添加axis on top以避免条形重印标签。

然后我们想要

y tick label style={anchor=west,color=white,xshift=\pgfkeysvalueof{/pgfplots/major tick length}},

到:

  • 锚定在标签的西边,
  • 颜色为白色,以便文本显示在黑色上,并且
  • 向右移动一个刻度宽度,因为您可能不想改变刻度标记本身的位置。

我们还需要bar shift=0pt将标签垂直置于条形图的中心。

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
 \begin{axis}[
  %axis lines={left, },% xtick=\empty
  %axis lines={left, right},
  %legend style={draw=none},
  %
  %/pgfplots/axis y line*=box|left|middle|center|right|none 219
  %giving up on removing top and right lines%%%
  compat=newest,
  axis on top,
  title=Gears Rxf,
  xbar, xmin=0,
  bar width= 1.0cm,
  width=12cm, height=5cm, enlarge y limits=0.5,
  xlabel={Points},
  %==========
  y=2.5cm,
  symbolic y coords={Special,Normal},
  ytick={Special, Normal}, 
  y tick label style={anchor=west,color=white,xshift= \pgfkeysvalueof{/pgfplots/major tick length}},
  bar shift=0pt,
  %yticklabel style={inner ysep=0pt, anchor=south east},
  nodes near coords, nodes near coords align={horizontal},
 ]
 \addplot[color=gray,fill] coordinates {(2.31,Normal)};
 \addplot[color=black,fill] coordinates {(2.58,Special)};
 \end{axis}
\end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

添加bar shift=0pt参数addplot

\documentclass{standalone}
\usepackage{pgfplots,tikz}
\pgfplotsset{compat=1.10}
\begin{document}
\begin{tikzpicture}
 \begin{axis}[
  %axis lines={left, },% xtick=\empty
  %axis lines={left, right},
  %legend style={draw=none},
  %
  %/pgfplots/axis y line*=box|left|middle|center|right|none 219
  %giving up on removing top and right lines%%%
  compat=newest,
  title=Gears Rxf,
  xbar, xmin=0,
  bar width= 1.0cm,
  width=12cm, height=5cm, enlarge y limits=0.5,
  xlabel={Points},
  %==========
  y=2.5cm,
  symbolic y coords={Special,Normal},
  ytick={Special, Normal}, 
  y tick label style={rotate=45,anchor=east},
  %yticklabel style={inner ysep=0pt, anchor=south east},
  nodes near coords, nodes near coords align={horizontal},
 ]
 \addplot[bar shift=0pt,color=gray,fill] coordinates {(2.31,Normal)};
 \addplot[bar shift=0pt,color=black,fill] coordinates {(2.58,Special)};
 \end{axis}
\end{tikzpicture}
\end{document}

相关内容