经过一番努力,我终于找到了一种方法来标记高斯钟形线和直线之间的交点。除了第一个点之外,我需要将两个交点投影到横坐标轴上。我的想法是这样的:提取交点的 x 坐标,例如x_1
,并在点 处创建一个节点(x_1,0)
。
这样的事能实现吗?
这是我的 MWE
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\newcommand*{\ShowIntersection}[2]{
\fill
[name intersections={of=#1 and #2, name=i, total=\t}]
[red, opacity=1, every node/.style={above left, black, opacity=1}]
\foreach \s in {1,...,\t}{(i-\s) circle (2pt)
node [above left] {\s}};
}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
yticklabels=\empty, xticklabels=\empty,
width=8cm,
height=6cm,
domain=-2:3, ymin = 0.07, xmin = -2, enlargelimits=upper,
xtick=\empty, ytick=\empty,
clip mode=individual,
xlabel={$T$}, ylabel={$\Delta T$},
]
\addplot[smooth,red,samples=50,domain=-2:3,name path=gaussian]{8 * gauss(1,0.75)};
\addplot[smooth,blue,name path=line]{x*1.13 + 1.36};
\ShowIntersection{gaussian}{line}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
答案1
此解决方案将截距的投影绘制到 x 轴上,并使用以下方法检索 x 值\pgfplotspointgetcoordinates
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\newcommand*{\ShowIntersection}[2]{
\draw [
name intersections={of=#1 and #2,name=i, total=\t}, thin, dotted]
[every node/.style={black,font=\tiny}]
\foreach \s in {1,...,\t}
{(i-\s) node[above left] {\s} % dysplay number of the intersection
node[fill,red,circle,inner sep=0,minimum size=4pt]{} % big dot in the intercection
(i-\s |- i-\s) -- (i-\s |- current axis.south) % line to projet from intesection to x-axis
node [below,font=\tiny] {\pgfplotspointgetcoordinates{(i-\s)} % recover x-y coordinates of the intesection
$\pgfmathprintnumber[fixed]{\pgfkeysvalueof{/data point/x}}$} % and put x below the line
};
}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
yticklabels=\empty, xticklabels=\empty,
width=8cm,
height=6cm,
domain=-2:3, ymin = 0.07, xmin = -2, enlargelimits=upper,
xtick=\empty, ytick=\empty,
clip mode=individual,
xlabel={$T$}, ylabel={$\Delta T$},
]
\addplot[smooth,red,samples=50,domain=-2:3,name path=gaussian]{8 * gauss(1,0.75)};
\addplot[smooth,blue,name path=line]{x*1.13 + 1.36};
\ShowIntersection{gaussian}{line}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}
现在很容易添加 y 截距
\documentclass[12pt]{article}
\usepackage{tikz}
\usetikzlibrary{intersections}
\usepackage{pgfplots}
\pgfplotsset{compat=1.18}
\pgfmathdeclarefunction{gauss}{2}{%
\pgfmathparse{1/(#2*sqrt(2*pi))*exp(-((x-#1)^2)/(2*#2^2))}%
}
\newcommand*{\ShowIntersection}[2]{
\draw [
name intersections={of=#1 and #2,name=i, total=\t}, thin, dotted]
[every node/.style={black,font=\tiny}]
\foreach \s in {1,...,\t}
{(i-\s) node[above left] {\s} % dysplay number of the intersection
node[fill,red,circle,inner sep=0,minimum size=4pt]{} % big dot in the intercection
(i-\s |- i-\s) -- (i-\s |- current axis.south) % line to projet from intesection to x-axis
node [below,font=\tiny] {\pgfplotspointgetcoordinates{(i-\s)} % recover x-y coordinates of the intesection
$\pgfmathprintnumber[fixed]{\pgfkeysvalueof{/data point/x}}$} % and put x below the line
( i-\s|- i-\s)-- (current axis.west |- i-\s)% line to projet from intesection to y-axis
node [left,font=\tiny] {\pgfplotspointgetcoordinates{(i-\s)} % do it again
$\pgfmathprintnumber[fixed]{\pgfkeysvalueof{/data point/y}}$} % and put y left of the line
};
}
\begin{document}
\begin{figure}
\centering
\begin{tikzpicture}
\begin{axis}[
yticklabels=\empty, xticklabels=\empty,
width=8cm,
height=6cm,
domain=-2:3, ymin = 0.07, xmin = -2, enlargelimits=upper,
xtick=\empty, ytick=\empty,
clip mode=individual,
xlabel={$T$}, ylabel={$\Delta T$},
]
\addplot[smooth,red,samples=50,domain=-2:3,name path=gaussian]{8 * gauss(1,0.75)};
\addplot[smooth,blue,name path=line]{x*1.13 + 1.36};
\ShowIntersection{gaussian}{line}
\end{axis}
\end{tikzpicture}
\end{figure}
\end{document}