3

My first question, I have only recently started using TexWorks. I'm writing notes in LaTeX to practice for writing my Bachelor thesis. So far, I have been able to learn how to recreate figures from my textbooks by reading tutorials on Stack Exchange.

But this has me stumped. Am I on the right track here?

How do I get the standard normal distribution curve to look like it does in my book?

\documentclass[a4paper, 12pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, backgrounds, positioning, matrix, calc, chains, fit}
\usepackage{pgfplots}  
\pgfmathdeclarefunction{norm}{3}{%
  \pgfmathparse{sqrt(0.5*#3/pi)*exp(-0.5*#3*(#1-#2)^2)}%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width=1*\textwidth,
    xlabel = {Residualer ($e_i$)},
    ylabel = {Antall enheter},
    ymin=0, ymax=10,
    xmin=0, xmax=70,
    minor y tick num = 3,
    area style,
    ]
\addplot+[ybar interval,mark=no] plot coordinates { (0, 5) (10, 9) (20, 7) (30, 3) (40, 1) (50, 2) (60, 1) (70, 1)};
\addplot [dashed, very thick, draw=red,  domain=0:70, samples=2000, smooth]
  (x, {norm(x, 35, 0.25)});
\end{axis}
\end{tikzpicture}
\end{document}

Figures: Histograms with normal distribution curves.

Any help is welcome, thanks in advance.

1

1 Answer 1

5

The curve seems to be based on the values of the lower diagram. You can use these to calculate mean (33.9655) and std (10.25). Then calculate a factor (231.2364) to scale the normal distribution on the y-axis.

I changed the definition of norm to be equal to the normal distribution (as defined here).

I reduced the number of samples to be faster (samples=200).

Code

\documentclass[a4paper, 12pt]{standalone}
\usepackage{tikz}
\usetikzlibrary{shapes.geometric, arrows, backgrounds, positioning, matrix, calc, chains, fit}
\usepackage{pgfplots}  
\pgfmathdeclarefunction{norm}{3}{%
    % #1=x, #2=mu, #3=sigma
    \pgfmathparse{exp(-.5 * ((#1-#2) / #3)^2) / (#3 * sqrt(2*pi))}%
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
    width=1*\textwidth,
    xlabel = {Residualer ($e_i$)},
    ylabel = {Antall enheter},
    ymin=0, ymax=10,
    xmin=0, xmax=70,
    minor y tick num = 3,
    area style,
    ]
\addplot+[ybar interval,mark=no] plot coordinates { (0, 5) (10, 9) (20, 7) (30, 3) (40, 1) (50, 2) (60, 1) (70, 1)};
\addplot [dashed, very thick, draw=red,  domain=0:70, samples=200, smooth]
  (x, {231.2364*norm(x, 33.9655, 10.25)});
\end{axis}
\end{tikzpicture}
\end{document}

Result

enter image description here

1
  • 2
    Thank you so much, this is exactly what I needed both to get the result I wanted, and I can also compare what I did with the solution to get a better understanding.
    – Emil
    Commented Jul 7, 2023 at 15:26

You must log in to answer this question.

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