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

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

• Please show us what you have tried so far. And, welcome to TeX.Se Apr 21 '19 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) Apr 21 '19 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 '19 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]}