pgfplot ybar 删除条形偏移

pgfplot ybar 删除条形偏移

我有这个 pgfplot

  \begin{tikzpicture}[baseline]
\begin{axis}[
  ymajorgrids,      
  legend style={at={(0.5,1.05)}, anchor=south,legend columns=3},
  ybar,
  bar width=0.5cm,
  ylabel=Ratio vs. original,
  xtick={1, 2, 3, 4},
  ymin=0,
  width=\textwidth,
  height=7cm
  ]
  \addplot+ [DarkRed] (1, 0.9166278045494584);
  \addplot+ [BlueViolet] (2, 0.9745841753458725);
  \addplot+ [CadetBlue] (3, 0.7903000155448467);
  \addplot+ [Blue] (4,0.9375097155293016);      
\end{axis}
\end{tikzpicture}    

这给出了这个输出 在此处输入图片描述

我怎样才能消除 x 轴上条形与刻度之间的偏移?

答案1

我猜您正在寻找以下内容:

在此处输入图片描述

在下面的 MWE 中,与您的代码片段相比的变化标记为% <---

\documentclass[a4paper,12pt]{article}
\usepackage{pgfplots}
\usepackage{tikz,xcolor}
\pgfplotsset{compat=newest}

\begin{document}
\begin{tikzpicture}[baseline]
\begin{axis}[
  ymajorgrids=true,
  %legend style={at={(0.5,1.05)}, anchor=south,legend columns=3}, % it is not used
  ybar=2mm,                     % <----
  bar width=8mm,                % <----
  bar shift=0mm,                % <---- remove offset
  ylabel={Ratio vs. original},
  enlarge x limits=0.2,
  width=0.5\textwidth,          % <----
  height=7cm
  ]
\addplot + [red]      coordinates {(1, 0.9166278045494584)};
\addplot + [green]    coordinates {(2, 0.9745841753458725)};
\addplot+ [magenta]   coordinates {(3, 0.7903000155448467)};
\addplot+ [yellow]    coordinates {(4,0.9375097155293016)};
\end{axis}
\end{tikzpicture}
\end{document}

相关内容