2

Surely it is an easy problem but I can't find a solution. Imagine you want to simulate a Gaussian distribution of frequencies around a mean value with a certain spread. It is an easy task to write the following code

\documentclass[tikz,border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}[scale=1] 
\pgfmathsetseed{431}%
\pgfmathsetmacro{\mu}{0}
\pgfmathsetmacro{\sigma}{1}

\fill[red,opacity=.5,domain=-3:3,variable=\x,smooth] plot ({\x},{(10/sqrt(2*pi)*\sigma)*exp(-((\x-\mu)^2)/(2*\sigma^2))});

\foreach \k in {-3,-2.9,...,3}
{
\pgfmathsetmacro{\y}{(10/sqrt(2*pi)*\sigma)*exp(-((\k-     \mu)^2)/(2*\sigma^2))}
\draw[xshift=-1.5pt](\k,0)rectangle(\k+1/10,\y+rand/10);
}
\end{tikzpicture}
\end{document}

But if instead one want to draw the histogram with a continue line following only the top border, what would be the code?

1
  • Could you please explain your question better, perhaps by adding a sketch of what you want to achieve? – user121799 Nov 16 '18 at 13:45
2

If I understood your question correctly, you can do this with a "const plot" in pgfplots. (It's not really a histogram here; it's an analytical step function.)

\documentclass[tikz,border=5pt]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
  \pgfmathsetmacro{\mu}{0}
  \pgfmathsetmacro{\sigma}{1}
  \begin{axis}
    \addplot[samples=30,domain=-3:3,const plot] {1/(2*\sigma*sqrt(pi))*exp(-(x-\mu)^2/(2*\sigma^2))};
  \end{axis}
\end{tikzpicture}
\end{document}

which gives the result

step histogram

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.