0

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}
  ]
    \addplot[only marks]
    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)
    };
    \addlegendentry{plot 1}
  \end{axis}
\end{tikzpicture}

enter image description here

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

2 Answers 2

1

enter image description here

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

\pgfplotstableread{
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}
        \addplot[only marks,select row=#1,color#1] table \table;
        \addlegendentry{plot #1}
    }
  \end{axis}
\end{tikzpicture}
\end{document}
3
  • 1
    Why don't you use \pgfmathsetmacro{\R}{rnd} directly?
    – GuestPost
    Apr 28 at 19:20
  • @GuestPost Good point, thanks. I updated the answer.
    – user187803
    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.
    – GuestPost
    Apr 28 at 19:47
1

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!

enter image description here

\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}
\pgfmathsetmacro{\randradius}{rnd*1pt}
\coordinate (randcenter) at (\i+rnd,\j+rnd);
\fill[randcolor,fill opacity=\factor] (randcenter) circle(\randradius);
}
\end{tikzpicture}
}
\end{document}

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.