在 pgfplots 中的函数路径中间添加箭头

在 pgfplots 中的函数路径中间添加箭头

我想在绘图中间添加一个箭头,但mark不起作用。我也尝试了\arrow[red]{stealth}而不是>,但它给了我一个错误。这是我的代码

\documentclass[a4paper,12pt]{book}

\usepackage{fontspec}
\usepackage{amsmath,empheq}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{patterns}
\usetikzlibrary{calc}

\begin{document}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0,
xmax=4,
ymin=0,
ymax=4,
]
\path[name path=axis] (1,0)--(3,0);
\addplot[name path=f,smooth,mark=at position 2 with >,domain=1:3,samples=200,blue,thick]{12/(x+3)};
\addplot[yellow] fill between[of=f and axis];
\draw[dashed] (1,3)--(1,0);
\draw[dashed] (3,2)--(3,0);
\fill (1,3) circle (1.5pt);
\fill (3,2) circle (1.5pt);
\end{axis}
\end{tikzpicture}
\caption{}
\label{}
\end{figure}

\end{document}

这是我得到的缺少箭头的图像。

在此处输入图片描述

这是根据用户 LaTexDraw-com 的建议更新的代码

\documentclass[a4paper,12pt]{book}

\usepackage{fontspec}
\usepackage{amsmath,empheq}
\usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{patterns}
\usetikzlibrary{calc}

\tikzstyle witharrow=[postaction={decorate,decoration={markings,mark=at position 0.5 with {\arrow[#1]{stealth}}}}] 

\begin{document}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0,
xmax=4,
ymin=0,
ymax=4,
]
\path[name path=axis] (1,0)--(3,0);
\addplot[name path=f,domain=1:3,samples=200,blue,thick,witharrow=red]{12/(x+3)};
\addplot[yellow] fill between[of=f and axis];
\draw[dashed] (1,3)--(1,0);
\draw[dashed] (3,2)--(3,0);
\fill (1,3) circle (1.5pt);
\fill (3,2) circle (1.5pt);
\end{axis}
\end{tikzpicture}
\caption{}
\label{}
\end{figure}

\end{document}

但现在我又遇到了另一个问题,填充是错误的,如下图所示

在此处输入图片描述

答案1

您既不需要装饰也不需要交叉点。(顺便说一句,您的dimension too large错误可能会消失/pgf/fpu/install only={reciprocal},请参阅第 640 页的 pgfmanual 3.1.8 第 50.2 节。)

\documentclass[a4paper,12pt]{book}

\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows.meta}


\begin{document}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}[pics/arrow/.style={code={
    \draw[blue,-{Stealth[color=red,length=#1]}] (-#1/2,0) -- (#1/2,0);}},
    pics/arrow/.default=3mm,
    bullet/.style={circle,fill,inner sep=0pt,minimum size=3pt}]
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0,
xmax=4,
ymin=0,
ymax=4,
]
\path[name path=axis] (1,0)--(3,0);
\addplot[name path=f,domain=1:3,smooth,blue,thick]{12/(x+3)}
 node[pos=0,bullet](b1){} pic[pos=0.5,sloped]{arrow} node[pos=1,bullet](b2){};
\addplot[yellow] fill between[of=f and axis];
\draw[dashed] foreach \x in {1,2}{(b\x) -- (b\x|-0,0)};
\end{axis}
\end{tikzpicture}
\caption{No decorations.}
\label{fig:No}
\end{figure}

\end{document}

在此处输入图片描述

答案2

这是我的尝试:

\documentclass[a4paper,12pt]{book}

\usepackage{fontspec}
\usepackage{amsmath,empheq}
% \usepackage{tikz}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=newest}
\usetikzlibrary{arrows.meta}
\usetikzlibrary{decorations.pathmorphing}
\usetikzlibrary{decorations.markings}
\usetikzlibrary{patterns}
\usetikzlibrary{calc}

% Arrow style
\tikzset{decorated arrows/.style={
    postaction={
        decorate,
        decoration={
            markings,
            mark=at position 0.5 with {\arrow{stealth}}
            }
        },
    }
}

\begin{document}

\begin{figure}[htbp]
\centering
\begin{tikzpicture}
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0,
xmax=4,
ymin=0,
ymax=4,
]
\path[name path=axis] (1,0)--(3,0);
\addplot[name path=f,smooth,domain=1:3,blue,thick]{12/(x+3)};
\addplot[smooth,domain=1:3,blue,thick,decorated arrows]{12/(x+3)};
\addplot[yellow] fill between[of=f and axis];
\draw[dashed] (1,3)--(1,0);
\draw[dashed] (3,2)--(3,0);
\fill (1,3) circle (1.5pt);
\fill (3,2) circle (1.5pt);
\end{axis}
\end{tikzpicture}
\caption{}
\label{}
\end{figure}

\end{document}

得出的结果是:

在此处输入图片描述

fill between我认为和之间存在问题markings。请查看此帖子”如何在路径中间绘制箭头“ 更多细节

答案3

使用intersections库确定函数的开始和结束坐标,softh clip在反向域上:

\documentclass[12pt]{article}
\usepackage{fontspec}
\usepackage{pgfplots}
\usepgfplotslibrary{fillbetween}
\pgfplotsset{compat=1.17}
\usetikzlibrary{arrows.meta,
                decorations.markings,
                intersections}
\tikzset{
    ->-/.style = {decoration={markings,         % Arrow style
                  mark=at position 0.5 with {\arrow{Stealth}}
                              },                      
                  postaction={decorate},
                  ultra thick, color=blue},
    dot/.style = {circle, fill, inner sep=2pt}  % black circle
        }
\newcommand\Ymax{\pgfkeysvalueof{/pgfplots/ymax}}

\begin{document}
   \begin{figure}[htbp]
    \centering
    \begin{tikzpicture}
\begin{axis}[
axis on top,
width=0.8\textwidth,
axis equal image,
xmin=0, xmax=4,
ymin=0, ymax=4,
tick label style = {font=\footnotesize},
        domain=1:3, 
samples=61
            ]
\path[name path=sgmnt] (1,\Ymax) |- (3,0) -- (3,\Ymax);
\addplot[name path=f,postaction={decorate}] {12/(x+3)};
\addplot[yellow] fill between  [of=sgmnt and f, 
                                soft clip={domain=3:1}]; % <--- inverted domain!
\addplot[->-] {12/(x+3)};   % <--- function with arrow head on the middle
%
\draw   [name intersections={of=sgmnt and f, by={a,b}}]
        [dashed] (1,0) -- (a) node[dot] {}
                 (3,0) -- (b) node[dot] {};
\end{axis}
    \end{tikzpicture}
\caption{My beautiful Diagram}
\label{fig: Field under function}
    \end{figure}
\end{document}

在此处输入图片描述

相关内容