I would like to plot the "u-shaped" or "bathtub" probability distribution using TikZ-PGF.
Using the u-quadratic's probability density function (pdf), I got the following results:
\documentclass[addpoints]{exam}
\usepackage{pgf,tikz}
\usepackage{pgfplots}
\usepgflibrary{shapes.geometric}
\usetikzlibrary{automata,arrows,positioning,calc,decorations.pathreplacing}
\usetikzlibrary{shapes}
%For plotting uniform distributions, see https://tex.stackexchange.com/questions/117954/probability-density-function-of-uniform-distribution
\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
%For plotting uniform distribution, see https://tex.stackexchange.com/questions/117954/probability-density-function-of-uniform-distribution
\pgfmathdeclarefunction{uniform}{3}{%
\pgfmathparse{(#1>=#2)*(#1<#3)*1/(#3-#2)}%
}
%For plotting normal distribution, see https://tex.stackexchange.com/questions/43610/plotting-bell-shaped-curve-in-tikz-pgf
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
%For plotting u-shaped distribution, I wrote this myself!
\pgfmathdeclarefunction{bathtub}{2}{%
\pgfmathparse{12/(#2-#1)^3*(x-(#2+#1)/2)^2}%
}
\title{MWE}
\begin{document}
Uniform distribution:
\begin{tikzpicture}
\begin{axis}[height=6cm,
axis lines=left,
ticks=none,
samples=11,
jump mark left,
ymin=0,ymax=1,
xmin=0, xmax=6,
every axis plot/.style={very thick},
discontinuous]
\addplot{uniform(x,2,4)};
\end{axis}
\end{tikzpicture}
U-shaped distribution:
\begin{tikzpicture}
\begin{axis}[height=6cm,
axis lines=left,
ticks=none,
samples=100,
smooth,
ymin=0,ymax=2,
xmin=0, xmax=6]
\addplot[domain=2:5]{bathtub(2,5)};
\end{axis}
\end{tikzpicture}
\end{document}
The code compiles fine without any compilation errors, although when pasted in Overleaf, several of the lines of code are highlighted red for some reason.
I would like the style of the bottom graph to match the style of the top graph. In particular, I would like to have two closed circles at the peaks, two open circles at the base, vertical dotted lines connecting the circles at the discontinuities, and have the function be drawn at "zero" everywhere else, like the uniform example.
Specifically, I would like to adapt this answer to my situation, but I'm afraid I don't understand how the author's code works, so I don't know how to modify it.
Thank you in advance for your help!
domain=2:5
.axis
options, or use+[domain=2:5]
. With\addplot [<stuff>] ...
the default style is replaced by<stuff>
. With\addplot +[<stuff>] ...
,<stuff>
is appended to the default style.