在axis
环境中,我有一条曲线和一条与它相交的直线。如何以轴单位获取等的(intersection-1)
分量?(intersection-2)
\documentclass{minimal}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\begin{document}
\begin{tikzpicture}
\begin{axis}
\addplot[name path global=GaussCurve] gnuplot[domain=48.00:56.00,samples=100] {exp(-0.5*((x-52.64)/1.82)**2)/(sqrt(2*pi)*1.82)};
\path[name path global=HelperLine] (axis cs:48,0.13288) -- (axis cs:56,0.13288);
\draw[dashed,name intersections={of=GaussCurve and HelperLine}] (axis cs:48,0.13288) -- (intersection-2);
\fill[red] (intersection-2) circle (.1cm);
\end{axis}
\end{tikzpicture}
\end{document}
例如如何获取红色圆圈的 x 分量(intersection-2)
?
该代码是由程序根据一些数据生成的,因此我无法使用所显示的值,它只是示例。
答案1
正如 wh1t3 在评论中所说,您可以使用 提取坐标\pgfgetlastxy{<macro for x>}{<macro for y>}
。为了将其转换为轴单位,您必须应用 PGFplots 使用的坐标转换的逆操作。在下面的示例中,我将转换包装在一个宏中\transformxdimension
,该宏接受长度pt
并设置\pgfmathresult
为包含轴单位的长度:
\documentclass{minimal}
\usepackage{pgfplots}
\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}
\begin{axis}[yticklabel style={/pgf/number format/.cd, fixed, fixed zerofill}]
\addplot[name path global=GaussCurve] gnuplot[domain=48.00:56.00,samples=100] {exp(-0.5*((x-52.64)/1.82)**2)/(sqrt(2*pi)*1.82)};
\path[name path global=HelperLine] (axis cs:48,0.13288) -- (axis cs:56,0.13288);
\draw[dashed,name intersections={of=GaussCurve and HelperLine}] (axis cs:48,0.13288) -- (intersection-2)
node [anchor=south, fill=white, fill opacity=0.75,text opacity=1]{
\pgfgetlastxy{\macrox}{\macroy}
\transformxdimension{\macrox}
\pgfmathprintnumber{\pgfmathresult},%
\transformydimension{\macroy}%
\pgfmathprintnumber{\pgfmathresult}
}
;
\fill[red] (intersection-2) circle (.1cm);
\end{axis}
\end{tikzpicture}
\end{document}
答案2
我尝试了 whlt3 的想法,但这并不容易;请参阅下一个代码(也许我做错了一些事情,因为我不太了解pgfplots
)。我也尝试了\pgfextractx
。我需要在每种情况下使用\pgfextra
来获取 x 组件。
更新杰克的出色回答:
\documentclass{minimal}
\usepackage{tikz,pgfplots}
\usetikzlibrary{intersections}
\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{document}
\begin{tikzpicture}
\begin{axis}
\addplot[name path global=GaussCurve] gnuplot[domain=48.00:56.00,samples=100] {%
exp(-0.5*((x-52.64)/1.82)**2)/(sqrt(2*pi)*1.82)};
\path[name path global=HelperLine] (axis cs:48,0.13288) -- (axis cs:56,0.13288);
\draw[dashed,name intersections={of=GaussCurve and HelperLine,name=i}] (axis %
cs:48,0.13288) -- (i-2)%
\pgfextra{\pgfgetlastxy{\macrox}{\macroy}%
\global\let\macrox\macrox};
\fill[red] (i-2) circle (.1cm);
\draw[dashed] (i-2)--(\macrox,0) node {%
\transformxdimension{\macrox}
\pgfmathprintnumber{\pgfmathresult}};
\end{axis}
\end{tikzpicture}
\end{document}
答案3
随着 PGFPlots v1.16 的发布,现在可以将 (轴) 坐标存储\pgfplotspointgetcoordinates
在内data point
,然后可以通过 调用\pgfkeysvalueof
。
有关详细信息,请查看代码中的注释。
% used PGFPlots v1.16
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{intersections}
\pgfplotsset{
% use this `compat' level or higher, so TikZ coordinates don't have to
% be prefixed by `axis cs:'
compat=1.11,
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
yticklabel style={
/pgf/number format/fixed,
},
]
\addplot [name path global=GaussCurve]
gnuplot [domain=48.00:56.00,samples=100]
{exp(-0.5*((x-52.64)/1.82)**2)/(sqrt(2*pi)*1.82)};
\path [name path global=HelperLine]
(48,0.13288) -- (56,0.13288)
coordinate [at start] (start)
;
\draw [dashed,name intersections={of=GaussCurve and HelperLine}]
(start) -- (intersection-2) -- (intersection-2 |- 0,0)
% -------------------------------------------------------------
% using `\pgfplotspointgetcoordinates' stores the (axis)
% coordinates of e.g. the coordinate (intersection-2) in
% `data point', which then can be called by `\pgfkeysvalueof'
node [at start,below left] {
\pgfplotspointgetcoordinates{(intersection-2)}
$(
\pgfmathprintnumber[fixed]{\pgfkeysvalueof{/data point/x}},
\pgfmathprintnumber[fixed]{\pgfkeysvalueof{/data point/y}}
)$
}
% -------------------------------------------------------------
;
\fill [red] (intersection-2) circle (1mm);
\end{axis}
\end{tikzpicture}
\end{document}