2

Im trying to plot a probability function in tikz, but the line plotted isn't even close to how the function should look. Im new to using latex and I have no idea why this isnt working, unless tikz just cant handle this function. Here is my code:

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

\begin{document}

\begin{tikzpicture}[
    declare function={Probability(\n,\k) = 1-((\n!)/((\n^(\k))*((\n-
\k)!)));}
]

\begin{axis}[
  no markers, domain=0:15, samples=2500,
  axis lines=left, xlabel=$k$, ylabel=$y$,
  every axis y label/.style={at=(current axis.above origin),anchor=east},
  every axis x label/.style={at=(current axis.right of origin),anchor=north},
  height=10cm, width=15cm,
  xtick={0,2,4,6,8,10,12,14},
  ytick={0.1,0.2,0.3,0.4,0.5,.6,.7,.8,.9,1},
  enlargelimits=false, clip=false, axis on top,
  grid = major,
  xmax=15
  ]

\addplot [very thin,cyan!20!black] {Probability(10,x)};


\end{axis}
\end{tikzpicture}
\end{document}

This is what the function should look like, wolfram alpha plot And this is what tikz is plotting latex plot

I would appreciate it if someone could point me in the right direction. Thanks

1

enter image description here

answer addresses only pgfplots aspect. for drawing (vertical) lines you need to add option ycomb:

\documentclass{article}

\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}
\begin{tikzpicture}[
declare function={p(\n,\k) = 1-((\n!)/((\n^(\k))*((\n-\k)!)));}
                    ]
\begin{axis}[axis lines=middle, 
            xlabel=$k$, ylabel=$y$, 
            ylabel style={anchor=south},
            xlabel style={anchor=west},
            ymax=1.2, xmax=15.5,
            domain=0:15, samples=150,
            no markers
                    ]
\addplot+ [ycomb] {p(2,x)}; % <----
\end{axis}
\end{tikzpicture}

\end{document}

addendum: image with correct settings of your probability function (variables should be integers) is:

\begin{tikzpicture}[
declare function={p(\n,\k) = 1-((\n!)/((\n^(\k))*((\n-\k)!)));}
                    ]
\begin{axis}[axis lines=middle,
            xlabel=$k$, ylabel=$y$,
            ylabel style={anchor=south},
            xlabel style={anchor=west},
            ymax=1.1, xmax=32,      % <-------
            domain=0:30, samples=31,% <-------
%            no markers
                    ]
\addplot+ [ycomb] {p(2,x)};         % <-------
\end{axis}
\end{tikzpicture}

enter image description here

  • That's perfect thankyou! Do you know why my code was plotting such a strange curve? – John 545 Nov 18 '17 at 0:41
  • regarding pgfplots you not use ycomb macro, regarding function: you use far to mach samples and selected function parameters lead to"strange" image. i didn't check definition of your function, however negative probabilities is unlike. – Zarko Nov 18 '17 at 0:49
  • As Its a probability function of a discrete variable, how would I get it to only plot values at the integers? – John 545 Nov 18 '17 at 0:52
  • hm, that is math problem ... one way can be increase function domain (for example domain=0:120) and select sample number equal to domain (samples=120) – Zarko Nov 18 '17 at 0:57
  • off-topic, but how can a probability function become negative? – user121799 Nov 18 '17 at 3:15

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.