# Plotting over/underfitting graph with TikZ package [closed]

I am trying to find a way of illustrating supervised learning good/over/under-fitting, like:

## closed as off-topic by Andrew Swann, user156344, Stefan Pinnow, Raaja, Phelype OleinikMay 16 at 10:21

• This question does not fall within the scope of TeX, LaTeX or related typesetting systems as defined in the help center.
If this question can be reworded to fit the rules in the help center, please edit the question.

• Please show us what you have tried so far. And, welcome to TeX.Se – Raaja Apr 21 at 13:30
• this is unrelated to texstudio (that is just the editor you are using to write the tex file, the tex output would be the same whichever editor you used) – David Carlisle Apr 21 at 13:58
• I'm voting to close this question as off-topic because this is a just-do-it-for-me question – user156344 May 16 at 7:32

Welcome to TeX-SE! The purpose of this site is not to convert screen shots into LaTeX code, but to seek for advice when you got stuck with your own attempts. This is to give you a start, and to encourage you to try on your own.

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