識別 TikZ 中的軸點

識別 TikZ 中的軸點

我編寫了以下程式碼,它基本上將二項式分佈繪製為其參數的函數。

\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}

產生下圖。

輸出

如您所看到的,峰值位於 0.2 處,此時函數的值略高於 0.13。 0.0699 處的紅色虛線僅代表該高度的一半。

我現在的問題是,我是否可以識別 x 軸上的那兩個點(我將其標記為 theta),即虛線與函數相交的位置。如果手動執行,這是一個相當困難的計算,我希望我能以圖形方式看到它。

答案1

像這樣的東西嗎?

在此輸入影像描述

代碼:

\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庫並name path(嗯......)命名路徑;然後你可以讓 TikZ 計算交點;使用name intersections您可以為它們指定名稱以進行進一步的操作。

要取得交點的座標,您可以應用Jake's answer交點座標

\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}

在此輸入影像描述

相關內容