3

I've developed a pgfplots ybar chart with white horizontal grid lines over the vertical bars to help the reader judge the length of the bars. The "base" of the ybar chart (the x-axis) is set to y=1 so that I can see whether the normalized results are above or below the axis at y=1. My problem is that I'm having trouble drawing the black x-axis line at y=1 without it being overwritten by a white grid line.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}
\pgfplotstableread[col sep=comma,header=false]{
    Cat A, 1.06
    Cat B, 0.92
    Cat C, 1.01
    Cat D, 1.04
    Cat E, 0.88
}\datatable
\begin{tikzpicture}
\begin{axis}[
    ybar, 
    enlarge x limits=0.2,
    height=4cm,
    ymax=0.125,
    ymin=-0.125,
    xticklabels from table={\datatable}{0},
    xtick=data,
    ytick={-0.1,-0.05,...,0.1},
    yticklabels={0.90,0.95,1.00,1.05,1.10},
    ytick pos=left,
    ymajorgrids=true,
    major grid style={thin,color=white},        
    axis on top,
    ylabel={Normalized value},
    x axis line style={draw opacity=0},
    xtick style={draw=none},
    xticklabel style={
        rotate=90, 
        anchor=east,
    }
]
    \addplot [fill=black,draw=none] table [x expr=\coordindex, y expr=\thisrowno{1}-1] {\datatable};
    \draw [black,thin] (rel axis cs:0,0.5) -- (rel axis cs:1,0.5);
\end{axis}
\end{tikzpicture}
\end{document}

clip1

If I remove axis on top, the x-axis is drawn correctly, but then I lose the white grid lines over the vertical bars.

clip2

How can I change my plot such that the x-axis is drawn last or in such a way that it is drawn on top of the grid line?

5

You can achieve this by using the set layers feature. Then you simply need to draw the black line on the top most layer ...

% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
    \pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
        \pgfplotstableread[
            col sep=comma,
            header=false,
        ]{
            Cat A, 1.06
            Cat B, 0.92
            Cat C, 1.01
            Cat D, 1.04
            Cat E, 0.88
        }\datatable
    \begin{axis}[
        set layers,          % <-- added
        ybar,
        enlarge x limits=0.2,
        height=4cm,
        ymax=0.125,
        ymin=-0.125,
        xticklabels from table={\datatable}{0},
        xtick=data,
        ytick={-0.1,-0.05,...,0.1},
        yticklabels={0.90,0.95,1.00,1.05,1.10},
        ytick pos=left,
        ymajorgrids=true,
        major grid style={thin,color=white},
        axis on top,
        ylabel={Normalized value},
        x axis line style={draw opacity=0},
        xtick style={draw=none},
        xticklabel style={
            rotate=90,
            anchor=east,
        },
    ]
        \addplot [fill=black,draw=none] table [
            x expr=\coordindex,
            y expr=\thisrowno{1}-1,
        ] {\datatable};

        \pgfonlayer{axis foreground}
            \draw [black,thin] (rel axis cs:0,0.5) -- (rel axis cs:1,0.5);
        \endpgfonlayer
    \end{axis}
\end{tikzpicture}
\end{document}

image showing the result of above code

2

This is somewhat a workaround

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=newest}

\begin{document}
\pgfplotstableread[col sep=comma,header=false]{
    Cat A, 1.06
    Cat B, 0.92
    Cat C, 1.01
    Cat D, 1.04
    Cat E, 0.88
}\datatable
\begin{tikzpicture}
\begin{axis}[
    ybar, 
    enlarge x limits=0.2,
    height=4cm,
    ymax=0.125,
    ymin=-0.125,
    xticklabels from table={\datatable}{0},
    xtick=data,
    ytick={-0.1,-0.05,...,0.1},
    yticklabels={0.90,0.95,1.00,1.05,1.10},
    ytick pos=left,
    ymajorgrids=true,
    major grid style={thin,color=white},        
    axis on top,
    ylabel={Normalized value},
    x axis line style={draw opacity=0},
    xtick style={draw=none},
    xticklabel style={
        rotate=90, 
        anchor=east,
    }
]
    \addplot [fill=black,draw=none] table [x expr=\coordindex, y expr=\thisrowno{1}-1] {\datatable};
\end{axis}
\draw (rel axis cs:0.2,0.5) -- (rel axis cs:1.1,0.5);
\end{tikzpicture}
\end{document}

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.