5

I wanted to draw an ellipse with an arrow like this:

enter image description here

on my plot.

Here is the code that I have so far:

\documentclass{article}
\usepackage{tikz,pgfplots}
\usepackage{filecontents}

\begin{filecontents*}{data2.csv}
x1 y1 x2 y 2
1, 0, 0 , -1
0.5, 1, -1, 1
0.5, 1.5, 0.5, -1
\end{filecontents*}

\pagestyle{empty}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width=0.8\textwidth,
    height=0.6\textwidth,
    xlabel={$x$},
    ylabel={$y$},
    xlabel style={below right},
    ylabel style={above left},
    xmin=-2.5,
    xmax=2.5,
    ymin=-2.5,
    ymax=2.5,
    legend style={font=\fontsize{7}{9}\selectfont}]
\addplot [only marks, line width = 0.3mm, mark=triangle*, red,mark options={scale=1.2}]table[x index=0, y index=1, col sep=comma, only marks] {data2.csv}; \addlegendentry{$Class 1$}
\addplot [only marks, line width = 0.3mm, mark=x, blue, mark options={scale=1.2}]table[x index=2, y index=3, col sep=comma, only marks] {data2.csv};\addlegendentry{$Class 2$}
\addplot [blue, line width = 0.3mm, domain=-2.5:2.5, samples=2]{-0.5*x + -0.125};\addlegendentry{$kNN$}
\addplot [dashed, blue, line width = 0.3mm] plot coordinates{(1,0) (0.5,-1)};
\end{axis}
\end{tikzpicture}
\end{document} 
4

With fit library you can draw ellipse which fit the two nodes and rotate it with rotate fit=<angle>

\documentclass{article}
\usepackage{tikz,pgfplots}
\usepackage{filecontents}
\usetikzlibrary{fit,shapes}

\begin{filecontents*}{data2.csv}
x1 y1 x2 y 2
1, 0, 0 , -1
0.5, 1, -1, 1
0.5, 1.5, 0.5, -1
\end{filecontents*}

\pagestyle{empty}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width=0.8\textwidth,
    height=0.6\textwidth,
    xlabel={$x$},
    ylabel={$y$},
    xlabel style={below right},
    ylabel style={above left},
    xmin=-2.5,
    xmax=2.5,
    ymin=-2.5,
    ymax=2.5,
    legend style={font=\fontsize{7}{9}\selectfont}]
\addplot [only marks, line width = 0.3mm, mark=triangle*, red,mark options={scale=1.2}]table[x index=0, y index=1, col sep=comma, only marks] {data2.csv}; 
\addlegendentry{$Class 1$}
\addplot [only marks, line width = 0.3mm, mark=x, blue, mark options={scale=1.2}]table[x index=2, y index=3, col sep=comma, only marks] {data2.csv};
\addlegendentry{$Class 2$}
\addplot [blue, line width = 0.3mm, domain=-2.5:2.5, samples=2]{-0.5*x + -0.125};
\addlegendentry{$kNN$}
\addplot [dashed, blue, line width = 0.3mm] plot coordinates{(1,0) (0.5,-1)}node[pos=0](A){}node[pos=1](B){};
\node (C)[very thick,draw=green!75!black,ellipse,rotate fit=-35,fit=(A) (B),fill=red,opacity=0.2] {};
\draw [<-,ultra thick,blue,align=center,anchor=south west](C.80)--+(5mm,5mm)node[inner sep=0pt,font=\small]{nearest \\ neighbour};
\end{axis}
\end{tikzpicture}
\end{document} 

enter image description here

  • 1
    This has the downside that you have to place the nodes which should fit into the ellipse "by hand". I would recommend to add the nodes to the \addplot [only mark]s (like I did for the line) using the pos key. Here it will "snap" to the coordinates so they will easily be found. Then your solution will even be better than mine ;) – Stefan Pinnow Oct 17 '16 at 10:31
  • Thanks for your solution. How do you go about making the ellipse filled with a transparency? – Joe Oct 17 '16 at 14:18
  • 1
    Just add fill=red,opacity=0.2 for node C – Salim Bou Oct 17 '16 at 14:50
3

There are plenty of ways to draw the ellipse with the arrow and the text. Here I present two of them in one solution. For more details have a look at the comments of the code.

