如何向使用“pgfplots”绘制的函数添加矩形并显示评估线?

如何向使用“pgfplots”绘制的函数添加矩形并显示评估线?

我使用 pgfplots 绘制了以下函数:

在此处输入图片描述

我想以某种方式添加如下窗口:

在此处输入图片描述

它可以只是线条,或者具有透明的背景颜色。

我使用的代码是这样的:

\begin{tikzpicture}
    \begin{axis}[
        height=4cm,
        width=0.6\textwidth,
        axis lines=middle,
        grid,                  % <--- added 
        grid style = {dashed}, % <--- added
        xlabel=$t$,     xlabel style={anchor=west},
        ylabel=$f(t)$,  ylabel style={anchor=south},
        xmin=-0.5,   xmax=10.5,  % <--- changed
        ymin=-0.5,  ymax=7.8 % <--- changed
        ]
        \addplot [thick,mark=*]
        coordinates { (0,0) (2,2)};
        \addplot [thick,o-, shorten <>=-2.5\pgflinewidth]
        coordinates {(2,4) (5,7)};
        \addplot [thick,mark=*]
        coordinates {(5,7) (8,3) (10,3)};    
        \end{axis}
\end{tikzpicture}

此外,我还想知道是否有办法用点来放置这条评估线(绿色)。

在此处输入图片描述

在此处输入图片描述

\begin{tikzpicture}
    \begin{axis}[
    height=5cm,
    width=0.6\textwidth,
    axis lines=middle,
    grid,                  % <--- added 
    grid style = {dashed}, % <--- added
    xlabel=$t$,     xlabel style={anchor=west},
    ylabel=$f^{\ast}(t)$,  ylabel style={anchor=south},
    xmin=-0.5,   xmax=10.5,  % <--- changed
    ymin=-0.5,  ymax=7.5 % <--- changed
    ]
    \addplot [thick,mark=*]
    coordinates { (0,0) (2,2)};
    \addplot [thick,o-]
    coordinates {(2,3) (4,3)};
    \addplot [thick,mark=*]
    coordinates { (4,3) (4.75,4) (10,7)};    
    \end{axis}
\end{tikzpicture}

我将非常感谢你的帮助!

