tikz의 그래픽에 텍스트 추가

tikz의 그래픽에 텍스트 추가

다음과 같은 그래픽이 있습니다.

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

솔루션에서 가져온 것여기. 그래픽에 다음과 같은 추가 텍스트를 추가하고 싶습니다.

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

"과적합", "과소적합" 및 "좋은 균형" 레이블과 함께 높은 편향, 낮은 편향 및 둘의 조합을 표시합니다. 이 텍스트를 현재 버전에 어떻게 통합할 수 있나요?tikzpicture

암호:

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc}
\begin{document}
\begin{tikzpicture}[font=\sffamily,
declare function={f(\x)=0.5*pow(abs(\x-2),2)-0.06*pow(\x-2,3);}]
 \foreach \Z in {1,...,42}
 {\pgfmathsetmacro{\X}{\Z/10}
 \pgfmathsetmacro{\Y}{f(\X)+0.9*rnd}
 \ifnum\Z=1
  \xdef\LstOne{(\X,\Y)}
  \xdef\LstTwo{"(\X,\Y)"}
 \else
  \xdef\LstOne{\LstOne (\X,\Y)}
  \xdef\LstTwo{\LstTwo,"(\X,\Y)"}
 \fi}
%  \begin{scope}[local bounding box=over0]
%  \foreach \Z in {1,...,42}
%  {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
%  \fill \Coor circle[radius=1pt];
%  }
%  \draw plot[smooth] coordinates \LstOne;
%  \end{scope}
 \begin{scope}[local bounding box=over,xshift=-5cm]
 \foreach \Z in {1,...,40}
 {\pgfmathsetmacro{\Last}{{\LstTwo}[\Z-1]}
 \pgfmathsetmacro{\Current}{{\LstTwo}[\Z]}
 \pgfmathsetmacro{\Next}{{\LstTwo}[\Z+1]}
 %\typeout{\Last,\Current,\Next}
  \edef\temp{\noexpand\path ($0.6*\Current+0.2*\Last+0.2*\Next$)   coordinate 
  (p\Z);}
  \temp
  \ifnum\Z=1
  \xdef\LstThree{(p\Z)}
  \else
  \xdef\LstThree{\LstThree (p\Z)}
  \fi
  }
 \foreach \Z in {1,...,42}
 {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
 \fill \Coor circle[radius=1pt];
 }
 \draw[thick,blue] plot[smooth] coordinates \LstThree;
 \end{scope}
 %
 \begin{scope}[local bounding box=good,xshift=-10cm]
 \foreach \Z in {1,...,42}
 {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
 \fill \Coor circle[radius=1pt];
 }
 \draw[thick,blue] plot[smooth,domain=0.1:4.2,variable=\x] (\x,{f(\x)+0.45});
 \end{scope}
 %
 \begin{scope}[local bounding box=under,xshift=-15cm]
 \foreach \Z in {1,...,42}
 {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
 \fill \Coor circle[radius=1pt];
 }
 \draw[thick,blue] (0.1,0.4) -- (4.2,2);
 \end{scope}
 %
 \foreach \X in {over,good,under}
 {\draw[gray,thin] ([xshift=-3pt,yshift=3pt]\X.north west) rectangle 
 ([xshift=3pt,yshift=-3pt]\X.south east);
 \draw[stealth-stealth,thick] ([xshift=-3pt,yshift=3pt]\X.north west) node[right=1.5pt,fill=white]{Values} 
 |- ([xshift=3pt,yshift=-3pt]\X.south east) node[below left]{Time};}
\end{tikzpicture}
\end{document}

답변1

\node[<options>](<name>) at (<coordinate>){<text>};안에 설명이나 텍스트를 추가할 수 있습니다 scope. 또한 TikZ라이브러리를 사용하면 positioning다른 노드와 관련하여 노드를 찾을 수 있습니다.위/아래, 오른쪽/왼쪽그리고 그들의 조합. 그 뒤에는 선택적 인수 <shifting part>와 가 올 수 있습니다 <of part>.

예를 들어 내부는 다음과 같습니다 scope.

\node[red] (lowlow) at (2.2,2.5) {Low bias, low variance}; % <-- here

또는

