Tikzpicture의 감독 및 비지도 학습 그래픽

Tikzpicture의 감독 및 비지도 학습 그래픽

를 사용하여 이 두 플롯을 표현하고 싶습니다 tikzpicture. 아래와 같이 결합할 필요는 없지만 비슷한 것을 갖고 싶습니다.

여기에 이미지 설명을 입력하세요

아래와 같이 산점도를 생성할 수 있습니다.

여기에 이미지 설명을 입력하세요

두 개의 고유하고 구별되는 클러스터가 있습니다. 지도 학습 예시에서와 같이 선형으로 분리 가능한 선을 그리는 동시에 비지도 학습 예시에서도 클러스터를 그리고 싶습니다.

또한 색상/더 큰 포인트를 갖는 것이 좋습니다.

암호:

\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}

답변1

두 클러스터의 포인트 형식을 다르게 지정할 수 있지만 scatter/classes두 클러스터로 분할하는 것이 더 쉽습니다 \addplot. 형식을 설정하려면 eg 를 사용하세요 \addplot [blue, only marks, mark=*, mark size=5] .... 대부분 설명이 필요합니다.

선과 원의 경우 거의 일반적인 TikZ 명령을 사용하는 경우입니다. 기본적으로 환경 \draw (x,y) ..내부에서 수행하고 의 좌표계에 있지 않은 경우 참고 하세요 .axisxyaxis하지 않는 한a) 을 사용 (axis cs:x,y)하거나 b) 추가 \pgfplotsset{compat=1.11}(또는 더 높은 버전 번호) 중 하나를 선택하세요. 이 경우 axis cs기본값이 됩니다.

내부에는 circle[radius=2]( 제 생각에는) axis필요 compat=1.11하지만 반경은 다시 축 좌표에 있으므로 이 경우 타원이 됩니다. 대신에 할 수 있는 일은 내부에 좌표를 정의 axis하고 외부에 원을 그리는 것입니다. 두 가지 모두 아래 코드에 설명되어 있습니다.

여기에 이미지 설명을 입력하세요

\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}

두 개의 축

두 개의 플롯을 나란히 배치하는 방법에는 여러 가지가 있습니다. 두 개의 tikzpictures를 차례로 추가하거나 axis동일한 환경에 두 개의 환경을 두고 tikzpicture두 번째 환경을 \begin{axis}[at={(x,y)},.... 개인적으로 저는 축 그리드를 만드는 라이브러리 groupplot의 환경을 좋아합니다.groupplots

여기에 이미지 설명을 입력하세요

\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}

관련 정보