附錄,新的傳說

附錄,新的傳說

我即將完成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也是 a \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}

相關內容