![Achsenpunkte in TikZ identifizieren](https://rvso.com/image/305873/Achsenpunkte%20in%20TikZ%20identifizieren.png)
Ich habe den folgenden Code geschrieben, der im Wesentlichen eine Binomialverteilung als Funktion ihrer Parameter darstellt.
\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}
wodurch die folgende Abbildung entsteht.
Wie Sie sehen, liegt der Peak bei 0,2 und der Wert der Funktion an diesem Punkt liegt etwas über 0,13. Die rot gestrichelte Linie bei 0,0699 stellt lediglich die Hälfte dieser Höhe dar.
Meine Frage ist nun, ob ich die beiden Punkte auf der x-Achse identifizieren kann, die ich als Theta bezeichnet habe, wo diese gestrichelte Linie die Funktion schneidet. Es ist eine ziemlich schwierige Berechnung, wenn man sie manuell durchführt, und ich hatte gehofft, ich könnte sie grafisch darstellen.
Antwort1
Etwas wie das?
Der 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}
intersections
Die Idee ist, die Bibliothek zu verwenden und name path
die Pfade (also...) zu benennen; dann kann man TikZ die Schnittpunkte berechnen lassen; mithilfe von name intersections
kann man ihnen Namen für weitere Aktionen zuweisen.
Um die Koordinaten der Schnittpunkte zu erhalten, können Sie anwendenJake's answer
ZuKoordinaten der Kreuzungen:
\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}