
Me gustaría representar estos dos gráficos usando tikzpicture
. No los necesito unidos como se muestra a continuación, pero me gustaría tener algo similar.
Puedo generar un diagrama de dispersión como se muestra a continuación:
Donde tengo dos grupos únicos/distintos. Me gustaría dibujar la línea linealmente separable como en el ejemplo de aprendizaje supervisado, y al mismo tiempo dibujar los grupos en el ejemplo de aprendizaje no supervisado.
Además, sería genial tener los colores/puntos más grandes.
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}
Respuesta1
Podrías usarlo scatter/classes
para formatear los puntos de los dos grupos de manera diferente, pero es más fácil dividirlos en dos \addplot
. Para configurar el formato, utilice, por ejemplo, \addplot [blue, only marks, mark=*, mark size=5] ...
, que supongo que en su mayoría se explican por sí solos.
Para la línea y los círculos es casi solo un caso de usar comandos normales de TikZ. Solo tenga en cuenta que, de forma predeterminada, si lo hace \draw (x,y) ..
dentro de un axis
entorno x
y y
no está en el sistema de coordenadas del archivo axis
.A menos queusted a) usa (axis cs:x,y)
, ob) agrega \pgfplotsset{compat=1.11}
(o un número de versión superior), en cuyo caso axis cs
se convierte en el valor predeterminado.
Tenga en cuenta además que un circle[radius=2]
interior axis
requiere compat=1.11
(creo), pero el radio está nuevamente en las coordenadas del eje, por lo que en este caso se convertiría en una elipse. Lo que podrías hacer en su lugar es definir una coordenada dentro axis
y dibujar el círculo fuera de ella. Ambos se demuestran en el código siguiente.
\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}
dos ejes
Existen varios métodos para colocar dos parcelas una al lado de la otra. Podrías agregar dos tikzpicture
s uno detrás del otro, o podrías tener dos axis
entornos en el mismo tikzpicture
y colocar el segundo con \begin{axis}[at={(x,y)},...
. Personalmente me gusta el groupplot
entorno de la groupplots
biblioteca, que es para hacer cuadrículas de ejes.
\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}