\node[red, below= -2.75cm of over] (lowlow)  {Low bias, low variance}; % <-- here

그런 다음 외부에서:

 \node[green,below = 3cm of lowlow] {Good balance}; % <-- here

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

MWE:

\documentclass[tikz,border=3.14mm]{standalone}
\usetikzlibrary{calc,positioning}
\begin{document}
\begin{tikzpicture}[font=\sffamily,
declare function={f(\x)=0.5*pow(abs(\x-2),2)-0.06*pow(\x-2,3);}]
 \foreach \Z in {1,...,42}
 {\pgfmathsetmacro{\X}{\Z/10}
 \pgfmathsetmacro{\Y}{f(\X)+0.9*rnd}
 \ifnum\Z=1
  \xdef\LstOne{(\X,\Y)}
  \xdef\LstTwo{"(\X,\Y)"}
 \else
  \xdef\LstOne{\LstOne (\X,\Y)}
  \xdef\LstTwo{\LstTwo,"(\X,\Y)"}
 \fi}
%  \begin{scope}[local bounding box=over0]
%  \foreach \Z in {1,...,42}
%  {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
%  \fill \Coor circle[radius=1pt];
%  }
%  \draw plot[smooth] coordinates \LstOne;
%  \end{scope}
 \begin{scope}[local bounding box=over,xshift=-5cm]
 \foreach \Z in {1,...,40}
 {\pgfmathsetmacro{\Last}{{\LstTwo}[\Z-1]}
 \pgfmathsetmacro{\Current}{{\LstTwo}[\Z]}
 \pgfmathsetmacro{\Next}{{\LstTwo}[\Z+1]}
 %\typeout{\Last,\Current,\Next}
  \edef\temp{\noexpand\path ($0.6*\Current+0.2*\Last+0.2*\Next$)   coordinate 
  (p\Z);}
  \temp
  \ifnum\Z=1
  \xdef\LstThree{(p\Z)}
  \else
  \xdef\LstThree{\LstThree (p\Z)}
  \fi
  }
 \foreach \Z in {1,...,42}
 {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
 \fill \Coor circle[radius=1pt];
 }
 \draw[thick,blue] plot[smooth] coordinates \LstThree;
% \node[red] (lowlow) at (2.2,2.5) {Low bias, low variance}; % <-- here
  \node[red, below= -2.75cm of over] (lowlow)  {Low bias, low variance}; % <-- here
 \end{scope}
 %
 \begin{scope}[local bounding box=good,xshift=-10cm]
 \foreach \Z in {1,...,42}
 {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
 \fill \Coor circle[radius=1pt];
 }
 \draw[thick,blue] plot[smooth,domain=0.1:4.2,variable=\x] (\x,{f(\x)+0.45});
%  \node[red] (highb) at (2.2,2.5) {High bias}; % <-- here
  \node[red, below= -2.75cm of good] (highb)  {High bias}; % <-- here
 \end{scope}
 %
 \begin{scope}[local bounding box=under,xshift=-15cm]
 \foreach \Z in {1,...,42}
 {\pgfmathsetmacro{\Coor}{{\LstTwo}[\Z-1]}
 \fill \Coor circle[radius=1pt];
 }
 \draw[thick,blue] (0.1,0.4) -- (4.2,2);
%  \node[red] (highv) at (2.2,2.5) {High variance}; % <-- here
  \node[red, below= -2.75cm of under] (highv)  {High variance}; % <-- here
 \end{scope}
 %
 \foreach \X in {over,good,under}
 {\draw[gray,thin] ([xshift=-3pt,yshift=3pt]\X.north west) rectangle 
 ([xshift=3pt,yshift=-3pt]\X.south east);
 \draw[stealth-stealth,thick] ([xshift=-3pt,yshift=3pt]\X.north west) node[right=1.5pt,fill=white]{Values} 
 |- ([xshift=3pt,yshift=-3pt]\X.south east) node[below left]{Time};}
 \node[green,below = 3cm of lowlow] {Good balance}; % <-- here
 \node[green,below = 3cm of highb]  {underfitting}; % <-- here
 \node[green,below = 3cm of highv]  {overfitting}; % <-- here
\end{tikzpicture}
\end{document}

관련 정보