\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
    % add the `shapes' library to be able to draw ellipse like nodes
    \usetikzlibrary{
        shapes,
    }
    \pgfplotsset{
        % to place the labels "a bit better"
        compat=1.3,
    }
\usepackage{filecontents}
    \begin{filecontents*}{data2.csv}
        x1 y1 x2 y 2
        1, 0, 0 , -1
        0.5, 1, -1, 1
        0.5, 1.5, 0.5, -1
    \end{filecontents*}
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            width=0.8\textwidth,
            height=0.6\textwidth,
            xlabel={$x$},
            ylabel={$y$},
            xlabel style={below right},
            ylabel style={above left},
            xmin=-2.5,
            xmax=2.5,
            ymin=-2.5,
            ymax=2.5,
            legend style={font=\fontsize{7}{9}\selectfont},
        ]
            \addplot [
                only marks,
                line width = 0.3mm,
                mark=triangle*,
                red,
                mark options={scale=1.2},
            ] table [
                x index=0,
                y index=1,
                col sep=comma,
                only marks,
            ] {data2.csv};
                \addlegendentry{$Class 1$}
            \addplot [
                only marks,
                line width = 0.3mm,
                mark=x,
                blue,
                mark options={scale=1.2},
            ] table [
                x index=2,
                y index=3,
                col sep=comma,
                only marks,
            ] {data2.csv};
                \addlegendentry{$Class 2$}
            \addplot [
                blue,
                line width = 0.3mm,
                domain=-2.5:2.5,
                samples=2,
            ] {-0.5*x + -0.125}
                % though we don't have to search both coordinates, where to
                % place the ellipse, add a node on the plot and just vary
                % the `pos' value
                node [
                    pos=0.65,
                    shape=ellipse,
                    rotate=55,
                    % use relative width and height values so these values
                    % don't have to be adjusted when changing the width or
                    % height of the axis environment
                    minimum width=0.25*\pgfkeysvalueof{/pgfplots/width},
                    minimum height=0.1*\pgfkeysvalueof{/pgfplots/height},
                    very thick,
                    draw=green!75!black,
                    %
                    % ---------------------------------------------------------
%                    % either draw directly a pin ...
%                    pin={
%                        [%
%                            pin distance=1ex,
%                            pin edge={
%                                <-,
%                                blue,
%                                thick,
%                            },
%                            align=center,
%                            text=black,
%                        ]
%                        -20:nearest \\ neighbour},
                    % ---------------------------------------------------------
                ] (ellipse) {}
            ;
                \addlegendentry{$kNN$}
            \addplot [
                dashed,
                blue,
                line width = 0.3mm,
            ] plot coordinates {(1,0) (0.5,-1)};

        % ---------------------------------------------------------------------
        % or ...
        % draw the node with the text somewhere and then "connect" the text
        % with the ellipse
            \node [
                align=center,
                anchor=east,
            ] (test) at (axis description cs:1,0.65) {nearest \\ neighbour};
            \draw [
                ->,
                blue,
                thick,
            ] (test) -- (ellipse);
%           % (or if you want to change the default anchors provide some angles
%           %  (keep in mind that the 0 angle of the ellipse is rotated))
%            ] (test.230) -- (ellipse.-15);
        % ---------------------------------------------------------------------

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

image showing the result of above code

2

Two file in one folder. We have the data.dat file:

x1 y1 x2 y 2
1, 0, 0 , -1
0.5, 1, -1, 1
0.5, 1.5, 0.5, -1

The chart.tex file:

\documentclass{standalone}\usepackage{tikz,pgfplots}\pagestyle{empty}
\begin{document}
\begin{tikzpicture}
\begin{axis}[width=0.8\textwidth,height=0.6\textwidth,
xlabel={$x$},ylabel={$y$},
xlabel style={below right},ylabel style={above left},
xmin=-2.5,xmax=2.5,ymin=-2.5,ymax=2.5,
legend style={font=\fontsize{7}{9}\selectfont}]
\addplot [only marks, line width = 0.3mm, mark=triangle*, red,mark options={scale=1.2}]table[x index=0, y index=1, col sep=comma, only marks] {data.dat}; \addlegendentry{$Class 1$}
\addplot [only marks, line width = 0.3mm, mark=x, blue, mark options={scale=1.2}]table[x index=2, y index=3, col sep=comma, only marks] {data.dat};\addlegendentry{$Class 2$}
\addplot [blue, line width = 0.3mm, domain=-2.5:2.5, samples=2]{-0.5*x + -0.125};\addlegendentry{$kNN$}
\addplot [dashed, blue, line width = 0.3mm] plot coordinates{(1,0) (0.5,-1)};
\end{axis}
\path[->,blue,line width=2pt,draw] (7,3.4) -- (5.9,3);
\draw (7,3.5) node[above, scale = 0.8] {nearest neighbor};
\draw[green,line width=2pt,rotate=60] (4.55,-3.45) circle (1 and .4);
\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.