4

I have written the following code which basically plots a binomial distribution as a function of its parameter.

\begin{tikzpicture}[scale=1.5]
\begin{axis}[
domain=0:1,
axis lines=left,
grid=both,
xlabel=$\theta$,
ylabel=$L(\theta)$
]
\addplot[smooth,thick,black]
{factorial(50)/(factorial(10)*factorial(40)) *x^10 *(1-x)^40};
\addplot[smooth,dashed,red]
{0.0699};
\end{axis}
\end{tikzpicture}

which produces the following figure.

output

As you can see the peak is at 0.2, and the value of the function at this point is a little over 0.13. The red-dashed line at 0.0699 merely represents half that height.

My question now is, whether I could identify those two points in the x-axis, which I have labelled as theta, where this dashed line intersects the function. It is quite a difficult calculation if it is performed manually and I was hoping I could see it graphically.

  • Mathematica clamis that \theta \approx 0.1375322 and \theta \approx 0.2741498. Now you can draw the two line segments. – Svend Tveskæg Feb 18 '15 at 20:35
  • @SvendTveskæg Thank you. Perhaps then, there is no way to do it in Tikz? For the line segments I would have to use the draw command, yes? – JohnK Feb 18 '15 at 20:40
  • I know vertually nothing about TikZ, sorry, so I can't tell you. – Svend Tveskæg Feb 18 '15 at 20:41
  • @SvendTveskæg That's alright. Thanks for your input. – JohnK Feb 18 '15 at 20:42
  • Coordinates of intersections is related at least. – Torbjørn T. Feb 18 '15 at 20:52
5

Something like this?

enter image description here

The code:

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{intersections}

\begin{document}

\begin{tikzpicture}[scale=1.5]
\begin{axis}[
domain=0:1,
axis lines=left,
grid=both,
clip=false,
xlabel=$\theta$,
ylabel=$L(\theta)$
]
\addplot[name path=curve,smooth,thick,black]
{factorial(50)/(factorial(10)*factorial(40)) *x^10 *(1-x)^40};
\addplot[name path=line,smooth,dashed,red]
{0.0699};
\path[name intersections={of=curve and line, by={a,b}}];
\draw[dashed] 
  (a) -- (a|-{axis cs:0,0}) node[anchor=north,font=\tiny] {$\theta_1$};
\draw[dashed] 
  (b) -- (b|-{axis cs:0,0}) node[anchor=north,font=\tiny] {$\theta_2$};
\node[fill,inner sep=1.5pt] at (a) {};
\node[fill,inner sep=1.5pt] at (b) {};
\end{axis}
\end{tikzpicture}

\end{document}

The idea is to use the intersections library and name path to (well...) name the paths; then you can let TikZ calculate the intersection points; using name intersections you can assign them names for further actions.

To get the coordinates of the intersection points, you can apply Jake's answer to Coordinates of intersections:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.11}
\usetikzlibrary{intersections}

\begin{document}

\makeatletter
\newcommand\transformxdimension[1]{
    \pgfmathparse{((#1/\pgfplots@x@veclength)+\pgfplots@data@scale@trafo@SHIFT@x)/10^\pgfplots@data@scale@trafo@EXPONENT@x}
}
\newcommand\transformydimension[1]{
    \pgfmathparse{((#1/\pgfplots@y@veclength)+\pgfplots@data@scale@trafo@SHIFT@y)/10^\pgfplots@data@scale@trafo@EXPONENT@y}
}
\makeatother

\begin{tikzpicture}[scale=1.5]
\begin{axis}[
yticklabel style={/pgf/number format/.cd, fixed, fixed zerofill},
domain=0:1,
axis lines=left,
grid=both,
clip=false,
xlabel=$\theta$,
ylabel=$L(\theta)$
]
\addplot[name path global=curve,smooth,thick,black]
{factorial(50)/(factorial(10)*factorial(40)) *x^10 *(1-x)^40};
\addplot[name path global=line,smooth,dashed,red]
{0.0699};
\path[name intersections={of=curve and line, by={a,b}}];
\node[anchor=south] at (a)
  {
    \pgfgetlastxy{\macrox}{\macroy}
    \transformxdimension{\macrox}
    \pgfmathprintnumber{\pgfmathresult},%
    \transformydimension{\macroy}%
    \pgfmathprintnumber{\pgfmathresult} 
  };
\node[anchor=north west] at (b)
  {
    \pgfgetlastxy{\macrox}{\macroy}
    \transformxdimension{\macrox}
    \pgfmathprintnumber{\pgfmathresult},%
    \transformydimension{\macroy}%
    \pgfmathprintnumber{\pgfmathresult} 
  };

\draw[dashed] 
  (a) -- (a|-{axis cs:0,0}) node[anchor=north,font=\tiny] {$\theta_1$};
\draw[dashed] 
  (b) -- (b|-{axis cs:0,0}) node[anchor=north,font=\tiny] {$\theta_2$};
\node[fill,inner sep=1.5pt] at (a) {};
\node[fill,inner sep=1.5pt] at (b) {};

\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

  • Thank you but how can I identify the points in the x axis, theta1 and theta2? – JohnK Feb 18 '15 at 21:58
  • @JohnK What do you mean "identify"? Do you want to get the coordinates of those points? – Gonzalo Medina Feb 18 '15 at 21:58
  • Sorry. I meant the coordinates of these points. – JohnK Feb 18 '15 at 21:59
  • @JohnK Please see my updated answer. – Gonzalo Medina Feb 18 '15 at 22:31

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.