在 latex 上画图

在 latex 上画图

大家新年快乐,请帮我在 Latex 上画这个图形

在此处输入图片描述

答案1

您可以执行类似以下的操作。我创建了几种样式以便于进行视觉修改,然后我创建了一个范围,其中更改了原点并进行了旋转。新的原点将位于灰色/绿色线的交叉点,并且 x 轴将与绿线相同。这样,找到所有相关元素的坐标就很简单了。

代码:

\documentclass[tikz,border=2mm]{standalone}

\tikzset
{% styles
  dot/.style={circle,draw,fill=#1,inner sep=0,minimum size=0.3cm},
  for text/.style={black,text width=2.4cm,align=center,font=\footnotesize},
  double arrow/.style={<->,rounded corners},
  margem/.style={latex-latex,thick,magenta!70!black},
  otima/.style={green!50!black,very thick}
}

\begin{document}
\begin{tikzpicture}[line cap=round,line join=round]
% axes
\draw[-latex] (-1,0) -- (10,0) node[below] {$x_1$};
\draw[-latex] (0,-1) --  (0,8) node[left]  {$x_2$};
% red dots
\foreach\i/\j in {5/2,6/2.5,6/1.5,7.1/1,7/2,6.9/3,7.9/2.5,8/1.5}
  \node[dot=red] at (\i,\j) {};
% blue dots
\foreach\i/\j in {2/4.8,3/5,2.5/5.8,3.5/6,4.5/6,5.5/6.5,4.5/7}
  \node[dot=blue] at (\i,\j) {};
% here I change the origin and rotate all around it
\begin{scope}[shift={(5,4)},rotate=30]
  % gray lines
  \foreach\i in {-40,-20,40}
    \draw[thick,gray!50] (\i:-4) -- (\i:4);
  % hiperplano de separação ótima
  \draw[otima] (-5,0) -- (5,0);
  % hiperplanos
  \foreach\i in {-1,1}
    \draw[dashed] (-5,\i) -- (5,\i);
  % some more dots
  \node[dot=red] at (0,-1) {};
  \foreach\i in {-1,1}
    \node[dot=blue] at (0.7*\i,1) {};
  % labels
  \draw[double arrow] (-0.7,1.25) |-++ (1.4,0.5) -- (0.7,1.25);
  \draw[double arrow] (5.1,-1)    -|++ (0.5,2)   -- (5.1,1);
  \draw (5.6,0)  --++ (1.5,0)                  node[right,for text] {Hiperplanos de Suporte};
  \draw (0,1.75) --++ (0,1.5)                  node[above,for text] {Vetores de Suporte}; 
  \draw[margem]       (-3.5,1)    --++ (0,-2)  node[below,for text] {Margem de separação};
  \draw[<-]           (4,-0.1)    --++ (0,-3)  node[below,for text] {Hiperplano de Separação Ótima};
  \draw[<-]  (0,-1) + (230:0.25)  --++ (230:2) node[below,for text] {Vetor de Suporte};
\end{scope}
\end{tikzpicture}
\end{document}

输出结果如下: 在此处输入图片描述

相关内容