Take the 2-minute tour ×
TeX - LaTeX Stack Exchange is a question and answer site for users of TeX, LaTeX, ConTeXt, and related typesetting systems. It's 100% free, no registration required.

I have a plot where I want to add a legend which is the combination of lines and marks. For that I want to replace (or define a new) the original pgf plots legend type. As said before, in the following MWE I want to replace the legend in the plot with one similar to the one that is bellow the graphic that uses the same symbols and lines as in the graphic. Is that possible?

\documentclass[10pt,border=0pt]{standalone}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotstableread{%
x0  y1  y2  y3  y4  y5  y6  
1   3.1 3.1 1.8 2.4 1.6 0.7 
3   3.2 2.8 1.8 2.2 1.7 0.5 
5   3.3 3.0 1.6 2.6 1.4 0.3 
7   3.1 3.0 1.2 2.7 1.5 0.2 
9   3.0 2.9 1.5 2.5 1.3 0.2
}{\testdata}

\usetikzlibrary{arrows}
\usetikzlibrary{plotmarks}

% mytick x interval=xmin:xstep:xmax
\pgfplotsset{mytick x interval/.style args={#1:#2:#3}{%
        xmin=#1,xmax=#3,xincrement={#1}{#2},
        xtick={#1,\xval,...,#3}
    },
    xincrement/.code 2 args={\pgfmathparse{#1+#2}\xdef\xval{\pgfmathresult}}
}

% mytick y interval=ymin:ystep:ymax
\pgfplotsset{mytick y interval/.style args={#1:#2:#3}{%
        ymin=#1,ymax=#3,yincrement={#1}{#2},
        ytick={#1,\yval,...,#3}
    },
    yincrement/.code 2 args={\pgfmathparse{#1+#2}\xdef\yval{\pgfmathresult}}
}

\pgfplotsset{every tick/.style={black,}}

\pgfplotsset{%
    compat=1.8,
    compat/show suggested version=false,
    mytick x interval=1:2:9,
    mytick y interval=0:0.5:4,
    tick label style={font=\footnotesize},
    label style={font=\footnotesize},
    xlabel={x-label},
    ylabel={y-label},
    %enlargelimits=false,
    legend style={%
        font=\footnotesize,
        legend cell align=left,
        legend pos=south east,
        nodes={inner xsep=2pt,inner ysep=0.4pt,text depth=0.15em},
    },
}

\begin{document}

\begin{tikzpicture}
\begin{axis}[%
    legend entries={%
        A,
        B,
        C,
        S1,
        S2,
    },
    mark options={fill=white,solid},
]
    \addlegendimage{thick,only marks,mark=square*} \label{c1}
    \addlegendimage{thick,only marks,mark=triangle*} \label{c2}
    \addlegendimage{thick,only marks,mark=*} \label{c3}
    \addlegendimage{thick} \label{l1}
    \addlegendimage{thick,dashed} \label{l2}
    %
    \addplot[thick,mark=square*] table[x=x0,y=y2] {\testdata}; \label{n1}
    \addplot[thick,dashed,mark=square*] table[x=x0,y=y1] {\testdata}; \label{n2}
    \addplot[thick,mark=triangle*] table[x=x0,y=y4] {\testdata}; \label{n3}
    \addplot[thick,dashed,mark=triangle*] table[x=x0,y=y3] {\testdata}; \label{n4}
    \addplot[thick,mark=*] table[x=x0,y=y6] {\testdata}; \label{n5}
    \addplot[thick,dashed,mark=*] table[x=x0,y=y5] {\testdata}; \label{n6}
\end{axis}
\end{tikzpicture} 

\begin{tabular}{llll}
  &          & S1       & S2 \\
  &          & \ref{l1} & \ref{l2} \\
A & \ref{c1} & \ref{n1} & \ref{n2} \\
B & \ref{c2} & \ref{n3} & \ref{n4} \\
C & \ref{c3} & \ref{n5} & \ref{n6} \\
\end{tabular}

\end{document}

enter image description here

share|improve this question
1  
perhaps related: tex.stackexchange.com/questions/54794/… –  Christian Feuersänger Aug 10 '13 at 7:45

1 Answer 1

The best method I found was to use a matrix \usetikzlibrary{matrix} with a border drawn around it (...draw,) and a reference to the axes ...anchor=south east] at (rel axis cs: 0.97,0.03)

Note: to tighten the bounding box, you need to reduce inner sep of the matrix to zero, but force it to a value greater than zero for the nodes, see code within square brackets following \matrix(dict):

\documentclass[10pt,border=0pt]{standalone}

\usepackage[T1]{fontenc}
\usepackage{lmodern}

\usepackage{tikz}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotstableread{%
x0  y1  y2  y3  y4  y5  y6  
1   3.1 3.1 1.8 2.4 1.6 0.7 
3   3.2 2.8 1.8 2.2 1.7 0.5 
5   3.3 3.0 1.6 2.6 1.4 0.3 
7   3.1 3.0 1.2 2.7 1.5 0.2 
9   3.0 2.9 1.5 2.5 1.3 0.2
}{\testdata}

\usetikzlibrary{arrows}
\usetikzlibrary{plotmarks}
\usetikzlibrary{matrix} %added

% mytick x interval=xmin:xstep:xmax
\pgfplotsset{mytick x interval/.style args={#1:#2:#3}{%
        xmin=#1,xmax=#3,xincrement={#1}{#2},
        xtick={#1,\xval,...,#3}
    },
    xincrement/.code 2 args={\pgfmathparse{#1+#2}\xdef\xval{\pgfmathresult}}
}

% mytick y interval=ymin:ystep:ymax
\pgfplotsset{mytick y interval/.style args={#1:#2:#3}{%
        ymin=#1,ymax=#3,yincrement={#1}{#2},
        ytick={#1,\yval,...,#3}
    },
    yincrement/.code 2 args={\pgfmathparse{#1+#2}\xdef\yval{\pgfmathresult}}
}

\pgfplotsset{every tick/.style={black,}}

\pgfplotsset{%
    compat=1.8,
    compat/show suggested version=false,
    mytick x interval=1:2:9,
    mytick y interval=0:0.5:4,
    tick label style={font=\footnotesize},
    label style={font=\footnotesize},
    xlabel={x-label},
    ylabel={y-label},
    %enlargelimits=false,
    legend style={%
        font=\footnotesize,
        legend cell align=left,
        legend pos=south east,
        nodes={inner xsep=2pt,inner ysep=0.4pt,text depth=0.15em},
    },
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[%
]
    %
    \addplot[thick,mark=square*] table[x=x0,y=y2] {\testdata}; \label{n1}
    \addplot[thick,dashed,mark=square*] table[x=x0,y=y1] {\testdata}; \label{n2}
    \addplot[thick,mark=triangle*] table[x=x0,y=y4] {\testdata}; \label{n3}
    \addplot[thick,dashed,mark=triangle*] table[x=x0,y=y3] {\testdata}; \label{n4}
    \addplot[thick,mark=*] table[x=x0,y=y6] {\testdata}; \label{n5}
    \addplot[thick,dashed,mark=*] table[x=x0,y=y5] {\testdata}; \label{n6}

    %legend images that aren't actually plotted
    \addlegendimage{thick, solid};\label{l1}
    \addlegendimage{thick, dashed};\label{l2}
    \addlegendimage{only marks, mark=square*};\label{c1}
    \addlegendimage{only marks, mark=triangle*};\label{c2}
    \addlegendimage{only marks, mark=*};\label{c3}
\end{axis}
\matrix(dict)[ inner sep=0pt,outer sep=0pt,draw,%necessary to move "border" in so it tightly bounds all nodes
matrix of nodes, %eliminates need to surround text with \node{<text>}
%nodes={draw},%draw boxes around all nodes (for debugging)
every node/.style={inner ysep=2pt,inner xsep=4pt},%need to set the "inner sep" of NODES to >0 so they are spaced
fill=white,
anchor=south east
] at (rel axis cs: 0.97,0.03){%places the "legend" so its southeast corner is just within the tick marks
    &          & S1       & S2 \\
    &          & \ref{l1} & \ref{l2} \\
A & \ref{c1} & \ref{n1} & \ref{n2} \\
B & \ref{c2} & \ref{n3} & \ref{n4} \\
C & \ref{c3} & \ref{n5} & \ref{n6} \\
};
\end{tikzpicture} 
\end{document}

Output: Legend drawn with matrix

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.