4

When I use the MWE below my bar plots are overlapping partly the black x-axis enter image description here

How is it possible that the bar graph starts exactly above the x-axis and not on top of it? And is there a way to remove the xtick between the two bars? If I use xmajortick=false it also removes the tick label (300, 500, and 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}
  • 2
    Use the axis on top key in the axis options list. – Paul Gessler Mar 31 '14 at 14:51
  • 1
    For your second question (I missed it the first time): x tick style={draw=white} is one option that works while keeping the tick labels. – Paul Gessler Mar 31 '14 at 15:07
  • Thanks @PaulGessler, but axis on top draws also the gridlines from the y-axis on top of the bars. I would like that the grid is behind the bars but the border (x-axis) on top of the bar (or that the bar starts above the x-axis. The x tick style={draw=white} does the job perfectly. Thanks! – Andy Sweden Mar 31 '14 at 15:23
  • x tick style={opacity=0} will also work. For the grid behind but axis line on top, the axis line on top style defined here should do what you want. – Paul Gessler Mar 31 '14 at 15:57
  • 1
    @PaulGessler sounds like you have an answer :) – cmhughes Mar 31 '14 at 16:07

1 Answers1

5

For the tick labels, you can use x tick style={draw=white}, x tick style={opacity=0}, or x tick style=transparent (or probably others too) to hide them.

To have axis lines on top but show the grid behind the bars, you can use Jake's axis line on top style defined in his excellent answer here.

I've also made a few minor improvements to your code, as commented in the code block here.

Code

\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}

Output

enter image description here

Paul Gessler
  • 29,607