1

I am having trouble drawing the following probability distribution:

enter image description here

I am not very sure about how I can do this. I've looked at sample plot functions using PGF Plots and Tikz, but I haven't been able to find very many posts regarding discrete probability distributions. I was thinking about maybe plotting the lines x = -2, -1, 0, 1, 2, and then somehow constraining the graph to my desired interval, but I didn't get anywhere with this approach either.

EDIT: Here is something that I found online that I've been working with:

\documentclass{article}
\usepackage{amsmath}
\usepackage{amssymb}
\usepackage{tikz}
\usepackage{pgfplots}

\newcommand{\prob}[1]{\ensuremath{\text{P}\left(#1\right)}}

\begin{document}

    \pgfplotsset{
        standard/.style={
            axis x line=middle,
            axis y line=middle,
            enlarge x limits=0.15,
            enlarge y limits=0.15,
            every axis x label/.style={at={(1,0.1)},anchor=north west},
            every axis y label/.style={at={(0.1,1.1)},anchor=north},
            every axis plot post/.style={mark options={fill=black}}
        }
    }

    \begin{center}
        \begin{tikzpicture}
        \begin{axis}[
        standard,
        domain = -4:4,
        samples = 9,
        xlabel={$x$},
        xmin=-2,
        xtick= {-2, -1, 0, 1, 2},
        xmax= 2,
        ylabel={$\prob{Y=x}$},
        ymin=0,
        yticklabel={$\frac{\pgfmathprintnumber{\tick}}{16}$},
        ytick = {1, 2, 3, 4, 5, 6},
        ymax=6]
        \addplot+[ycomb,black,thick] {(abs(x))};
        \end{axis}
        \end{tikzpicture}
    \end{center}

\end{document}
4
  • 2
    By “probability distribution function”, I think you meant “probability mass function” in the discrete case. Have a look at this answer. Commented Oct 4, 2018 at 13:42
  • Yes, I meant probability mass function. I will take a look. Commented Oct 4, 2018 at 13:56
  • Having a vertical line only is not a problem as you already have found out. Define your function using /pgf/declare function and then we will look at the remaining problem. Commented Oct 4, 2018 at 19:45
  • @Ekesh I've turned your code into a proper minimal working example (MWE) which others can compile and defined a new command \prob which you can check out. Having a proper MWE is vital for easing other users' attempts to help.
    – Thev
    Commented Oct 4, 2018 at 20:18

1 Answer 1

1

You can do this without pgfplots, for example by using pic elements for the bars of the bar graph.

\documentclass[tikz,border=7pt]{standalone}
\tikzset{
  % define the bar graph element
  bar/.pic={
    \fill (-.1,0) rectangle (.1,#1) (0,#1) node[above,scale=1/2]{$#1$};
  }
}
\begin{document}
  \begin{tikzpicture}[y=4cm]
    \draw
      % the main axis
      (-3,0) edge[-latex] (3,0)
      % draw the distribution and label it
      foreach[count=\i from -2] ~ in {1/16,4/16,6/16,4/16,1/16}{
        (\i,0) pic[red]{bar=~} node[below]{$\i$}
      };
  \end{tikzpicture}
\end{document}

enter image description here

Or you can also use plot coordinates with ycomb :

\documentclass[tikz,border=7pt]{standalone}
\begin{document}
  \begin{tikzpicture}[y=4cm]
    \draw[ycomb,color=red,line width=2mm]
      plot coordinates{(-2,1/16)(-1,4/16)(0,6/16)(1,4/16)(2,1/16)};
    \draw (-3,0) edge[-latex] (3,0);
  \end{tikzpicture}
\end{document}

enter image description here

You must log in to answer this question.

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