나는 pgfplots
실험 데이터를 설명하기 위한 플롯을 거의 마무리하고 있습니다.
지금까지 작동하지 않는 유일한 것은 나의 전설입니다. 내 코드와 문제는 이전 질문에서 볼 수 있습니다. 첫 번째, 두번째, 제삼.
주요 문제는 "세 번째" 질문에 표시됩니다. 사실, 적절한 전설을 포함시킬 수 없기 때문에 \legend
플롯 안에 전설을 손으로 "만들기"를 생각했습니다.
제가 만든 코드는 다음과 같습니다.
\begin{filecontents*}{data.txt}
a b d c
9 1 a 2
8 2 b 4
7 3 c 6
6 4 d 8
5 5 e 10
4 6 f 12
3 7 g 14
2 8 x 16
1 9 x 18
\end{filecontents*}
\documentclass[paper=a4,ngerman,xcolor=dvipsnames]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usepackage{siunitx}
\usepackage{caption}
\usepackage{graphicx}
\usepackage{lipsum}
\pgfplotscreateplotcyclelist{mycolorlist}{
blue!99!black,dashed,every mark/.append style={fill=blue!10!black},mark=+\\
red!99!black,solid,every mark/.append style={fill=blue!10!black},mark=+\\
green!99!black,densely dashed,every mark/.append style={fill=blue!10!black},mark=+\\
yellow!80!black,every mark/.append style={fill=blue!10!black},mark=+\\
}
\def\showmark#1{\tikz\node[red!95!black]{\pgfuseplotmark{#1}};}
\begin{document}
\begin{figure}
\begin{tikzpicture}
\begin{axis}[
grid=both,
width=12cm,
height=9cm,
axis equal,
xtick pos=left,
ytick pos=left,
title style={draw,fill=none,name=legend},
title={%
\showmark{pentagon*}=$\SI{100}{\kilo\Hz}$,
\showmark{triangle*}=$\SI{10}{\kilo\Hz}$,
\showmark{square*}=$\SI{1}{\kilo\Hz}$,
\showmark{diamond*}=$\SI{100}{\Hz}$,
\showmark{10-pointed star}=$\SI{10}{\Hz}$,
\showmark{oplus}=$\SI{1}{\Hz}$,
\showmark{o}=$\SI{20}{\mHz}$},
xlabel={R}, ylabel={Z},
legend style={at={(0.02,0.98)},anchor=north west,cells={anchor=west}},
legend style={font=\footnotesize},
cycle list name=mycolorlist,
scatter,
point meta=explicit symbolic,
scatter/classes={
x={},% empty argument means: use the default style
g={mark=pentagon*,red!95!black},
f={mark=triangle*,red!95!black},
e={mark=square*,red!95!black},
d={mark=diamond*,red!95!black},
c={mark=10-pointed star,red!95!black},
b={mark=oplus,red!95!black},
a={mark=o,red!95!black},
z={mark=*,red!80!black}
},
]
\draw [fill=white](-4,10) rectangle (2.0,14) ;
\draw[color=green] (-3.5,13.0)--(-2.5,13.0);
\node[mark size=2pt, color=green] at(-3.0,13.0) {\pgfuseplotmark{*}};
\node[font=\footnotesize] at (-0.7,12.95) {Zyklus 0};
\draw[color=green] (-3.5,12.0)--(-2.5,12.0);
\node[mark size=2pt, color=green] at(-3.0,12.0) {\pgfuseplotmark{*}};
\node[font=\footnotesize] at (-0.7,11.95) {Zyklus 1};
\addplot table [x=b,y=c, meta=d]{data.txt};
\addplot table [x=c,y=b, meta=d]{data.txt};
\addplot table [x=a,y=c, meta=d]{data.txt};
\addplot table [x=b,y=a, meta=d]{data.txt};
\legend{1, 2, 3 ,4}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
일반 범례(맨 위의 범례)와 매우 유사하며 마지막으로 색상과 마커가 "Zyklus 1"에 적합합니다.
제 질문은 이 코드를 다른 플롯에 사용할 수 있느냐는 것입니다(X 및 Y 값은 다양함). 그래서 그렇게 하기 위해 각 플롯에 대해 모든 숫자를 반복해서 입력하는 대신 Y축을 기준으로 범례와 노드를 배치하고 싶습니다. 그게 가능합니까?
답변1
이것은 흥미로울 수 있습니다. 먼저 .name=mylegend
legend style
그런 다음 a를 사용하여 \matrix
첫 번째 범례를 기준으로 두 번째 범례를 만듭니다. 범례 pfgplots
도 이므로 \matrix
방법은 둘 다 동일합니다.
다음 코드는 새로운 범례를 만드는 것입니다. 배치해야 한다는 점 참고하세요밖의환경 내부에서는 참조를 사용할 수 없기 axis
때문입니다 .mylegend
\matrix (m) [
fill=white,
draw=black,
below=3pt of mylegend.south west,
anchor=north west,
cells={anchor=west}]
{
\LegendImage{color=green,mark=*,mark size=2pt} & \LegendEntry{Zuklus 0}; \\
\LegendImage{color=green,mark=o,mark size=2pt} & \LegendEntry{Zuklus 1}; \\
};
사용자 정의 매크로는 선 범례를 그리는 데 자체 \LegendImage
와 동일한 정의를 사용하는 반면, 에서 정의한 것과 유사한 스타일을 가진 노드일 뿐입니다 .pgfplots
LegendEntry
legend style
\newcommand\LegendImage[1]{% from pgfplots.code.tex
\draw[%
/pgfplots/mesh=false,%
bar width=3pt,%
bar shift=0pt,%
%
mark repeat=2,%
mark phase=2,#1]
plot coordinates {
(0cm,0cm)
(0.3cm,0cm)
(0.6cm,0cm)%
};
}
\newcommand\LegendEntry[1]{\node[anchor=west,black,font=\footnotesize,inner xsep=2pt]{#1};}
아래의 출력 및 전체 코드.
\begin{filecontents*}{data.txt}
a b d c
9 1 a 2
8 2 b 4
7 3 c 6
6 4 d 8
5 5 e 10
4 6 f 12
3 7 g 14
2 8 x 16
1 9 x 18
\end{filecontents*}
\documentclass[paper=a4,ngerman,xcolor=dvipsnames]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{positioning}
\usepackage{siunitx}
\newcommand\LegendImage[1]{% from pgfplots.code.tex
\draw[%
/pgfplots/mesh=false,%
bar width=3pt,%
bar shift=0pt,%
%
mark repeat=2,%
mark phase=2,#1]
plot coordinates {
(0cm,0cm)
(0.3cm,0cm)
(0.6cm,0cm)%
};
}
\newcommand\LegendEntry[1]{\node[anchor=west,black,font=\footnotesize,inner xsep=2pt]{#1};}
\pgfplotscreateplotcyclelist{mycolorlist}{
blue!99!black,dashed,every mark/.append style={fill=blue!10!black},mark=+\\
red!99!black,solid,every mark/.append style={fill=blue!10!black},mark=+\\
green!99!black,densely dashed,every mark/.append style={fill=blue!10!black},mark=+\\
yellow!80!black,every mark/.append style={fill=blue!10!black},mark=+\\
}
\def\showmark#1{\tikz\node[red!95!black]{\pgfuseplotmark{#1}};}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=both,
width=12cm,
height=9cm,
axis equal,
xtick pos=left,
ytick pos=left,
title style={draw,fill=none,name=legend},
title={%
\showmark{pentagon*}=$\SI{100}{\kilo\Hz}$,
\showmark{triangle*}=$\SI{10}{\kilo\Hz}$,
\showmark{square*}=$\SI{1}{\kilo\Hz}$,
\showmark{diamond*}=$\SI{100}{\Hz}$,
\showmark{10-pointed star}=$\SI{10}{\Hz}$,
\showmark{oplus}=$\SI{1}{\Hz}$,
\showmark{o}=$\SI{20}{\mHz}$},
xlabel={R}, ylabel={Z},
legend style={
at={(0.02,0.98)},
anchor=north west,
cells={anchor=west},
font=\footnotesize,
name=mylegend
},
cycle list name=mycolorlist,
scatter,
point meta=explicit symbolic,
scatter/classes={
x={},% empty argument means: use the default style
g={mark=pentagon*,red!95!black},
f={mark=triangle*,red!95!black},
e={mark=square*,red!95!black},
d={mark=diamond*,red!95!black},
c={mark=10-pointed star,red!95!black},
b={mark=oplus,red!95!black},
a={mark=o,red!95!black},
z={mark=*,red!80!black}
},
]
\addplot table [x=b,y=c, meta=d]{data.txt};
\addplot table [x=c,y=b, meta=d]{data.txt};
\addplot table [x=a,y=c, meta=d]{data.txt};
\addplot table [x=b,y=a, meta=d]{data.txt};
\legend{1, 2, 3 ,4}
\end{axis}
\matrix (m) [
fill=white,
draw=black,
below=3pt of mylegend.south west,
anchor=north west,
cells={anchor=west}]
{
\LegendImage{color=green,mark=*,mark size=2pt} & \LegendEntry{Zuklus 0}; \\
\LegendImage{color=green,mark=o,mark size=2pt} & \LegendEntry{Zuklus 1}; \\
};
\end{tikzpicture}
\end{document}
부록, 새로운 전설
rel axis cs
의견을 보면 맞춤 범례를 배치한 직후인 것 같나요 ?
\begin{filecontents*}{data.txt}
a b d c
9 1 a 2
8 2 b 4
7 3 c 6
6 4 d 8
5 5 e 10
4 6 f 12
3 7 g 14
2 8 x 16
1 9 x 18
\end{filecontents*}
\documentclass[paper=a4,ngerman,xcolor=dvipsnames]{article}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\usetikzlibrary{positioning}
\usepackage{siunitx}
\newcommand\LegendImage[1]{% from pgfplots.code.tex
\draw[%
/pgfplots/mesh=false,%
bar width=3pt,%
bar shift=0pt,%
%
mark repeat=2,%
mark phase=2,#1]
plot coordinates {
(0cm,0cm)
(0.3cm,0cm)
(0.6cm,0cm)%
};
}
\newcommand\LegendEntry[1]{\node[anchor=west,black,font=\footnotesize,inner xsep=2pt]{#1};}
\pgfplotscreateplotcyclelist{mycolorlist}{
blue!99!black,dashed,every mark/.append style={fill=blue!10!black},mark=+\\
red!99!black,solid,every mark/.append style={fill=blue!10!black},mark=+\\
green!99!black,densely dashed,every mark/.append style={fill=blue!10!black},mark=+\\
yellow!80!black,every mark/.append style={fill=blue!10!black},mark=+\\
}
\def\showmark#1{\tikz\node[red!95!black]{\pgfuseplotmark{#1}};}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=both,
width=12cm,
height=9cm,
axis equal,
xtick pos=left,
ytick pos=left,
title style={draw,fill=none,name=legend},
title={%
\showmark{pentagon*}=$\SI{100}{\kilo\Hz}$,
\showmark{triangle*}=$\SI{10}{\kilo\Hz}$,
\showmark{square*}=$\SI{1}{\kilo\Hz}$,
\showmark{diamond*}=$\SI{100}{\Hz}$,
\showmark{10-pointed star}=$\SI{10}{\Hz}$,
\showmark{oplus}=$\SI{1}{\Hz}$,
\showmark{o}=$\SI{20}{\mHz}$},
xlabel={R}, ylabel={Z},
legend style={
at={(0.02,0.98)},
anchor=north west,
cells={anchor=west},
font=\footnotesize,
name=mylegend
},
cycle list name=mycolorlist,
scatter,
point meta=explicit symbolic,
scatter/classes={
x={},% empty argument means: use the default style
g={mark=pentagon*,red!95!black},
f={mark=triangle*,red!95!black},
e={mark=square*,red!95!black},
d={mark=diamond*,red!95!black},
c={mark=10-pointed star,red!95!black},
b={mark=oplus,red!95!black},
a={mark=o,red!95!black},
z={mark=*,red!80!black}
},
]
\addplot table [x=b,y=c, meta=d]{data.txt};
\addplot table [x=c,y=b, meta=d]{data.txt};
\addplot table [x=a,y=c, meta=d]{data.txt};
\addplot table [x=b,y=a, meta=d]{data.txt};
% coordinate for upper left corner of legend
\coordinate (legpos) at (rel axis cs:0.02,0.98);
%% this works, but the top marker from the green line is drawn on top of the legend. I don't know why.
% \matrix (m) at (rel axis cs:0.02,0.98) [
% fill=white,
% draw=black,
% anchor=north west,
% cells={anchor=west},
% ampersand replacement=\&,
% on layer=axis foreground]
% {
% \LegendImage{color=green,mark=*,mark size=2pt} \& \LegendEntry{Zuklus 0}; \\
% \LegendImage{color=green,mark=o,mark size=2pt} \& \LegendEntry{Zuklus 1}; \\
% };
\end{axis}
\matrix (m) at (legpos) [
fill=white,
draw=black,
anchor=north west,
cells={anchor=west}]
{
\LegendImage{color=green,mark=*,mark size=2pt} & \LegendEntry{Zuklus 0}; \\
\LegendImage{color=green,mark=o,mark size=2pt} & \LegendEntry{Zuklus 1}; \\
};
\end{tikzpicture}
\end{document}