ybar 与轴线重叠(并移除 xtick)

ybar 与轴线重叠(并移除 xtick)

当我使用下面的 MWE 时,我的条形图与黑色 x 轴部分重叠 enter image description here

条形图怎么可能恰好从 x 轴上方开始,而不是从其上方开始?有没有办法删除两个条形之间的 xtick?如果我使用xmajortick=false它,也会删除刻度标签(300、500 和 700)。

            \documentclass{article}

            \usepackage{pgfplots}
            \usepackage{float}
            \pgfplotsset{compat=newest}
            \pgfplotsset{plot coordinates/math parser=false}

            \begin{document}
            \begin{tikzpicture}

            \begin{axis}[%
            area legend,
            scale only axis,
            xmin=200,
            xmax=800,
            xtick={300,500,700},
            xlabel={weight},
            %xmajorgrids,
            ymin=0,
            ymax=25,
            ylabel={Loss},
            ymajorgrids,
            legend style={at={(0.03,0.97)},anchor=north west,draw=black,fill=white,legend cell align=left},
            xminorticks=false,
            ]
            \addplot[ybar,bar width=0.0685714285714286\textwidth,bar shift=-0.0428571428571429\textwidth,draw=red,fill=red] plot table[row sep=crcr] {
            300 9.30726578814723    \\
            500 12.7793803418804    \\
            700 21.7049648016225    \\
            };
            \addlegendentry{Measurements};
            \addplot[ybar,bar width=0.0685714285714286\textwidth,bar shift=0.0428571428571429\textwidth,draw=green,fill=green] plot table[row sep=crcr] {
            300 5.15137978331173    \\
            500 10.6435633181912    \\
            700 19.5700000419451    \\
            };
            \addlegendentry{simulation};

            \end{axis}
            \end{tikzpicture}%

            \end{document}

答案1

对于刻度标签,您可以使用x tick style={draw=white}、、x tick style={opacity=0}x tick style=transparent(或可能还有其他方法)来隐藏它们。

要使轴线位于顶部但在条形图后面显示网格,可以使用杰克他的axis line on top风格在他的优秀答案中得到定义这里

我还对您的代码做了一些小改进,如此处的代码块中所注释的那样。

代码

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.9} % set for future compatibility
\pgfplotsset{plot coordinates/math parser=false}

% begin code from https://tex.stackexchange.com/questions/56230/force-axis-on-top-for-plotmarks-in-pgfplots/56259#56259
\makeatletter \newcommand{\pgfplotsdrawaxis}{\pgfplots@draw@axis} \makeatother

\pgfplotsset{axis line on top/.style={
  axis line style=transparent,
  ticklabel style=transparent,
  tick style=transparent,
  axis on top=false,
  after end axis/.append code={
    \pgfplotsset{
      axis line style=opaque,
      ticklabel style=opaque,
      tick style=opaque,
      grid=none
    }
    \pgfplotsdrawaxis
  }
  }
}
% end code from https://tex.stackexchange.com/questions/56230/force-axis-on-top-for-plotmarks-in-pgfplots/56259#56259

\begin{document}
\begin{tikzpicture}

\begin{axis}[%
  axis line on top, % <<< added style from Jake's answer
  x tick style={opacity=0}, % don't show tick marks (or use x tick style={draw=white})
  area legend,
  scale only axis,
  xmin=200,
  xmax=800,
  xtick={300,500,700},
  xlabel={weight},
%  xmajorgrids,
  ymin=0,
  ymax=25,
  ylabel={Loss},
  ymajorgrids,
  legend style={at={(0.03,0.97)},anchor=north west,draw=black,fill=white,legend cell align=left},
  xminorticks=false,
  ybar,                                   % common options
  bar width=0.0685714285714286\textwidth, % brought out from individual plot cmds
]
  \addplot[  
    bar shift=-0.0428571428571429\textwidth,
    draw=red,
    fill=red,
  ] plot table[row sep=crcr] {
            300 9.30726578814723    \\
            500 12.7793803418804    \\
            700 21.7049648016225    \\
          };
  \addlegendentry{Measurements};
  \addplot[
    bar shift=0.0428571428571429\textwidth,
    draw=green,
    fill=green,
  ] plot table[row sep=crcr] {
            300 5.15137978331173    \\
            500 10.6435633181912    \\
            700 19.5700000419451    \\
          };
  \addlegendentry{simulation};

\end{axis}
\end{tikzpicture}%

\end{document}

输出

enter image description here

相关内容