# PGFPlot scatter plot with random color and legend

I want to draw a PGFplot where every point has a separate legend entry and a random color. So far I have just one legend entry and everything is black:

\begin{tikzpicture}
\pgfplotsset{
scale only axis,
}
\begin{axis}[
xlabel={A},
ylabel={B}
]
coordinates{
(1.25,96.88)
(0.58,96.25)
(0.77,96.25)
(1.17,96.04)
(0.89,96.04)
(3.50,95.83)
(1.11,95.63)
(1.12,95.42)
(2.32,95.42)
(0.48,95.21)
(1.65,94.58)
(4.08,94.38)
(5.15,94.38)
(1.07,94.17)
(0.13,93.75)
(0.21,92.71)
(0.10,89.38)
};
\end{axis}
\end{tikzpicture}


• Welcome to TeX.SE :) Apr 28 at 18:56

\documentclass{article}
\usepackage{xcolor}
\usepackage{pgfplots}
\usepackage{pgfplotstable}
\pgfplotsset{compat=1.18}

1.25 96.88
0.58 96.25
0.77 96.25
1.17 96.04
0.89 96.04
3.50 95.83
1.11 95.63
1.12 95.42
2.32 95.42
0.48 95.21
1.65 94.58
4.08 94.38
5.15 94.38
1.07 94.17
0.13 93.75
0.21 92.71
0.10 89.38
} \table

\pgfplotstablegetrowsof{\table}
\pgfmathsetmacro{\maxRowIndex}{\pgfplotsretval-1}

\pgfplotsset{
select row/.style={
x filter/.code={\ifnum\coordindex=#1\else\def\pgfmathresult{}\fi}
},
}

\pgfmathsetseed{1} % for reproducibility

\begin{document}
\begin{tikzpicture}
\begin{axis}
\pgfplotsinvokeforeach{0,...,\maxRowIndex}{
\pgfmathsetmacro{\R}{rnd}
\pgfmathsetmacro{\G}{rnd}
\pgfmathsetmacro{\B}{rnd}
\definecolor{color#1}{rgb}{\R,\G,\B}
}
\end{axis}
\end{tikzpicture}
\end{document}

• Why don't you use \pgfmathsetmacro{\R}{rnd} directly? Apr 28 at 19:20
• @GuestPost Good point, thanks. I updated the answer. Apr 28 at 19:26
• You can also remove the packages xcolor and pgfplots because pgfplotstable is a lightweight sub-package of pgfplots and pgf/TikZ (pgf/TikZ is the basis of pgfplots(table)) uses already the xcolor package. Apr 28 at 19:47

This is totally not an answer, a bit involving with no legendries, only a comment for fun.

The figure reminds one of my favorite illustration about some stuff like social relationships, freedom, etc. for a community. Just random colors for characteristic characters, random centers for places, and random radius for scope around people.

Animation would be more fun!

\documentclass[border=2mm]{standalone}
\usepackage{tikz}
\pagecolor{yellow!5}
\begin{document}
\def\n{8}
\foreach \factor in {1,.5,rnd}{
\pgfmathsetseed{2022}
\begin{tikzpicture}
\foreach \i in {0,...,\n}
\foreach \j in {0,...,\n}{
\pgfmathsetmacro{\R}{rnd}
\pgfmathsetmacro{\G}{rnd}
\pgfmathsetmacro{\B}{rnd}
\definecolor{randcolor}{rgb}{\R,\G,\B}