An other approach is to use
legend entries={plot A},
legend image code/.code={%
\draw[dashed] (0cm,-0.1cm) -- (0.5cm,-0.1cm);
\draw[solid] (0cm, 0.1cm) -- (0.5cm, 0.1cm);
},
legend style={xshift=-0.5cm}
Code
\documentclass{standalone}
\usepackage{amsmath,tikz,pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
width=\linewidth,
xmin=-5,xmax=5,
ymin=0,ymax=25,
axis y line*=left,
axis x line*=bottom,
]
\addplot[solid] {x^2};
\end{axis}
\begin{axis}[
width=\linewidth,
xmin=-5,xmax=5,
ymin=0,ymax=25,
axis y line*=right,
axis x line*=bottom,
legend entries={plot A},
legend image code/.code={%
\draw[dashed] (0cm,-0.1cm) -- (0.5cm,-0.1cm);
\draw[solid] (0cm, 0.1cm) -- (0.5cm, 0.1cm);
},
legend style={xshift=-0.5cm}
]
\addplot[dashed] {1.1*x^2};
\end{axis}
\end{tikzpicture}
\end{document}
You can label each plot and then set the legend as a tikz matrix outside the axis
environment. The two related legendimages can be placed in the same matrix cell using \raisebox
and \llap
.
\documentclass[tikz,border=5pt]{standalone}
\usepackage[ngerman]{babel}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{pgfplots}
\usetikzlibrary{matrix}
\usepackage{amsmath}
% Settings for tikz-Image
\pgfplotsset{compat=1.10,
01_temp/.style={
%title=Knotentemperaturen,
xmin=0,
xmax=200,
xlabel={Zeit $t$ [h]},
x tick style={
color=black,
thin
},
ymin=15,
ymax=50,
ylabel={Knotentemperatur $T$ [$^\circ$C]},
y tick style={
color=black,
thin
},
height=9cm,
width=15cm,
grid=major,
grid style={
solid,
ultra thin,
gray
},
/pgf/number format/.cd,
use comma,
set thousands separator={},
}
}
\newcommand\refentry[1]{% positions two related legendimages in one cell
\raisebox{1.5pt}{\ref{plot:#1a}}\llap{\raisebox{-1pt}{\ref{plot:#1b}}}%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[01_temp]
\addplot[black, thin, solid] table [x index=0,y index=1, skip first n=8] {01_temp.dat};
\label{plot:K70101a}
\addplot[black, very thick, solid] table [x index=0,y index=1, skip first n=8] {01_temp.dat};
\label{plot:K70101b}
\addplot[red, thin, dotted] table [x index=0,y index=2, skip first n=8] {01_temp.dat};
\label{plot:K70201a}
\addplot[red, very thick, dotted] table [x index=0,y index=2, skip first n=8] {01_temp.dat};
\label{plot:K70201b}
\addplot[green, thin, dashed] table [x index=0,y index=3, skip first n=8] {01_temp.dat};
\label{plot:K70301a}
\addplot[green, very thick, dashed] table [x index=0,y index=3, skip first n=8] {01_temp.dat};
\label{plot:K70301b}
\addplot[blue, thin, dashed] table [x index=0,y index=4, skip first n=8] {01_temp.dat};
\label{plot:K70401a}
\addplot[blue, very thick, dashed] table [x index=0,y index=4, skip first n=8] {01_temp.dat};
\label{plot:K70401b}
\addplot[pink, thin, dashed] table [x index=0,y index=5, skip first n=8] {01_temp.dat};
\label{plot:K70501a}
\addplot[pink, very thick, dashed] table [x index=0,y index=5, skip first n=8] {01_temp.dat};
\label{plot:K70501b}
\end{axis}
% Legend
\matrix[
matrix of nodes,
anchor=north east,
inner sep=0.2em,
nodes={font=\scriptsize},
] at([yshift=-1ex]current axis.north east)
{
\refentry{K70101}& K70101&[2pt]\refentry{K70201}& K70201&[2pt]\refentry{K70301}& K70301\\
\refentry{K70401}& K70401&[2pt]\refentry{K70501}& K70501\\};
\end{tikzpicture}
\end{document}
Run twice to get
If the background of the legend should be colored use fill=<color>
as a matrix
option.
% Legend
\matrix[
matrix of nodes,
anchor=north east,
inner sep=0.2em,
fill=gray!20,% background color of the legend
nodes={font=\scriptsize},
] at([xshift=-1ex,yshift=-1ex]current axis.north east)
{
\refentry{K70101}& K70101&[2pt]\refentry{K70201}& K70201&[2pt]\refentry{K70301}& K70301\\
\refentry{K70401}& K70401&[2pt]\refentry{K70501}& K70501\\};
Best Answer
This can be done by using the commands
\addlegendimage{empty legend}
and\addlegendentry{}
.You have 5 graphs divided among 3 groups and the legend numbering increases in the vertical direction. Try
For multiple legend rows of different lengths, the trick is similar. Simply use the empty legend entries, possibly in combination with
transpose legend
.The result looks like