
Eu gostaria de representar esses dois gráficos usando tikzpicture
. Não preciso deles unidos como abaixo, mas gostaria de ter algo semelhante.
Posso gerar um gráfico de dispersão conforme abaixo:
Onde tenho dois clusters únicos/distintos. Gostaria de traçar a linha linearmente separável como no exemplo de aprendizagem supervisionada, ao mesmo tempo que desenhava os agrupamentos no exemplo de aprendizagem não supervisionada.
Além disso, ter as cores/pontos maiores seria ótimo.
Código:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
axis lines = left,
xlabel = x,
ylabel = y,
]
\addplot[only marks] table [%
x = x,
y = y,
col sep = comma]{
x, y
%cluster 1
2, 3
3, 5
4, 5
3, 8
5, 9
3, 2
5, 6
6, 6
7, 9
10, 4
11, 5
9, 4
%cluser 2
20, 10
21, 12
24, 12
25, 13
27, 14
22, 13
23, 15
25, 10
15, 14
};
\end{axis}
\end{tikzpicture}
\end{document}
Responder1
Você poderia usar scatter/classes
para formatar os pontos dos dois clusters de maneira diferente, mas é mais fácil simplesmente dividi-los em dois \addplot
s. Para definir a formatação, use, por exemplo \addplot [blue, only marks, mark=*, mark size=5] ...
, que são em sua maioria autoexplicativos, eu acho.
Para a linha e os círculos é quase apenas um caso de usar comandos normais do TikZ. Apenas observe que, por padrão, se você fizer isso \draw (x,y) ..
dentro de um axis
ambiente x
e y
não estiver no sistema de coordenadas do arquivo axis
.A menos quevocê a) usa (axis cs:x,y)
ou b) adiciona \pgfplotsset{compat=1.11}
(ou um número de versão superior), caso em que axis cs
se torna o padrão.
Observe ainda que a circle[radius=2]
dentro de an axis
requer compat=1.11
(eu acho), mas o raio está novamente nas coordenadas do eixo, portanto, neste caso, ele se tornaria uma elipse. O que você poderia fazer é definir uma coordenada dentro de axis
e desenhar o círculo fora dela. Ambos são demonstrados no código abaixo.
\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11} % <-- added
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
axis lines = left,
xlabel = $x$,
ylabel = $y$,
clip mode=individual % so things drawn by \draw and similar are not cut off
]
\addplot [blue, only marks, mark=*, mark size=5] table [%
x = x,
y = y,
col sep = comma]{
x, y
%cluster 1
2, 3
3, 5
4, 5
3, 8
5, 9
3, 2
5, 6
6, 6
7, 9
10, 4
11, 5
9, 4
};
\addplot+[red, only marks, mark=*, mark size=5] table [%
x = x,
y = y,
col sep = comma]{
x, y
20, 10
21, 12
24, 12
25, 13
27, 14
22, 13
23, 15
25, 10
15, 14
};
% to be able to use axis coordinates with \draw directly you need
% \pgfplotsset{compat=1.11} or a higher version
% if that is not present, use (axis cs:4,14) instead of (4,14),
% to specify that the values should be interpreted as axis coordinates
\draw [dashed] (4,14) -- (25,2);
% save a coordinate for use later
\coordinate (c2) at (23,12);
% the blue circle is drawn inside the axis environment, and in axis coordinates
% hence it becomes an ellipse
\draw [blue, dashed] (6,6) circle[radius=5];
\end{axis}
% the red circle is drawn outside the axis, so actually looks like a circle,
% but the radius has no relation to the axis coordinates
\draw [red, dashed] (c2) circle[radius=2cm];
\end{tikzpicture}
\end{document}
Dois eixos
Existem vários métodos para obter dois gráficos próximos um do outro. Você pode adicionar dois tikzpicture
s um após o outro, ou pode ter dois axis
ambientes no mesmo tikzpicture
e posicionar o segundo com \begin{axis}[at={(x,y)},...
. Pessoalmente gosto do groupplot
ambiente da groupplots
biblioteca, que serve para fazer grades de eixos.
\documentclass{article}
\usepackage{pgfplots}
\usepgfplotslibrary{groupplots}
\pgfplotsset{compat=1.11}
\begin{document}
\begin{tikzpicture}
\begin{groupplot}[
group style={
group size=2 by 1,
horizontal sep=1.5cm
},
axis lines = left,
xlabel = $x$,
ylabel = $y$,
width=5cm, % <-- set size of axes
clip mode=individual, % to avoid \draws being cut off
title style={yshift=1mm, font=\bfseries\sffamily}
]
\nextgroupplot[title=Supervised learning]
\addplot [blue, only marks, mark=*, mark size=3] table [%
x = x,
y = y,
col sep = comma]{
x, y
%cluster 1
2, 3
3, 5
4, 5
3, 8
5, 9
3, 2
5, 6
6, 6
7, 9
10, 4
11, 5
9, 4
};
\addplot+[red, only marks, mark=*, mark size=3] table [%
x = x,
y = y,
col sep = comma]{
x, y
20, 10
21, 12
24, 12
25, 13
27, 14
22, 13
23, 15
25, 10
15, 14
};
% to be able to use axis coordinates with \draw directly you need
% \pgfplotsset{compat=1.11} or a higher version
% if that is not present, use (axis cs:4,14) instead of (4,14),
% to specify that the values should be interpreted as axis coordinates
\draw [dashed] (4,14) -- (25,2);
\nextgroupplot[title=Unsupervised learning]
\addplot [blue, only marks, mark=*, mark size=3] table [%
x = x,
y = y,
col sep = comma]{
x, y
%cluster 1
2, 3
3, 5
4, 5
3, 8
5, 9
3, 2
5, 6
6, 6
7, 9
10, 4
11, 5
9, 4
};
\addplot+[red, only marks, mark=*, mark size=3] table [%
x = x,
y = y,
col sep = comma]{
x, y
20, 10
21, 12
24, 12
25, 13
27, 14
22, 13
23, 15
25, 10
15, 14
};
% save a coordinate for use later
\coordinate (c2) at (23,12);
% the blue circle is drawn inside the axis environment, and in axis coordinates
% hence it becomes an ellipse
\draw [blue, dashed] (6,6) circle[radius=5];
\end{groupplot}
% the red circle is drawn outside the axis, so actually looks like a circle,
% but the radius has no relation to the axis coordinates
\draw [red, dashed] (c2) circle[radius=1cm];
\end{tikzpicture}
\end{document}