pgfplot: 두 개의 축, 둘 다 Y 그리드 표시, 겹치는 플롯

pgfplot: 두 개의 축, 둘 다 Y 그리드 표시, 겹치는 플롯

Yaxis1 Ygrid는 누적 플롯 뒤에 나타나고, Yaxis2 Ygrid는 누적 플롯 위에 나타납니다.

여기에 이미지 설명을 입력하세요

요구 사항: 두 그리드 모두 두 플롯 뒤에 있어야 합니다.

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}

답변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}

관련 정보