8

I wanted to add some transparent circles around specific points in my pgfplot like this:

enter image description here

here is what I have so far:

\documentclass{article}
\usepackage{tikz,pgfplots}
\pgfplotsset{compat=newest}
\usepgfplotslibrary{groupplots}
\usetikzlibrary{fit,shapes}

\newcommand{\dsnfive}{prob5_2.dat}
\begin{filecontents*}{\dsnfive}
2   7   4   8
3   6   5   8
2   5   9   5
3   5   9   9
3   3   9   4
2   2   8   9
5   1   8   8
6   2   6   9
8   1   7   4
6   4   4   4
\end{filecontents*}

\pagestyle{empty}
\begin{document}
\begin{figure}
\begin{center}
\begin{tikzpicture}
\begin{groupplot}[group style={group size=2 by 2, horizontal sep=4em, vertical sep=5em}, height = 8cm, width = 10cm]

\nextgroupplot[xmin=1, xmax=10, ymin=0, ymax=10, legend style={at={(1,1)},anchor = north west, font=\fontsize{7}{9}\selectfont}]

\addplot [only marks,mark=*,mark options={scale=1.2, fill=blue}]table[x index=0, y index=1, col sep=comma, only marks,col sep=space] {\dsnfive}; \addlegendentry{$class 1$}

\addplot [only marks,mark=*,mark options={scale=1.2, fill=red}]table[x index=2, y index=3, col sep=comma, only marks,col sep=space] {\dsnfive}; \addlegendentry{$class 2$}

%\addplot [mark=*, mark size=0.3cm,color=red!20] coordinates {(2,7)};


\end{groupplot}
\end{tikzpicture}
\caption[]{Plot of Data}
\end{center}
\end{figure}

\end{document} 
Stefan Pinnow
  • 29,535
Joe
  • 9,080

1 Answers1

9

Besides adding the circles "manually" as John Kormylo has suggested in the comments below the question, here a more "automatic" solution.

(This is very related your question: Add ellipse around data point part 2.)

% used PGFPlots v1.14
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
        \newcommand{\dsnfive}{prob5_2.dat}
    \begin{filecontents*}{\dsnfive}
        2   7   4   8
        3   6   5   8
        2   5   9   5
        3   5   9   9
        3   3   9   4
        2   2   8   9
        5   1   8   8
        6   2   6   9
        8   1   7   4
        6   4   4   4
    \end{filecontents*}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin=1,
            xmax=10,
            ymin=0,
            ymax=10,
            legend pos=outer north east,    % <-- used predefined command to position legend
            only marks,
            mark=*,
            mark options={
                scale=1.2,
            },
        ]
                % store number of data points
                \pgfplotstablegetrowsof{\dsnfive}
                \pgfmathtruncatemacro{\N}{\pgfplotsretval-1}

            \addplot+ [
                blue,
                % -------------------------------------------------------------
                % comment this block to hide names of the coords
                % -----
                % text of node coords should be the same as for the
                % corresponding coordinates
                nodes near coords=a\coordindex,
                % increase the distance of the nodes a bit
                nodes near coords align={above=1ex},
                % -------------------------------------------------------------
            ] table [x index=0,y index=1] {\dsnfive}
                % set a coordinate on each data point
                % (needed for the `fit' library solution)
                \foreach \i in {0,...,\N} {
                    coordinate [pos=\i/\N] (a\i)
                }
            ;
                \addlegendentry{$class 1$}
            \addplot+ [
                red,
                % -------------------------------------------------------------
                nodes near coords=b\coordindex,
                nodes near coords align={below=1ex},
                % -------------------------------------------------------------
            ] table [x index=2,y index=3] {\dsnfive}
                % set a coordinate on each data point
                % (needed for the `fit' library solution)
                \foreach \i in {0,...,\N} {
                    coordinate [pos=\i/\N] (b\i)
                }
            ;
                \addlegendentry{$class 2$}

        \end{axis}
            % now add the circles to the points
            \foreach \i in {a0,b6,b8} {
                \draw [green!60!black,thick] (\i) circle (5pt);
            }
    \end{tikzpicture}
\end{document}

image showing the result of above code

Stefan Pinnow
  • 29,535
  • Thanks for your solution. I did try your posts to my previous questions, however, when I tried to add a transparent colored circle over the points, I could not see the actual data point anymore. Thanks! – Joe Oct 24 '16 at 05:20
  • 1
    Sorry, but from that I description I don't have a clue what could be the reason for that. Please note that I have use \draw (only) in the above solution, which means that there is no fill color and consequently is transparent. – Stefan Pinnow Oct 24 '16 at 05:44
  • 1
    I think this will end in a discussion, so maybe we should continue in a chat. Would you open one? – Stefan Pinnow Oct 24 '16 at 05:44
  • My apologies. I wanted to also add a transparent color fill to one of the circles, in my attempt to do so, I covered up the actual data point. Is there a way to add a transparent color fill to one of the circles, but still view the point? – Joe Oct 24 '16 at 06:11
  • 1
    Sure, add e.g. fill opacity=0.5 to the circle options (and of course a fill color). But I would suggest to "fill" a (transparent) color behind the data point like the ellipses in the other quoted question of you. – Stefan Pinnow Oct 24 '16 at 06:20
  • Thanks again! I tried your suggestion...\addplot [] coordinates{(2,7)}node[pos=0](A){}; \node [very thick,draw=green!75!black,circle,fit=(A),fill=red,opacity=0.2] {};...but how do you control the diameter of the colored circle? It is currently too large behind the points. – Joe Oct 24 '16 at 07:08