PGFPlots:如何正确获取坐标的 x 分量值以便在计算中使用它?

PGFPlots:如何正确获取坐标的 x 分量值以便在计算中使用它?

如何修复的语法\draw (i-2) circle let \p{dgcr} = (i-2) in node at (i-2) {$d_{g,cr} = \fpeval{\x{dgcr}/0.35}$};以使节点计算的数字正确(即,它应该在左右0.6,而不是316)?

在此处输入图片描述

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc, intersections}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin = 0, xmax = 0.4, 
            ymin = 0, ymax = 0.6,
            xtick distance = {0.2},
            ytick distance = {0.2},
            minor tick num = 1,
            grid = both,
            ]
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \addplot 
            [
            domain = 0:1.5, 
            samples = 100, 
            black, 
            variable = dg,
            name path = OptBrakingDist,
            ] (
            { dg / 2.5 * (1.2 - dg * 0.5) },
            { dg / 2.5 * (1.3 + dg * 0.5) } 
            );
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \addplot 
            [
            domain = 0:\fpeval{1/(1 - 0.35)}, 
            %samples = 2,
            blue,           
            variable = dg,
            name path = fixeddist,
            ]
            ( { 0.35 * dg } , { (1 - 0.35) * dg } )
            ;
            %%%%%%%%%%%%%%%%%%%%%%%%
            % Find and label the intersections
            \path [name intersections = {of = fixeddist and OptBrakingDist, name=i}];
            %
            \draw[fill=red, draw=none] (i-2) circle [radius=3pt] let \p{dgcr} = (i-2) in node[left=3pt, inner sep=0pt, fill=white, scale=0.75] at (i-2) {$d_{g,cr} = \fpeval{\x{dgcr}/0.35}$};
        \end{axis}
    \end{tikzpicture}
\end{document}

答案1

我找到了这个帖子交叉口坐标 我想知道它是否合适。关键是\pgfplotspointgetcoordinates{(i-2)}

代码

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc, intersections}
%%%%%%%%%%%%%%
%   https://tex.stackexchange.com/questions/220284/coordinates-of-intersection
%
%%%%%%%%%%%%%%
\newcommand*{\ShowIntersection}{
        \fill [
            name intersections={
                of = fixeddist and OptBrakingDist,
                name=i,
            },
            fill=red,
        ]  (i-2) circle (3pt)
            node [left=3pt, inner sep=0pt, fill=white, scale=0.75] {
                % -------------------------------------------------------------
                % using `\pgfplotspointgetcoordinates' stores the (axis)
                % coordinates in `data point' which then can be called by
                % `\pgfkeysvalueof'
                \pgfplotspointgetcoordinates{(i-2)}
                $d_{g,cr} \approx \pgfmathprintnumber{\fpeval{\pgfkeysvalueof{/data point/x}/0.35}}$
                % -------------------------------------------------------------
        };
    }
\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin = 0, xmax = 0.4, 
            ymin = 0, ymax = 0.6,
            xtick distance = {0.2},
            ytick distance = {0.2},
            minor tick num = 1,
            grid = both,
            ]
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \addplot 
            [
            domain = 0:1.5, 
            samples = 100, 
            black, 
            variable = dg,
            name path = OptBrakingDist,
            ] (
            { dg / 2.5 * (1.2 - dg * 0.5) },
            { dg / 2.5 * (1.3 + dg * 0.5) } 
            );
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \addplot 
            [
            domain = 0:\fpeval{1/(1 - 0.35)}, 
            %samples = 2,
            blue,           
            variable = dg,
            name path = fixeddist,
            ]
            ( { 0.35 * dg } , { (1 - 0.35) * dg } )
            ;
            %%%%%%%%%%%%%%%%%%%%%%%%
            % Find and label the intersections
            \ShowIntersection
            % \path [name intersections = {of = fixeddist and OptBrakingDist, name=i}];
            % %
            % \draw[fill=red, draw=none] (i-2) circle [radius=3pt] let \p{dgcr} = (i-2) in node[left=3pt, inner sep=0pt, fill=white, scale=0.75] at (i-2) {$d_{g,cr} = \fpeval{\x{dgcr}/0.35}$};
        \end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

答案2

棘手的部分是以轴 cs: 单位获取答案。因此,我首先获取已知位置的屏幕坐标,然后根据交点的屏幕坐标使用它们计算轴 cs: 坐标。

请注意,轴原点并不总是与 tikz 原点相同。

\documentclass{article}
\usepackage{pgfplots}
\usetikzlibrary{calc, intersections}

\begin{document}
    \begin{tikzpicture}
        \begin{axis}[
            xmin = 0, xmax = 0.4, 
            ymin = 0, ymax = 0.6,
            xtick distance = {0.2},
            ytick distance = {0.2},
            minor tick num = 1,
            grid = both,
            ]
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \addplot 
            [
            domain = 0:1.5, 
            samples = 100, 
            black, 
            variable = dg,
            name path = OptBrakingDist,
            ] (
            { dg / 2.5 * (1.2 - dg * 0.5) },
            { dg / 2.5 * (1.3 + dg * 0.5) } 
            );
            %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
            \addplot 
            [
            domain = 0:\fpeval{1/(1 - 0.35)}, 
            %samples = 2,
            blue,           
            variable = dg,
            name path = fixeddist,
            ]
            ( { 0.35 * dg } , { (1 - 0.35) * dg } )
            ;
            \path (axis cs: 0,0) coordinate (origin);% axis origin
            \path (axis cs: 0.1,0.1) coordinate (unit);% 0.1 unit vector
        \end{axis}
        \path (origin); \pgfgetlastxy{\xo}{\yo}%
        \path (unit); \pgfgetlastxy{\xunit}{\yunit}%
        %%%%%%%%%%%%%%%%%%%%%%%%
        % Find and label the intersections
        \path [name intersections = {of = fixeddist and OptBrakingDist, name=i}];
        %
        \draw[fill=red, draw=none] (i-2) circle [radius=3pt] let \p{dgcr} = (i-2) in node[left=3pt, inner sep=0pt, fill=white, scale=0.75] at (i-2) {$d_{g,cr} = \fpeval{\x{dgcr}/0.35}$};
        \path (i-2); \pgfgetlastxy{\xint}{\yint}%
        \draw (i-2) circle node[above] at (i-2) {$d_{g,cr} = \fpeval{0.1*(\xint-\xo)/(\xunit-\xo)}$};
        
    \end{tikzpicture}
\end{document}

相关内容