Probability density function of Uniform Distribution

Can anyone help me about drawing pdf of uniform distribution function in Latex? I am newbie in this and could not find anything helpful. Thanks!

• Jun 6, 2013 at 16:08
• These functions are linear. Or am I mistaken? So use the graphic environment of you choice (most us pstricks or tikz/pgf) and draw the respective linear functions. Jun 6, 2013 at 16:33
• I am sure that tkz-euclide does a nice job for this purpose. Jun 6, 2013 at 16:46

2 Answers

If anyone has interest, here is another solution with symbolic coordinates.

\documentclass[10pt]{standalone}
\usepackage{mathtools}

\usepackage{tkz-euclide}

% arrow and line for 'tkzPointShowCoord'
\makeatletter
\tikzset{arrow coord style/.style={%
densely dashed,
\tkz@euc@linecolor,
%>=stealth',
%->,
}}
\tikzset{xcoord style/.style={%
\tkz@euc@labelcolor,
font=\normalsize,text height=1ex,
inner sep = 0pt,
outer sep = 0pt,
fill=\tkz@fillcolor,
below=6pt
}}
\tikzset{ycoord style/.style={%
\tkz@euc@labelcolor,
font=\normalsize,text height=1ex,
inner sep = 0pt,
outer sep = 0pt,
fill=\tkz@fillcolor,
left=6pt
}}
\makeatother

\begin{document}

\begin{tikzpicture}
\tkzInit[xmax=1,xstep=0.2,ymax=1,ystep=0.2]
\tkzDrawX[noticks,label={$x$}]
\tkzDrawY[noticks,label={$f_X(x)$}]
\tkzDefPoint(0.2,0.8){A}
\tkzDefPoint(0.8,0.8){B}
\tkzDefShiftPoint[A](-90:4){AB}
\tkzDefShiftPoint[B](-90:4){BB}
\tkzDefShiftPoint[AB](0:-0.6){ABL}
\tkzDefShiftPoint[BB](0:0.6){BBR}
\tkzPointShowCoord[xlabel=$a$,ylabel=$\frac{1}{b-a}$](A)
\tkzPointShowCoord[xlabel=$b$](B)
\tkzDrawSegments[color=cyan,thick](A,B AB,ABL BB,BBR)
\tkzDrawPoints[color=cyan,fill=cyan,size=6pt](A,B)
\tkzDrawPoints[color=cyan,fill=white,size=6pt](AB,BB)
\end{tikzpicture}

\end{document}


You can use PGFPlots for this. The PDF of the uniform distribution is not implemented by default, but you can define it quite easily yourself using

declare function={unipdf(\x,\xl,\xu)= (\x>\xl)*(\x<\xu)*1/(\xu-\xl);}


which allows you to write unipdf(<variable>,<lower>,<upper>):

\documentclass{article}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture}[
declare function={unipdf(\x,\xl,\xu)= (\x>\xl)*(\x<\xu)*1/(\xu-\xl);}
]
\begin{axis}[
samples=100,
const plot mark mid,
ymin=0,ymax=1
]
\addplot [very thick, orange] {unipdf(x,0,2)};
\addplot [very thick, cyan] {unipdf(x,-3,1)};
\end{axis}
\end{tikzpicture}
\end{document}


To get a proper discontinuous function with markers for the open/closed intervals, you can use the scatter/@pre marker code functionality to decide whether to draw a marker:

\documentclass{article}
\usepackage{pgfplots}

\makeatletter
\long\def\ifnodedefined#1#2#3{%
\@ifundefined{pgf@sh@ns@#1}{#3}{#2}%
}

\pgfplotsset{
discontinuous/.style={
scatter,
scatter/@pre marker code/.code={
\ifnodedefined{marker}{
\pgfpointdiff{\pgfpointanchor{marker}{center}}%
{\pgfpoint{0}{0}}%
\ifdim\pgf@y>0pt
\tikzset{options/.style={mark=*}}
\draw [densely dashed] (marker-|0,0) -- (0,0);
\draw plot [mark=*,mark options={fill=white}] coordinates {(marker-|0,0)};
\else
\ifdim\pgf@y<0pt
\tikzset{options/.style={mark=*,fill=white}}
\draw [densely dashed] (marker-|0,0) -- (0,0);
\draw plot [mark=*] coordinates {(marker-|0,0)};
\else
\tikzset{options/.style={mark=none}}
\fi
\fi
}{
\tikzset{options/.style={mark=none}}
}
\coordinate (marker) at (0,0);
\begin{scope}[options]
},
scatter/@post marker code/.code={\end{scope}}
}
}

\makeatother

\begin{document}
\begin{tikzpicture}[
declare function={unipdf(\x,\xl,\xu)= (\x>=\xl)*(\x<\xu)*1/(\xu-\xl);}
]

\begin{axis}[
samples=11,
jump mark left,
ymin=0,ymax=1,
xmin=-5, xmax=5,
every axis plot/.style={very thick},
discontinuous
]
\addplot [orange] {unipdf(x,0,2)};
\end{axis}

\end{tikzpicture}
\end{document}

• Is it possible to improve your answer, to show the graphic as here: en.wikipedia.org/wiki/Uniform_distribution_%28continuous%29? Such that it is open at 0 (mark=o) and close in non-zero (mark=*). Jun 6, 2013 at 17:04
• @cacamailg: Sure. See the edited answer.
– Jake
Jun 6, 2013 at 17:49
• Very very good! If I could I would up vote it twice. Jun 6, 2013 at 17:57
• Awesome, @Jake, best answer on the Internet ! Thanks so much. This will end up in so many probability books for discrete cumulated distributions ;) Apr 10, 2018 at 20:16
• Hi Jake, I am trying to achieve something similar to your answer for the u-shaped probability distribution. I tried following your approach, so that changing the parameters automatically changes the placement of the circles and dashed lines. In case you'd like to take a look, I'd appreciate it! tex.stackexchange.com/questions/492049/… May 23, 2019 at 17:05