pgfplot: dos ejes, ambos trazan cuadrícula Y, trazado superpuesto

pgfplot: dos ejes, ambos trazan cuadrícula Y, trazado superpuesto

Yaxis1 Ygrid aparece detrás del gráfico apilado. Sin embargo, Yaxis2 Ygrid aparece sobre el gráfico apilado.

ingrese la descripción de la imagen aquí

Requisito: Ambas cuadrículas deben estar detrás de ambas parcelas.

MWE:

\documentclass{article}
\usepackage{adjustbox}
\usepackage{pgfplots}
\usepackage{xcolor}

\begin{document}

\begin{figure}
\centering
\begin{adjustbox}{max width=.75\textwidth}
\begin{tikzpicture}
    \pgfplotsset{
        symbolic x coords={40,60,80,100,120},
        xtick=data,
        xlabel=x-axis,
        legend columns=-1,
        legend style={draw=none},
        legend to name=named,
    }

    \begin{axis}[
    axis y line*=left,
    ylabel=y-axis 1,
    ylabel near ticks,
    ybar stacked, ymin=0,
    bar width=7mm,
    legend style={at={(0.5,-0.2)},anchor=north},
    ymajorgrids=true,
    major grid style={dotted,black},
    ]
    \addplot [fill={rgb:red,4;green,2;yellow,1},draw=none,area legend] coordinates {
        ({40},15)
        ({60},25)
        ({80},35)
        ({100},15)
        ({120},10)
    };\label{A1nm}
    \addplot [fill=yellow,draw=none,area legend] coordinates {
        ({40},10)
        ({60},35)
        ({80},30)
        ({100},25)
        ({120},10)
    };\label{plot_two}
    \end{axis}

    \begin{axis}[
    axis y line*=right,
    ymode=log,
    ylabel=y-axis 2,
    ylabel near ticks,
    yticklabel pos=right,
    ymajorgrids=true,
    major grid style={dashed, gray}
    ]

    \addlegendimage{empty legend}\addlegendentry{\textbf{Sample:}}

    \addlegendimage{/pgfplots/refstyle=A1nm}\addlegendentry{plot 1}

    \addlegendimage{/pgfplots/refstyle=plot_two}\addlegendentry{plot 2}

    \addplot[mark=*,black]
    coordinates{
        ({40},1)
        ({60},2)
        ({80},35)
        ({100},1)
        ({120},10)
    };\addlegendentry{C}
    \end{axis}
    \end{tikzpicture}
\end{adjustbox}
\\
\ref{named}
\end{figure}
\end{document}

Respuesta1

Del comentario de @Bobyandbob,aquí.

\documentclass{article}
\usepackage{adjustbox}
\usepackage{pgfplots}
\usepackage{xcolor}

\begin{document}

\begin{figure}
\centering
\begin{adjustbox}{max width=.75\textwidth}
\begin{tikzpicture}
    \pgfplotsset{
        symbolic x coords={40,60,80,100,120},
        xtick=data,
        xlabel=x-axis,
        legend columns=-1,
        legend style={draw=none},
        legend to name=named,
    }

    \begin{axis}[
    axis y line*=left,
    ylabel=y-axis 1,
    ylabel near ticks,
    ybar stacked, ymin=0,
    bar width=7mm,
    legend style={at={(0.5,-0.2)},anchor=north},
    ymajorgrids=true,
    major grid style={dotted,black},
    ]
    \addplot [fill={rgb:red,4;green,2;yellow,1},draw=none,area legend] coordinates {
        ({40},15)
        ({60},25)
        ({80},35)
        ({100},15)
        ({120},10)
    };\label{A1nm}
    \addplot [fill=yellow,draw=none,area legend] coordinates {
        ({40},10)
        ({60},35)
        ({80},30)
        ({100},25)
        ({120},10)
    };\label{plot_two}
    \end{axis}

    \begin{axis}[
    set layers,axis background,
    axis y line*=right,
    ymode=log,
    ylabel=y-axis 2,
    ylabel near ticks,
    yticklabel pos=right,
    ymajorgrids=true,
    major grid style={dashed, gray}
    ]

    \addlegendimage{empty legend}\addlegendentry{\textbf{Sample:}}

    \addlegendimage{/pgfplots/refstyle=A1nm}\addlegendentry{plot 1}

    \addlegendimage{/pgfplots/refstyle=plot_two}\addlegendentry{plot 2}

    \addplot[mark=*,black]
    coordinates{
        ({40},1)
        ({60},2)
        ({80},35)
        ({100},1)
        ({120},10)
    };\addlegendentry{C}
    \end{axis}
    \end{tikzpicture}
\end{adjustbox}
\\
\ref{named}
\end{figure}
\end{document}

información relacionada