0

I use this code:

\documentclass[12pt,a4paper,oneside]{book}

\usepackage{tikz}
\usetikzlibrary{positioning, decorations.text, calc}
\usepackage{lipsum}

\begin{document}
\lipsum[1]

\begin{figure}[!ht]
\centering
 \tikzset{declare function={f(\x)=(-0.06*(\x-2)+0.5)*(\x-2)*(\x-2);}}% applied math style
 \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{minipage}[b]{0.32\textwidth}\centering
\begin{tikzpicture}
 \begin{scope}[local bounding box=over]
 \foreach \Z in {1,...,40}
 {\pgfmathsetmacro{\Last}{{\LstTwo}[\Z-1]}
 \pgfmathsetmacro{\Current}{{\LstTwo}[\Z]}
 \pgfmathsetmacro{\Next}{{\LstTwo}[\Z+1]}
  \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}
 \draw[style=help lines,step=0.5cm] ([xshift=-3pt,yshift=-3pt]over.south west) grid ([xshift=3pt,yshift=3pt]over.north east);
 \draw[gray,thin] ([xshift=-3pt,yshift=3pt]over.north west) rectangle  ([xshift=3pt,yshift=-3pt]over.south east);
 \draw[stealth-stealth,thick] ([xshift=-3pt,yshift=3pt]over.north west) |- ([xshift=3pt,yshift=-3pt]over.south east);
\end{tikzpicture}
\caption{Overfitting}\label{fig:overfitting}
\end{minipage}\hfil
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{minipage}[b]{0.32\textwidth}\centering
\begin{tikzpicture}
 \begin{scope}[local bounding box=under]
 \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}
 \draw[style=help lines,step=0.5cm] ([xshift=-3pt,yshift=-3pt]under.south west) grid ([xshift=3pt,yshift=3pt]under.north east);
 \draw[gray,thin] ([xshift=-3pt,yshift=3pt]under.north west) rectangle  ([xshift=3pt,yshift=-3pt]under.south east);
 \draw[stealth-stealth,thick] ([xshift=-3pt,yshift=3pt]under.north west) |- ([xshift=3pt,yshift=-3pt]under.south east);
\end{tikzpicture}
\caption{Underfitting}\label{fig:underfitting}
\end{minipage}\hfil
%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
\begin{minipage}[b]{0.32\textwidth}\centering
\begin{tikzpicture}
 \begin{scope}[local bounding box=good]
 \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}
 \draw[style=help lines,step=0.5cm] ([xshift=-3pt,yshift=-3pt]good.south west) grid ([xshift=3pt,yshift=3pt]good.north east);
 \draw[gray,thin] ([xshift=-3pt,yshift=3pt]good.north west) rectangle  ([xshift=3pt,yshift=-3pt]good.south east);
 \draw[stealth-stealth,thick] ([xshift=-3pt,yshift=3pt]good.north west) |- ([xshift=3pt,yshift=-3pt]good.south east);
\end{tikzpicture}
\caption{Balance}\label{fig:balance}
\end{minipage}
\end{figure}

\lipsum[1]
\end{document}

This outputs the following plots:

enter image description here

The point of origin for the help lines is inside the plot. The left most column and the bottom most row are therefore not full which they should be. Is there a way to set the point of origin of the help lines to align with the bottom left corner of the plot?

2
  • I think that users may hesitate to answer this question because they may feel that you copied the code for the graphs. Why? Because it is all too easy to find this post. Maybe you could indicate the source of your inspiration, BTW, you can safely ignore the "answer" you got, the behavior of this user is one of the reasons why the author of the more original post. quit.
    – user229669
    Dec 2 '20 at 7:23
  • Yes, I used most of the code you linked to generate the plots. However I don't know why that changes anything about my question.
    – philipp_th
    Dec 2 '20 at 9:12
0

Why you not use pgfplots which is dedicated for such sort of diagrams? Using it align of grids is simple to control:

\documentclass[12pt,a4paper,oneside]{book}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usetikzlibrary{calc,
                decorations.text, 
                positioning}
\usepackage[skip=1ex, 
            font=footnotesize, labelfont=bf]{caption}
\usepackage{lipsum}

\begin{document}
\lipsum[1]

    \begin{figure}[!ht]
\centering
\pgfplotsset{width=\linewidth, % style of diagrams
    axis lines=center,
    scale only axis,
    grid,
    axis line style={->, semithick},
    tick pos=upper,
    xmin=0, ymax=40,
    ymin=0, ymax=6,
    xticklabels={}, yticklabels={}
            }

\begin{minipage}[b]{0.32\textwidth}
    \centering
    \begin{tikzpicture}
\begin{axis}[
    declare function = {f(\t)=(-0.06*(\t-2)+0.5)*(\t-2)*(\t-2);},
restrict y to domain = 0:6
            ]
\addplot [scatter, only marks, domain=0:40] {5*rnd};
\addplot [red, thick, domain=0:40] {f(x)};
\end{axis}
    \end{tikzpicture}
\caption{Overfiting}
    \label{fig:balance}
\end{minipage}%
\hfill
\begin{minipage}[b]{0.32\textwidth}
    \centering
    \begin{tikzpicture}
\begin{axis}[
    declare function = {f(\t)=0.1*(\t + 2) +1;},
            ]
\addplot [scatter, only marks, domain=0:40] {5*rnd};
\addplot [red, thick, domain=0:40] {f(x)};
\end{axis}
    \end{tikzpicture}
\caption{Overfitting}
    \label{fig:balance}
\end{minipage}%
\hfill
\begin{minipage}[b]{0.32\textwidth}
    \centering
    \begin{tikzpicture}
\begin{axis}[
    declare function = {f(\t)=0.01*(\t - 20)*(\t - 20)+1;},
            ]
\addplot [scatter, only marks, domain=0:40] {5*rnd};
\addplot [red, thick, domain=0:40] {f(x)};
\end{axis}
    \end{tikzpicture}
\caption{Balance}
    \label{fig:balance}
\end{minipage}

\end{figure}

\lipsum[1]
\end{document}

Sorry, I didn't succeed to figure out your functions, so in diagrams are used some dummy ones, which gives the following result.

enter image description here

0
 \draw[style=help lines,step=0.5cm,shift={($(over.south west) - (3pt, 3pt)$)}] ([xshift=-3pt,yshift=-3pt]over.south west) grid ([xshift=3pt,yshift=3pt]over.north east);

Adding shift coordinates which are dependent on the dynamic over point and subtracting 3pt did the trick.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.