
하나의 다이어그램에 다양한 그래프를 표시하기 위해 다음과 같은 MWE가 있다고 가정합니다.
최소 작업 예(MWE):
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest, scale only axis}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
axis y line* = left,
legend style = {at={(0.5,-0.125)},
anchor = north,
legend columns = 3,
/tikz/every even column/.append style = {column sep=0.5cm},
/tikz/every odd column/.append style = {column sep=0.15cm},
},
]%
\addplot coordinates {(0,0) (1,1)};
\legend{Graph 1};
\end{axis}%
%
\begin{axis}[
axis x line = none,
axis y line* = right,
legend style = {at={(0.5,-0.125)},
anchor = north,
legend columns = 3,
/tikz/every even column/.append style = {column sep=0.5cm},
/tikz/every odd column/.append style = {column sep=0.15cm},
},
]%
\addplot coordinates {(0,1) (1,0)};
\addplot coordinates {(0,0.5) (1,0.5)};
\legend{Graph 1, Graph 2};
\end{axis}%
%
\begin{axis}[
axis x line = none,
axis y line* = right,
legend style = {at={(0.5,-0.125)},
anchor = north,
legend columns = 3,
/tikz/every even column/.append style = {column sep=0.5cm},
/tikz/every odd column/.append style = {column sep=0.15cm},
},
]%
\pgfplotsset%
{%
every outer y axis line/.style = {xshift=2cm},
every tick/.style = {xshift=2cm},
every y tick label/.style = {xshift=2cm},
}%
\addplot coordinates {(0.5,0) (0.5,1)};
\legend{Graph 4};
\end{axis}%
\end{tikzpicture}
\end{document}
결과 스크린샷:
문제 설명:
보시다시피 전설은 매우 추악해 보입니다. 플롯 아래 중앙에 있는 범례를 어떻게 조정할 수 있나요?
업데이트:
내가 게시한추가 질문범례 항목을 두 행이나 열에 정렬하여 보기 좋게 표시하는 방법에 대해 설명합니다.
답변1
legend to name
당신은 그것을 사용할 수 있습니다 .
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\pgfplotsset{compat=newest, scale only axis}
\begin{document}
\begin{tikzpicture}
\begin{scope}[local bounding box=plots]
\begin{axis}[
axis y line* = left,
legend to name = legA,
legend style = {
legend columns = 3,
/tikz/every even column/.append style = {column sep=0.5cm},
/tikz/every odd column/.append style = {column sep=0.15cm},
},
]%
\addplot coordinates {(0,0) (1,1)};
\legend{Graph 1};
\end{axis}%
%
\begin{axis}[
axis x line = none,
axis y line* = right,
legend to name = legB,
legend style = {
legend columns = 3,
/tikz/every even column/.append style = {column sep=0.5cm},
/tikz/every odd column/.append style = {column sep=0.15cm},
},
]%
\addplot coordinates {(0,1) (1,0)};
\addplot coordinates {(0,0.5) (1,0.5)};
\legend{Graph 1, Graph 2};
\end{axis}%
%
\begin{axis}[
axis x line = none,
axis y line* = right,
legend to name = legC,
legend style = {
legend columns = 3,
/tikz/every even column/.append style = {column sep=0.5cm},
/tikz/every odd column/.append style = {column sep=0.15cm},
},
]%
\pgfplotsset%
{%
every outer y axis line/.style = {xshift=2cm},
every tick/.style = {xshift=2cm},
every y tick label/.style = {xshift=2cm},
}%
\addplot coordinates {(0.5,0) (0.5,1)};
\legend{Graph 4};
\end{axis}%
\end{scope}
\node[below=0.5em of plots.south] (legB) {\ref{legB}};
\node[left=1em of legB] (legA) {\ref{legA}};
\node[right=1em of legB] (legC) {\ref{legC}};
\end{tikzpicture}
\end{document}
하나의 상자에 모든 범례가 포함되어 있습니다.
\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{positioning}
\pgfplotsset{compat=newest, scale only axis}
\begin{document}
\begin{tikzpicture}
\begin{scope}[local bounding box=plots]
\begin{axis}[
axis y line* = left,
legend to name = legA,
legend style = {draw=none,
legend columns = 3,
/tikz/every even column/.append style = {column sep=0.5cm},
/tikz/every odd column/.append style = {column sep=0.15cm},
},
]%
\addplot coordinates {(0,0) (1,1)};
\legend{Graph 1};
\end{axis}%
%
\begin{axis}[
axis x line = none,
axis y line* = right,
legend to name = legB,
legend style = {draw=none,
legend columns = 3,
/tikz/every even column/.append style = {column sep=0.5cm},
/tikz/every odd column/.append style = {column sep=0.15cm},
},
]%
\addplot coordinates {(0,1) (1,0)};
\addplot coordinates {(0,0.5) (1,0.5)};
\legend{Graph 1, Graph 2};
\end{axis}%
%
\begin{axis}[
axis x line = none,
axis y line* = right,
legend to name = legC,
legend style = {draw=none,
legend columns = 3,
/tikz/every even column/.append style = {column sep=0.5cm},
/tikz/every odd column/.append style = {column sep=0.15cm},
},
]%
\pgfplotsset%
{%
every outer y axis line/.style = {xshift=2cm},
every tick/.style = {xshift=2cm},
every y tick label/.style = {xshift=2cm},
}%
\addplot coordinates {(0.5,0) (0.5,1)};
\legend{Graph 4};
\end{axis}%
\end{scope}
\node[below=0.5em of plots.south] (legB) {\ref{legB}};
\node[left=0.2em of legB] (legA) {\ref{legA}};
\node[right=0.2em of legB] (legC) {\ref{legC}};
\draw (legA.south west) rectangle (legC.north east);
\end{tikzpicture}
\end{document}