0

I'm trying to generate a diagram like this with Tikz:

enter image description here

I just ability create such an image, but it is not desirable. Thank you if you can help me.

enter image description here

\documentclass[border=3pt,tikz]{standalone} %[dvipsnames]
\usepackage{amsmath} % for \dfrac
\usepackage{tikz}
\tikzset{>=latex} % for LaTeX arrow head
\usepackage{pgfplots} % for the axis environment
\usepackage{xcolor}
\usepackage[outline]{contour} % halo around text
\contourlength{1.2pt}
\usetikzlibrary{positioning,calc}
\usetikzlibrary{backgrounds}% required for 'inner frame sep'


\pgfmathdeclarefunction{gauss}{3}{%
    \pgfmathparse{1/(#3*sqrt(2*pi))*exp(-((#1-#2)^2)/(2*#3^2))}%
    }



% to fill an area under function
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.12} % TikZ coordinates <-> axes coordinates


\begin{document}
        \begin{tikzpicture}
            \message{Normal distributions, different mu^^J}
        
        \def\q{5};
        \def\B{3};
        \def\S{7};
        \def\Bs{1.0};
        \def\Ss{1.0};
        \def\xmax{\S+3.2*\Ss};
        \def\ymin{{-0.15*gauss(\B,\B,\Bs)}};
    
        \begin{axis}[every axis plot post/.append style={
            mark=none,domain={-0.05*(\xmax)}:{1.08*\xmax},samples=50,smooth},
            xmin={-0.1*(\xmax)}, xmax=\xmax,
            ymin=\ymin, ymax={1.1*gauss(\B,\B,\Bs)},
            axis lines=middle,
            axis line style=thick,
            enlargelimits=upper, % extend the axes a bit to the right and top
            ticks=none,
            ylabel=Probability density,
            xlabel=value,
            x label/.style={at={(current axis.right of origin)},anchor=north west},
            width=0.7*\textwidth, height=0.5*\textwidth,
            clip=false, % prevent labels falling off
            y=200pt
            ]
        
            % PLOTS
            \addplot[red,  name path=S,thick] {gauss(x,\S,\Ss)};
        
            % LABELS
            \node[above=2pt,black!20!red]  at (1.05*\S,{gauss(\S,\S,\Ss)}) {$\mathcal{N}(\mu,\sigma)$};
        \end{axis}
    \end{tikzpicture}
\end{document}
2
  • What is exactly not desirable for you in the picture?
    – antshar
    Aug 18, 2022 at 8:40
  • - Direction and position of xlabel and label and - And I want lines and directions to be added to the diagram
    – Moein
    Aug 18, 2022 at 8:44

1 Answer 1

2

Your attempt with x label/.style is close, but there is a keyvalue x label style= for that instead of explicit tikz style. Also, you can make use of left, right, above, below predefined anchors to align the text.

To rotate a ylabel, you simply can add rotate=90 to its style.

Adding extra lines is also straight forward.

\documentclass[border=3pt,tikz]{standalone} %[dvipsnames]
\usepackage{amsmath} % for \dfrac
\usepackage{tikz}
\tikzset{>=latex} % for LaTeX arrow head
\usepackage{pgfplots} % for the axis environment
\usepackage{xcolor}
\usepackage[outline]{contour} % halo around text
\contourlength{1.2pt}
\usetikzlibrary{positioning,calc}
\usetikzlibrary{backgrounds}% required for 'inner frame sep'


\pgfmathdeclarefunction{gauss}{3}{%
    \pgfmathparse{1/(#3*sqrt(2*pi))*exp(-((#1-#2)^2)/(2*#3^2))}%
    }



% to fill an area under function
\usepgfplotslibrary{fillbetween}
\usetikzlibrary{patterns}
\pgfplotsset{compat=1.12} % TikZ coordinates <-> axes coordinates


\begin{document}
        \begin{tikzpicture}
            \message{Normal distributions, different mu^^J}
        
        \def\q{5};
        \def\B{3};
        \def\S{7};
        \def\Bs{1.0};
        \def\Ss{1.0};
        \def\xmax{\S+3.2*\Ss};
        \def\ymin{{-0.15*gauss(\B,\B,\Bs)}};
    
        \begin{axis}[every axis plot post/.append style={
            mark=none,domain={-0.05*(\xmax)}:{1.08*\xmax},samples=50,smooth},
            xmin={-0.1*(\xmax)}, xmax=\xmax,
            ymin=\ymin, ymax={1.1*gauss(\B,\B,\Bs)},
            axis lines=middle,
            axis line style=thick,
            enlargelimits=upper, % extend the axes a bit to the right and top
            ticks=none,
            ylabel=Probability density,
            xlabel=value,
            x label style={below},
            y label style={above left, rotate=90},
            width=0.7*\textwidth, height=0.5*\textwidth,
            clip=false, % prevent labels falling off
            y=200pt
            ]
        
            % PLOTS
            \addplot[red,  name path=S,thick] {gauss(x,\S,\Ss)};
        
            % LABELS
            \node[above=2pt,black!20!red]  at (1.05*\S,{gauss(\S,\S,\Ss)}) {$\mathcal{N}(\mu,\sigma)$};
            
            % EXTRA LINES
            \draw[<->, thick] (\S-3*\Ss, -0.02) -- node[below] {Precision} (\S+3*\Ss, -0.02);
            
            \draw[thick, dash dot] (\S, 0) -- (\S, 0.4);
            \draw[thick, green] (\B, 0) -- (\B, 0.4);
            
            \draw[<->, thick] (\B, 0.3) -- node[above] {Accuracy} (\S, 0.3);
        \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

4
  • Thank you very much for your good answer Is it possible to create all four diagrams?
    – Moein
    Aug 18, 2022 at 9:34
  • @Moein it's only matter of copy-pasting the code. You might take a look at related question about arranging.
    – antshar
    Aug 18, 2022 at 9:55
  • Thank you @antshar, my problem is not placement (arranging) and I can use subfigure for this work. Actually, my problem is creating a Gaussian function with the mean and standard deviation in the first figure...
    – Moein
    Aug 18, 2022 at 10:15
  • @Moein actually, Gaussian pdf never looks like on the first graph of your screenshot, despite on the parameters. It's either incorrect or a different distribution.
    – antshar
    Aug 18, 2022 at 10:36

You must log in to answer this question.

Not the answer you're looking for? Browse other questions tagged .