这是完整的代码:

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
%\pgfplotsset{width=8cm,compat=1.3} %gráficas
\pgfplotsset{compat=1.3,
    every mark/.append style={scale=1},
    scale only axis,
}
\usetikzlibrary{arrows}
\tikzset{shorten <>/.style = {shorten <=#1, shorten >=#1}}


\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        height=4cm,
        width=0.6\textwidth,
        axis lines=middle,
        grid,                  % <--- added 
        grid style = {dashed}, % <--- added
        xlabel=$t$,     xlabel style={anchor=west},
        ylabel=$f(t)$,  ylabel style={anchor=south},
        xmin=-0.5,   xmax=10.5,  % <--- changed
        ymin=-0.5,  ymax=7.8 % <--- changed
        ]
        \addplot [thick,mark=*]
        coordinates { (0,0) (2,2)};
        \addplot [thick,o-, shorten <>=-2.5\pgflinewidth]
        coordinates {(2,4) (5,7)};
        \addplot [thick,mark=*]
        coordinates {(5,7) (8,3) (10,3)};    
        \end{axis}
\end{tikzpicture}


\begin{tikzpicture}
    \begin{axis}[
    height=5cm,
    width=0.6\textwidth,
    axis lines=middle,
    grid,                  % <--- added 
    grid style = {dashed}, % <--- added
    xlabel=$t$,     xlabel style={anchor=west},
    ylabel=$f^{\ast}(t)$,  ylabel style={anchor=south},
    xmin=-0.5,   xmax=10.5,  % <--- changed
    ymin=-0.5,  ymax=7.5 % <--- changed
    ]
    \addplot [thick,mark=*]
    coordinates { (0,0) (2,2)};
    \addplot [thick,o-]
    coordinates {(2,3) (4,3)};
    \addplot [thick,mark=*]
    coordinates { (4,3) (4.75,4) (10,7)};    
    \end{axis}
\end{tikzpicture}

\end{document}

答案1

使用轴坐标系绘制框很容易,只需使用命令即可\draw[red] (axis cs:3,0) rectangle (axis cs:7,7.5);。如果要填充它,只需使用\fill

对于评估线,我使用了库。通过添加到选项中intersections,为要在其上标记评估线的路径命名。然后,添加一条足够长的幻影路径,以便与您的函数相交,例如,。其余部分使用name path global=myFunction\addplot\path [name path=myEvaluation] (axis cs:5.5,0) -- (axis cs:5.5,6);

\draw [thick,green!80!black,name intersections={of=myFunction and myEvaluation,name=i}] (axis cs:5.5,0) -- (i-1) node [fill,circle,at end,inner sep=2pt] {};

name intersections={of=myFunction and myEvaluation,name=i}选项查找两条路径myFunction和的所有交点,myEvaluation并将它们命名为i-1i-2i-3等。在本例中,只有一个交点 。i-1我们用它来绘制评估线,并在末端添加一个节点来标记圆。

\documentclass{article}
\usepackage{tikz}
\usepackage{pgfplots}
%\pgfplotsset{width=8cm,compat=1.3} %gráficas
\pgfplotsset{compat=1.3,
    every mark/.append style={scale=1},
    scale only axis,
}
\usetikzlibrary{arrows,intersections,plotmarks}
\tikzset{shorten <>/.style = {shorten <=#1, shorten >=#1}}


\begin{document}

\begin{tikzpicture}
    \begin{axis}[
        height=4cm,
        width=0.6\textwidth,
        axis lines=middle,
        grid,                  % <--- added 
        grid style = {dashed}, % <--- added
        xlabel=$t$,     xlabel style={anchor=west},
        ylabel=$f(t)$,  ylabel style={anchor=south},
        xmin=-0.5,   xmax=10.5,  % <--- changed
        ymin=-0.5,  ymax=7.8 % <--- changed
        ]
        \addplot [thick,mark=*]
        coordinates { (0,0) (2,2)};
        \addplot [thick,o-, shorten <>=-2.5\pgflinewidth]
        coordinates {(2,4) (5,7)};
        \addplot [thick,mark=*]
        coordinates {(5,7) (8,3) (10,3)};    
        \draw[red] (axis cs:3,0) rectangle (axis cs:7,7.5);
        \end{axis}
\end{tikzpicture}


\begin{tikzpicture}
    \begin{axis}[
    height=5cm,
    width=0.6\textwidth,
    axis lines=middle,
    grid,                  % <--- added 
    grid style = {dashed}, % <--- added
    xlabel=$t$,     xlabel style={anchor=west},
    ylabel=$f^{\ast}(t)$,  ylabel style={anchor=south},
    xmin=-0.5,   xmax=10.5,  % <--- changed
    ymin=-0.5,  ymax=7.5 % <--- changed
    ]
    \addplot [thick,mark=*]
    coordinates { (0,0) (2,2)};
    \addplot [thick,o-]
    coordinates {(2,3) (4,3)};
    \addplot [thick,mark=*,name path global=myFunction]
    coordinates { (4,3) (4.75,4) (10,7)};
    \path [name path=myEvaluation] (axis cs:5.5,0) -- (axis cs:5.5,6);
    \draw [thick,green!80!black,name intersections={of=myFunction and myEvaluation,name=i}] (axis cs:5.5,0) -- (i-1) node [fill,circle,at end,inner sep=2pt] {};
    \end{axis}
\end{tikzpicture}

\end{document}

输出:

答案2

与@Rushi 的回答非常相似......但进行了一些题外的调整(用于练习):-)

除了使用MWEarrows.meta之外的库intersection还可以:

\documentclass[border=3.141592, varwidth] {standalone}
\usepackage{pgfplots}
%\pgfplotsset{width=8cm,compat=1.3} % far to old version ...
\pgfplotsset{compat=1.18,           % recent version, update your LaTeX instalation!
        width=0.6\textwidth,
    % common axis options are moved here
        axis lines=middle,
        scale only axis,
        grid,
        grid style = {dashed},
        xmin=-0.5,   xmax=10.5,
        ymin=-0.5,   ymax= 7.8,
        tick label style = {font=\footnotesize}  % <--- new
            }
\usetikzlibrary{arrows.meta,    % <--- changed
                intersections}  % <--- new

\begin{document}
    \begin{tikzpicture}
\begin{axis}[
    height=4cm,
    xlabel=$t$,     xlabel style={anchor=west},
    ylabel=$f(t)$,  ylabel style={anchor=south},
            ]
\addplot [thick,mark=*] coordinates { (0,0) (2,2)};
\addplot [thick,
          {Circle[open, length=0pt 6, sep=0pt -3]}-]    % <--- new
                        coordinates { (2,4) (5,7)};
\addplot [thick,mark=*] coordinates { (5,7) (8,3) (10,3)};
\draw[red, thick]   (3,0) rectangle (7,7.6);
\end{axis}
    \end{tikzpicture}

    \begin{tikzpicture}
\begin{axis}[
    height=5cm,
    xlabel=$t$,            xlabel style={anchor=west},
    ylabel=$f^{\ast}(t)$,  ylabel style={anchor=south},
    extra x ticks={5.4},                        % <--- new
    extra x tick style={grid=none, text=teal},  % <--- new
            ]
\addplot [thick,mark=*] coordinates { (0,0) (2,2)};
\addplot [thick,
          {Circle[open, length=0pt 6, sep=0pt -3]}-]    % <--- new
                        coordinates { (2,3) (4,3)};
\addplot [thick,mark=*, name path=A]
                        coordinates { (4,3) (4.75,4) (10,7)};
\path[name path=B]  (5.4,0) -- (5.4,5);
\path[name intersections={of=A and B, by=AB},  % <--- new
      draw=teal, thick, dashed, -{Circle[length=0pt 6, sep=0pt -3]}]
     (5.4,0) -- (AB);
\end{axis}
    \end{tikzpicture}
\end{document}

与您的 MWE 相比,上述 MWE 的变化以 为标志% <---

在此处输入图片描述

答案3

使用tzplot包裹:

图加框

在此处输入图片描述

\documentclass[tikz]{standalone}
    
\usepackage{tzplot}

\begin{document}

\begin{tikzpicture}[yscale=.7,font=\scriptsize,thick]
\tzhelplines[step=2](11,7.9)
\settzdotsize{4pt}
\tzaxes[thin](-.4,-.4)(11,7.9){$t$}{$f(t)$}
\tzticks{2,4,...,10}{2,4,6}
\tzplot(0,0)(2,2);
\tzplot"AA"(2,4)(5,7)(8,3)(10,3);
\tzdot[fill=white,thick](2,4)
\tzdots*(0,0)(2,2)(5,7)(8,3)(10,3);
% box
\tzrectangle[red](2.5,0)(6.5,7.5)
\end{tikzpicture}

\end{document}

绘图加投影

在此处输入图片描述

\documentclass[tikz]{standalone}
    
\usepackage{tzplot}

\begin{document}
    
\begin{tikzpicture}[yscale=.7,font=\scriptsize,thick]
\tzhelplines[step=2](11,7.9)
\settzdotsize{4pt}
\tzaxes[thin](-.4,-.4)(11,7.9){$t$}{$f(t)$}
\tzticks{2,4,...,10}{2,4,6}
\tzplot(0,0)(2,2);
\tzplot"AA"(2,3)(4,3)(4.75,4)(10,7);
\tzdot[fill=white,thick](2,3)
\tzdots*(0,0)(2,2)(4,3)(4.75,4)(10,7);
% projection
\tzvXpointat*[teal]{AA}{5}(X)
\tzprojx[teal,dashed](X)
\end{tikzpicture}

\end{document}

相关内容