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.

JohnK
  • 354

1 Answers1

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

Gonzalo Medina
  • 505,128