pgfplot: zwei Achsen, beide plotten Y-Raster aktiviert, überlappendes Diagramm

pgfplot: zwei Achsen, beide plotten Y-Raster aktiviert, überlappendes Diagramm

Yaxis1 Ygrid wird hinter dem gestapelten Diagramm angezeigt, Yaxis2 Ygrid wird jedoch über dem gestapelten Diagramm angezeigt.

Bildbeschreibung hier eingeben

Voraussetzung: Beide Raster müssen sich auf der Rückseite beider Parzellen befinden.

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}

Antwort1

Aus dem Kommentar von @Bobyandbob,Hier.

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

verwandte Informationen