이중 축 플롯 문제

이중 축 플롯 문제
\documentclass{article}
\usepackage{pgfplots, tikz}
\usepackage{adjustbox}  % table scale


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

    \begin{axis}[
    axis y line*=left,
    xlabel=x-axis,
    ylabel=y-axis 1,
    ybar stacked, ymin=0,
    bar width=7mm, 
    legend entries={a,b},
    ]
    \addplot [fill=blue] coordinates {
        ({40},15)
        ({60},25)
        ({80},35)
        ({100},15)
        ({120},10)
    };
    \addplot [fill=red] coordinates {
        ({40},10)
        ({60},35)
        ({80},30)
        ({100},25)
        ({120},10)
    };
    \end{axis}

    \begin{axis}[
    axis y line*=right,
    ylabel=y-axis 2, legend entries={time},
    ]


    \addplot[smooth,mark=*,black]
    coordinates{
        ({40},15)
        ({60},25)
        ({80},35)
        ({100},15)
        ({120},10)
    };
    \end{axis}

    \end{tikzpicture}
\end{adjustbox}
\\
Sample: \ref{named}
\end{figure}
\end{document}

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

문제:

  1. y축 2 레이블이 제대로 표시되지 않습니다(왼쪽에 표시됨).

  2. 누적 플롯에 대한 범례가 표시되지 않습니다.

  3. "Sample"이라는 텍스트와 범례가 같은 줄에 가로로 표시되지 않는 것 같습니다.

답변1

이 솔루션은 다음의 조합입니다.@cfr 여기에 답변하세요그리고@soapygopher 답변은 여기에. 축 레이블을 하나만 추가하면 이중 텍스트를 제거할 수 있습니다. 범례 위치는 legend style={at={(0.5,-0.2)},anchor=north}두 축 환경 모두에 대해 정의됩니다.

질문 3으로 인해 업데이트된 답변은 다음과 같습니다. 와 같은 형식 변경을 수행하는 대신 추가 열을 삽입하여 일부 텍스트를 추가 할 수 legend columns=4있습니다 . 추가 텍스트/제목을 추가하려면 및 . 오른쪽에 두 번째 ylabel을 추가하려면 다음 대신 사용하십시오 .legend columns=3legend styletext width\addlegendimage{empty legend}\addlegendentry{\textbf{Sample:}}ylabel=y-axis 2, ylabel near ticks, yticklabel pos=right,axis y line*=right,

\documentclass{article}
\usepackage{pgfplots, tikz}
\usepackage{adjustbox}  % table scale


\begin{document}

\begin{tikzpicture}
    \pgfplotsset{
        symbolic x coords={40,60,80,100,120},
        xtick=data,
        legend columns=4,
        legend style={
                    /tikz/every even column/.append style={text width=1.4cm}
                        },
    }

    \begin{axis}[
    axis y line*=left,
    xlabel=x-axis 1,
    ylabel=y-axis 1,
    ybar stacked, ymin=0,
    bar width=7mm, legend style={at={(0.5,-0.2)},anchor=north}
    ]

    \addplot [fill=blue,draw=none,area legend] coordinates {
        ({40},15)
        ({60},25)
        ({80},35)
        ({100},15)
        ({120},10)
    };\label{plot_one}
\addlegendentry{plot 1}
    \addplot [fill=red,draw=none,area legend] coordinates {
        ({40},10)
        ({60},35)
        ({80},30)
        ({100},25)
        ({120},10)
    };\label{plot_two}
\addlegendentry{plot 2}
    \end{axis}

 \begin{axis}[
    ylabel=y-axis 2, ylabel near ticks, yticklabel pos=right,legend style={at={(0.5,-0.2)},anchor=north},
    ]
\addlegendimage{empty legend}
\addlegendentry{\hspace*{0cm}\textbf{Sample:}}
\addlegendimage{/pgfplots/refstyle=plot_one}\addlegendentry{plot 1}
\addlegendimage{/pgfplots/refstyle=plot_two}\addlegendentry{plot 2}
    \addplot[smooth,mark=*,black]
    coordinates{
        ({40},15)
        ({60},25)
        ({80},35)
        ({100},15)
        ({120},10)
    };
    \addlegendentry{plot 3}
    \end{axis}

    \end{tikzpicture}

\end{document}

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

또는 2x를 사용하면 \begin{axis}[...,ymin=0,ymax=70, ...]다음을 얻을 수 있습니다.

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

관련 정보