\documentclass{standalone}

\usepackage{pgfplots}               % TikZ (TeX ist kein Zeichenprogramm)
\usetikzlibrary{calc, perspective, pgfplots.colormaps}  % PGF-Plot / TikZ config

\pgfplotsset{
    compat = newest,
    colormap = {grayscale}{color=(lightgray) color=(white) color=(lightgray)},
    colormap = {blackscale}{color=(black!70) color=(black!50) color=(black!70)},
}

% Define the (component) embedding into the torus
\tikzset{declare function = {  % Note: NO spaces in function argument list!
    Z(\u,\v) = 0.4 * \u * \u * cos(\v * 120);
    bx(\t) = -0.5 + 0.3 * cos(\t) + 0.05 * sin(3 * \t);
    by(\t) = 0.2 + 0.3 * sin(\t);
}}

% Further packages and macros
\usepackage{amssymb, bm}

\renewcommand{\t}[1]{{#1}^{T}}
\newcommand{\mat}[1]{\boldsymbol{#1}}
\newcommand{\manifold}[1]{\mathfrak{#1}}



\begin{document}
\begin{tikzpicture}[>=latex]

    \begin{axis}[
        axis equal image,
        hide axis,
        view = {120}{30},
        scale = 1,
        clip = false
    ]
        \coordinate (O) at (0, 0, 0);

        \draw[->] (-0.1, 0, 0) -- (0.6, 0, 0) node[pos = 1.1] {};
        \draw[->] (0, -0.1, 0) -- (0, 1.2, 0) node[pos = 1.1] {};
        \draw[->] (0, 0, -0.1) -- (0, 0, 1.0) node[pos = 1.1] {};

        \addplot3[
            surf,
            shader = faceted interp,
            samples = 16,
            samples y = 16,
            domain = -1:0.4,
            domain y = -0.5:1,
            z buffer = sort,
            colormap name = grayscale,
            thin
        ]
            ({\x}, {\y}, {Z(\x, \y)});

        \addplot3[
            samples = 64,
            samples y = 0,
            domain = 0:360,
            color = black!40!gray,
            fill = black,
            fill opacity = 0.1,
            colormap name = blackscale,
            thick
        ]
            ({bx(\x)}, {by(\x)}, {Z(bx(\x), by(\x))});

        \coordinate (coordU) at ({bx(150)}, {by(150)}, {Z(bx(150), by(150))});

        \node[anchor = south west] (U) at (coordU) {$U$};

        \node[
            circle, fill=black, inner sep=0.75pt, label={$\mat{\theta}_0$}
        ] (theta0) at (-0.5, 0.2, {Z(-0.5, 0.2)}) {};

        \node at (0, 0.5, 1.2) {$\Theta\subseteq\mathbb{R}^p$};

        \node (UU) at ({bx(0)}, {by(0)}, {Z(bx(0), by(0))}) {};

    \end{axis}
    
    \begin{scope}[shift = {(11cm, 2cm)}, scale = 2.5]

        \coordinate (O) at (-1.1, -0.3);

        \draw[step=0.1, lightgray!80, thin] (O) grid +(1.2, 1.2);

        \draw[->] ($(O) - (0.05, 0)$) -- +(1.4, 0) node[pos=1.1] {};
        \draw[->] ($(O) - (0, 0.05)$) -- +(0, 1.4) node[pos=1.1] {};

        \draw[domain=0:360, smooth, variable=\x, fill=black, fill opacity = 0.1, thick] plot ({bx(\x)}, {by(\x)});

        \node[
            circle, fill=black, inner sep=0.75pt, label={$\mat{s}_0$}
        ] (s0) at (-0.5, 0.2) {};

        \coordinate (coordPhiU) at ({bx(90)}, {by(90)});

        \node[anchor = south east, outer sep = 0pt] (phiU) at (coordPhiU) {$\varphi(U)$};

        \node at (-0.5, 1.28) {$\varphi(U)\subseteq\mathbb{R}^d$};

        \node (phiUU) at ({bx(270)}, {by(270)}) {};

    \end{scope}


    \draw[->, out = 20, in = 160] (U.north east) to node[above, pos = 0.5] {$\varphi$} (phiU.north west);
    \draw[->, out = 200, in = 340] (phiU.south west) to node[above, pos = 0.5] {$\varphi^{-1}$} (U.south east);

    \node (R) at (6.1, 0) {$\mathbb{R}$};

    \draw[->, out = 270, in = 180] (UU) to node[below left, pos = 0.6] {$M$} (R);
    \draw[->, out = 270, in = 0] (phiUU) to node[below right, pos = 0.4] {$M_{\varphi}$} (R);

\end{tikzpicture}
\end{document}
