pgfplot:两个轴,均绘制 Y 网格,重叠图

pgfplot:两个轴,均绘制 Y 网格,重叠图

Yaxis1 Ygrid 出现在堆叠图后面,但是,Yaxis2 Ygrid 出现在堆叠图上方。

在此处输入图片描述

要求:两个网格都应位于两个地块的背面。

平均能量损失

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

答案1

来自@Bobyandbob的评论,这里

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

相关内容