As my colorbar contains only few values, I want to use further legend entries instead of a colorbar.

How can I (1) add additional entries to the existing legend or (2) add a second legend, which contains the entries for the used colors.

(As different marks are used, I guess it is best to use an area legend for the colors. Or someone might come with a better idea.)

MWE:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

x y z1 z2
5 5 1  1
6 6 1  2
7 7 2  1
8 8 2  2
}\mytable

\begin{document}

\begin{tikzpicture}
\begin{axis}[
colorbar,
colorbar style={ytick={1,2},ylabel={z2}},
legend pos=north west
]

\addplot [scatter,scatter src=\thisrow{z2},only marks,mark=*]         table [restrict expr to domain={\thisrow{z1}}{1:1}] {\mytable};
\addplot [scatter,scatter src=\thisrow{z2},only marks,mark=triangle*] table [restrict expr to domain={\thisrow{z1}}{2:2}] {\mytable};

\legend{$z1=1$,%
$z1=2$}

\end{axis}
\end{tikzpicture}

\end{document}


Update:

I'm struggling at finding the correct colors.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

x y z1 z2
5 5 1  1
6 6 1  2
7 7 2  3
8 8 2  4
9 9 2  5
}\mytable

\begin{document}

\begin{tikzpicture}
\begin{axis}[
legend pos=north west,
scatter,
scatter src=explicit,
point meta=explicit,
only marks
]

\addplot [mark=*]         table [meta=z2,restrict expr to domain={\thisrow{z1}}{1:1}] {\mytable};
\addplot [mark=triangle*] table [meta=z2,restrict expr to domain={\thisrow{z1}}{2:2}] {\mytable};

\legend{$z1=1$,%
$z1=2$}

\addlegendentry{$z2=1$}
\addlegendentry{$z2=2$}
\addlegendentry{$z2=3$}
\addlegendentry{$z2=4$}
\addlegendentry{$z2=5$}

\end{axis}
\end{tikzpicture}

\end{document}

• You can use \addlegendimage to add entries. This can be customized to show whatever you like. How should these additional entries look like? – marmot Apr 23 at 16:52

Here is a possible way. Use \addlegendimage. I use flexible version that allows you to adjust the legend image code to get whatever you have in mind.

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

x y z1 z2
5 5 1  1
6 6 1  2
7 7 2  1
8 8 2  2
}\mytable

\pgfplotsset{my legend/.style={
legend image code/.code={
\fill [#1] (0cm,-0.1cm) rectangle (0.6cm,0.4cm);
},
}}

\begin{document}

\begin{tikzpicture}
\begin{axis}[
%     colorbar,
%     colorbar style={ytick={1,2},ylabel={z2}},
legend pos=north west
]

\addplot [scatter,scatter src=\thisrow{z2},only marks,mark=*]         table [restrict expr to domain={\thisrow{z1}}{1:1}] {\mytable};
\addplot [scatter,scatter src=\thisrow{z2},only marks,mark=triangle*] table [restrict expr to domain={\thisrow{z1}}{2:2}] {\mytable};

\legend{$z1=1$,%
$z1=2$}

\end{axis}
\end{tikzpicture}
\end{document}


As for your updated question: the colors are derived from the color map. So you could just use this to color the legends.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

x y z1 z2
5 5 1  1
6 6 1  2
7 7 2  3
8 8 2  4
9 9 2  5
}\mytable

\begin{document}

\begin{tikzpicture}
\begin{axis}[
legend pos=north west,
scatter,
scatter src=explicit,
point meta=explicit,
only marks
]

\addplot [mark=*]         table [meta=z2,restrict expr to domain={\thisrow{z1}}{1:1}] {\mytable};
\addplot [mark=triangle*] table [meta=z2,restrict expr to domain={\thisrow{z1}}{2:2}] {\mytable};

\legend{$z1=1$,%
$z1=2$}
\pgfplotsinvokeforeach{1,...,5}
{\pgfplotscolormapdefinemappedcolor{\the\numexpr(#1-1)*250}
\colorlet{leg#1}{mapped color}
\addlegendentry{$z2=#1$}}
\end{axis}
\end{tikzpicture}

\end{document}


Of course that can be further automatized, e.g. the minimum and maximum can be extracted from the table, but this deserves IMHO a new question.

• Please see also my answer below. I removed the scatter part, because for two colors blue and red it was easy to name these, but not for three or more used colors. Or do you know a way to "ask" the scatter for its used color? – Ben Apr 23 at 17:15
• @Ben These colors are set in cycle lists. Is that what you are asking? – marmot Apr 23 at 17:18
• Are they? A scatter also does so? Maybe, if it results in something like my legend=\firstcolor – Ben Apr 23 at 17:19
• @Ben I am a bit lost. What precisely do you want to achieve? You removed scatter but want to reinstall it? And then what? – marmot Apr 23 at 17:20
• I could imagine two ways to get to my desired result. First, using your solution, I question how to specify the color of my legend if there are more than two used colors and scatter adds orange and dark yellow and some odd color. Second, using my solution (I specify the colors on my own, and I guess this could also be useful, all the more if z2 is nominal but not ordered), I need to change the color of the first two legend entries – Ben Apr 23 at 17:25

Thanks to marmot's many comments pointing me towards addlegendimage and point meta, I got the following.

\documentclass[border=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}

x y z1 z2
5 5 1  1
6 6 1  2
7 7 2  1
8 8 2  2
}\mytable

\begin{document}

\begin{tikzpicture}
\begin{axis}[
legend pos=north west
]

\addplot [scatter,scatter src=explicit,point meta=explicit,only marks,mark=*]         table [meta=z2,restrict expr to domain={\thisrow{z1}}{1:1}] {\mytable};
\addplot [scatter,scatter src=explicit,point meta=explicit,only marks,mark=triangle*] table [meta=z2,restrict expr to domain={\thisrow{z1}}{2:2}] {\mytable};

\legend{$z1=1$,%
$z1=2$}

\addlegendentry{$z2=1$}
\addlegendentry{$z2=2$}