1

I have the following PGFPlots (mini sample).

\documentclass[11pt, a4paper, twoside]{scrreprt} % [11pt, parskip]{scrartcl} 
\usepackage[utf8]{inputenc}

\usepackage[colorlinks = true, citecolor=black, filecolor=black, linkcolor=black, urlcolor=black, linktocpage=true]{hyperref}
\usepackage{tikz}       % Zeichnungen
\usepackage{pgfplots}   % Diagramme

\begin{document}
\begin{tikzpicture}
\begin{axis}[
scatter/classes={a={blue},b={mark=triangle*,blue},c={mark=square*,blue}},
every x tick scale label/.style={at={(xticklabel cs:1)},anchor=south west},
every axis plot/.append style={mark=none} , 
legend pos=north east,
legend style={draw=none, fill=none},
]
\addplot [scatter, only marks, scatter src=explicit symbolic] 
    coordinates{
    (1,1) [a]
    (1,2) [b]
    (1,3) [c]
    (2,2) [a]
    };
\addplot [color=red]
coordinates{
    (1,1)
    (2,2)
};
\legend{~,~,Data,Fit}
\end{axis}
\end{tikzpicture}
\end{document}

I want now a legend of the following style (one liner):
standard mark, triangle, square Data

I've updated the sample cause I got new some new information that I also have to put a Fit into the data. In the picture you can find an example of how I'd like my legend to look like.

What I want

4
  • I am not sure if I have understood your question right, but are you searching for legend columns=-1? Sep 23 '18 at 19:34
  • That kinda works yes, But I have to change the legend command to \legend{~,~,Data}. But now there is the next problem, I found out now, that I also have to draw a line into this plot. So I also need a Legend entry therefore. I'd need now a 3x2 Matrix where the first 2 entrys in the second line are empty.
    – Steradiant
    Sep 23 '18 at 19:52
  • 1
    Could you perhaps include everything that you found out in your question? I am particularly curious about "that I also have to draw a line into this plot", which I do not understand. A cartoon showing what you'd like to achieve might also be advantageous.
    – user121799
    Sep 23 '18 at 20:28
  • 1
    @marmot done. I've updated it and added a picture of how I want the legend.
    – Steradiant
    Sep 23 '18 at 20:36
2

One can always patch legends together using \addlegendentry and \addlegendimage. And on can come up with whatever legend image code one likes. Here is an example, certainly improvable. To find appropriate coordinates for the marks, I looked up the line legend code in the manual.

\documentclass[11pt, a4paper, twoside]{scrreprt} % [11pt, parskip]{scrartcl} 
\usepackage[utf8]{inputenc}

\usepackage[colorlinks = true, citecolor=black, filecolor=black, linkcolor=black, urlcolor=black, linktocpage=true]{hyperref}
\usepackage{tikz}       % Zeichnungen
\usepackage{pgfplots}   % Diagramme

\pgfplotsset{%
my legend/.style={legend image code/.code={%
\node[##1,anchor=west] at (0cm,0cm){\pgfuseplotmark{*}};
\node[##1] at (0.3cm,0cm){\pgfuseplotmark{triangle*}};
\node[##1,anchor=east] at (0.6cm,0cm){\pgfuseplotmark{square*}};
}},%
}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
scatter/classes={a={blue},b={mark=triangle*,blue},c={mark=square*,blue}},
every x tick scale label/.style={at={(xticklabel cs:1)},anchor=south west},
every axis plot/.append style={mark=none} , 
legend pos=north east,
legend style={draw=none, fill=none},
]
\addlegendimage{my legend,blue}
\addlegendentry{Data}
\addlegendimage{line legend,red,line width=1pt}
\addlegendentry{Fit}

\addplot [scatter, only marks, scatter src=explicit symbolic] 
    coordinates{
    (1,1) [a]
    (1,2) [b]
    (1,3) [c]
    (2,2) [a]
    };
\addplot [color=red]
coordinates{
    (1,1)
    (2,2)
};
\end{axis}
\end{tikzpicture}
\end{document}

enter image description here

2
  • I've tried it with \addlegendentry before but I didn't know that how to deal with that legend image code. Thank you very much!
    – Steradiant
    Sep 23 '18 at 21:43
  • @vogs BTW, if an answer works for you, it would be great if you could consider accepting it. You can accept an answer by clicking on the check mark left of it. I am not specifically talking about this answer, but it seems that you have not accepted a single answer, and hence I am wondering if you know how to accept an answer.
    – user121799
    Sep 23 '18 at 22:00

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

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