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 軸上的 tikcs 中刪除條形的偏移?

答案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}

相關內容