3

I'm pretty close to finishing my pgfplots-plot for the illustration of my experimental data.

The only thing that is not working until now is my legend. My code and the problem can be seen in previous questions: first, second, third.

The main problem is displayed in the "third" question. Due to the fact, that I'm unable to include a proper legend with \legend I thought about "building" a legend by hand inside my plot.

This is the code I produced:

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

Pretty close to the regular legend (the one on top) and finally the color and marker are right for "Zyklus 1"

enter image description here

My question is can I use this code for other plots (values of X and Y will vary)? So in order to do so I would like to place the legend and the nodes in respect to the Y-axes instead of typing all the numbers over and over again for each plot. Is that possible?

Björn
  • 327
  • Are those \draw and \node lines actually placed inside an axis environment? I think you're after the rel axis cs coordinate system (see pgfplots manual, but why didn't you add complete example to this question as well, as in third question you link to? – Torbjørn T. Jan 30 '17 at 12:35
  • Sorry. Changed it. – Björn Jan 30 '17 at 13:39
  • So, my goal is that the new legend behaves like \legend. Always the same position on the top left corner, same size of the rectangle, same vertical space between lines+marker+text. – Björn Jan 30 '17 at 13:56
  • See http://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture/148855?s=23|0.0000#148855 – John Kormylo Jan 31 '17 at 15:41

1 Answers1

4

This might be of interest. First, I add name=mylegend to the legend style. I then use a \matrix to make the second legend, placed relative to the first legend. A pfgplots legend is also a \matrix, so the method is the same for both.

The following code is what makes the new legend. Note that it has to be placed outside the axis environment, as the mylegend reference isn't available inside it.

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

The custom \LegendImage macro uses the same definition as pgfplots itself to draw a line legend, whereas LegendEntry is just a node with style similar to your definition in the 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};}

Output, and complete code below.

enter image description here

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

Addendum, new legend

Given comments, I guess you're just after rel axis cs to position the custom 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}
\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}
Torbjørn T.
  • 206,688
  • Great work Tor! As always! – Björn Feb 12 '17 at 17:46
  • Maybe I put it the wrong way. I don't want to create a second legend, I want to replace the \legend{}. So, I can't use below=3pt of mylegend.south west in my code, beacuse "mylegend" is not existing. When I put below=3pt of mylegend.south west in % the legend is placed below the plot althought anchor says north west in the code. – Björn Feb 12 '17 at 17:59
  • 1
    @Björn I'm so slow that's it not 100% clear still, but see the addendum to my answer. – Torbjørn T. Feb 12 '17 at 18:06
  • I tried the addendum! I think you nailed it! – Björn Feb 13 '17 at 09:11