通过给出 x 坐标(pgfplots)绘制垂直线到任意路径或函数

通过给出 x 坐标(pgfplots)绘制垂直线到任意路径或函数

这是这个。我的目标是绘制一条垂直线到任何路径或函数,但主要要求是避免时间路径用于获取交点。

期望结果如下: 在此处输入图片描述

这是我尝试定义一个查找交点的宏,但它不起作用:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{intersections}

\makeatletter
\newcommand\topath[1]{
\path[name path=t] (\the\tikz@lastxsaved,\pgfkeysvalueof{/pgfplots/ymin}) -- (\the\tikz@lastxsaved,\pgfkeysvalueof{/pgfplots/ymax});
\coordinate[name intersections={of=#1 and t}] at (intersection-1)
}
\newcommand\currentcoordinate{\the\tikz@lastxsaved,\the\tikz@lastysaved}
\makeatother

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines = center,
samples=200,
clip=false
]
\def\ymaxv{\pgfkeysvalueof{/pgfplots/ymax}}

\addplot[blue, name path=f, restrict y to domain=-3:5] {x^4-3*x^2+x+2};

% ↓ doesn't work ↓
%\draw[red] (1,0) -- \topath{f};
%\draw[red] (-1.5,0) -- \topath{f};

\end{axis}
\end{tikzpicture}
%
\qquad
%
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
samples=200,
clip=false
]

\addplot[blue, name path=b, smooth, tension=1] coordinates {(0,1) (1,2) (1,0) (0.5,0) (0.5,1) (1.5,0.5) (2,0.7)};

% ↓ doesn't work ↓
%\draw[red] (0.2,0) -- \topath{b};
%\draw[red] (1.5,0) -- \topath{b};

\end{axis}
\end{tikzpicture}

\end{document}

答案1

使用 expl3 自动生成路径名(tempintersectionpath1、tempintersectionpath2 等):

如果您只想从 x 轴到命名路径的交点绘制线条,那么您可以这样做,但否则似乎无法使用 TikZ 界面扩展计算交点。

%! TEX program = lualatex

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\usetikzlibrary{intersections}

\ExplSyntaxOn

% example usage: \topath{\draw[red]}{1.5}{-- (intersection-1)}{f}
% 1.5 is x coordinate

\int_new:N \my__path_index
\int_gset:Nn \my__path_index {0}
\cs_generate_variant:Nn \regex_replace_all:nnN {nVN}
\NewDocumentCommand\topath{mmmm}{
    \int_gset:Nn \my__path_index {\my__path_index+1}
    \tl_set:Nn \my__tmp {
        \path [name~path=tempintersectionpath\my__path_index]
            (#2, \pgfkeysvalueof{/pgfplots/ymin}) --
            (#2, \pgfkeysvalueof{/pgfplots/ymax});
        \path [name~intersections={of=#4~and~tempintersectionpath\my__path_index}];
        #1 (#2, 0) #3;
    }
    \regex_replace_all:nVN {\c{my__path_index}} \my__path_index \my__tmp
    %\nonstopmode \tl_show:N \my__tmp \errorstopmode
    \tl_use:N \my__tmp
}
\ExplSyntaxOff

\begin{document}

\begin{tikzpicture}
\begin{axis}[
axis lines = center,
samples=200,
clip=false
]
\def\ymaxv{\pgfkeysvalueof{/pgfplots/ymax}}

\addplot[blue, name path=f, restrict y to domain=-3:5] {x^4-3*x^2+x+2};

\topath{\draw[red]}{1}{-- (intersection-1)}{f}
\topath{\draw[red]}{-1.5}{-- (intersection-1)}{f}

\end{axis}
\end{tikzpicture}
%
\qquad
%
\begin{tikzpicture}
\begin{axis}[
axis lines = center,
samples=200,
clip=false
]

\addplot[blue, name path=b, smooth, tension=1] coordinates {(0,1) (1,2) (1,0) (0.5,0) (0.5,1) (1.5,0.5) (2,0.7)};

\topath{\draw[red]}{0.2}{-- (intersection-1)}{b}
\topath{\draw[red]}{0.8}{-- (intersection-1)}{b}
\topath{\draw[red]}{1.5}{-- (intersection-1)}{b}

\end{axis}
\end{tikzpicture}

\end{document}

答案2

使用tzplot包裹:

在此处输入图片描述

\documentclass[border=1mm]{standalone}
    
\usepackage{tzplot}

\begin{document}

\begin{tikzpicture}[yscale=.5,font=\tiny]
\tzaxes*(-2,-2)(2,5)
\tzticks(-2pt:2pt){-2/$-2$,-1/$-1$,1}(-1pt:1pt){2,4}
\def\Fx{(\x)^4-3*(\x)^2+\x+2}
\tzfn[blue]\Fx[-2:1.8]
\tzvXpointat{Fx}{-1.5}(A)
\tzvXpointat{Fx}{1}(B)
\tzprojx[red,solid](A)
\tzprojx[red,solid](B)
\end{tikzpicture}
\quad %%%
\begin{tikzpicture}[font=\tiny]
\tzaxes*(-1,-1)(3,2.5)
\tzticks(-1pt:1pt){0.5,1,1.5,2}(-1pt:1pt){0.5,1,1.5,2}
\tzplotcurve[blue]"curve"(0,1)(1,2)(1,0)(0.5,0)(0.5,1)(1.5,0.5)(2,0.7);
\tzvXpointat{curve}{0.2}(A)
\tzvXpointat{curve}{1.5}(B)
\tzprojx[red,solid](A)
\tzprojx[red,solid](B)
\end{tikzpicture}

\end{document}

相关内容