2

this is a follow up question to: Gap between line and point in pgfplots, like pointintervalbox in gnuplot. I would like to add a legend in the plot and maybe even on the y-axis. Unfortunatelly this does not work with the "discontinuous line" code:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\makeatletter
\pgfplotsset{
discontinuous line/.code={
    \pgfkeysalso{mesh, shorten <=#1, shorten >=#1}
    \def\pgfplotsplothandlermesh@VISUALIZE@std@fill@andor@stroke{%
        \pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{fill path}%
        \pgfplotsplothandlermesh@definecolor
        \pgfusepath{stroke}
        \pgfplotsplothandlermesh@show@normals@if@configured
    }%
},
discontinuous line/.default=1.5mm
}
\makeatother
\begin{document}
\begin{tikzpicture}

\begin{axis}[
axis background/.style={
    shade,bottom color=gray!50,top color=white
},
legend pos=north west,
xlabel=$x$,
ylabel=\ref{dataone}\ one  \ref{datatwo}\ two
]
\addplot [discontinuous line, black, mark=*] table {
0 5
1 3
2 4
3 8
4 0
};
\label{dataone}
\addlegendentry{one}
\addplot [discontinuous line=3mm, red, mark=square] table {
0 1
2 5.5
3 7.25
4 8
};
\label{datatwo}
 \addlegendentry{two}
\end{axis}
\end{tikzpicture}
\end{document}

Which gives this result:

enter image description here

Does anyone have an idea how to get the correct legend? Thanks and greetings!

Rainer
  • 125
  • See http://tex.stackexchange.com/questions/54794/using-a-pgfplots-style-legend-in-a-plain-old-tikzpicture/148855?s=2|2.4045#148855 – John Kormylo Nov 28 '15 at 04:16
  • Thanks John Kormylo, unfortunatelly I did not have the expertise and time to transfer the answer in your link to the problem at hand and now there is Jakes ready to use solution. But realy, thank you! – Rainer Jan 09 '16 at 17:42

1 Answers1

3

To fix the legend, you'll need to redefine the legend image code/.code:

\documentclass[border=5mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\makeatletter
\pgfplotsset{
    discontinuous line/.code={
        \pgfkeysalso{mesh, shorten <=#1, shorten >=#1,
        legend image code/.code={
        \draw [##1, shorten <=0cm] (-#1,0cm) -- (0.3cm,0cm);
        \draw [only marks] plot coordinates {(0.3cm,0cm)};
        \draw [##1, shorten >=0cm] (0.3cm,0cm) -- (0.6cm+#1,0cm);
        }}
        \def\pgfplotsplothandlermesh@VISUALIZE@std@fill@andor@stroke{%
            \pgfplotspatchclass{\pgfplotsplothandlermesh@patchclass}{fill path}%
            \pgfplotsplothandlermesh@definecolor
            \pgfusepath{stroke}
            \pgfplotsplothandlermesh@show@normals@if@configured
        }%
    },
    discontinuous line/.default=1.5mm
}
\makeatother
\begin{document}
\begin{tikzpicture}

\begin{axis}[
axis background/.style={
    shade,bottom color=gray!50,top color=white
},
legend pos=north west,
xlabel=$x$,
ylabel=\ref{dataone}\ one  \ref{datatwo}\ two
]
\addplot [discontinuous line, black, mark=*] table {
0 5
1 3
2 4
3 8
4 0
};
\label{dataone}
\addlegendentry{one}
\addplot [discontinuous line=3mm, red, mark=square] table {
0 1
2 5.5
3 7.25
4 8
};
\label{datatwo}
 \addlegendentry{two}
\end{axis}
\end{tikzpicture}
\end{document}
Jake
  • 232,450