在 tikz 函数中放置箭头

在 tikz 函数中放置箭头

我正在尝试获得这种表述: 在此处输入图片描述

但我遇到了一些麻烦:

在此处输入图片描述

我试图:

  • 把箭头放大,如手绘函数所示
  • 仅用一行即可获得函数+\addplot箭头
  • 获取手绘函数中所示的图例条目

我的 MWE:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=newest}

\pgfplotsset{my style1/.append style={clip = false, axis lines* = middle, axis equal, xtick = \empty, ytick = \empty}}

\usepackage{tikz}
\usetikzlibrary{arrows} 

\begin{document}
    
    \begin{tikzpicture}
        \begin{axis}[my style1, legend pos=outer north east, samples=200]
            \addplot [red, domain=-2:1] ({x},{0.5*sqrt(4-x^2)});
            \addlegendentry {$\gamma_1$};   
            \addplot [<-, red, domain=1:2] ({x},{0.5*sqrt(4-x^2)});
            
            \addplot [->, blue, domain=-2:-1] ({x},{-0.5*sqrt(4-x^2)}); 
            \addplot [blue, domain=-1:2] ({x},{-0.5*sqrt(4-x^2)});
            \addlegendentry {$\gamma_2$};
            
        \end{axis}
    \end{tikzpicture}
    
\end{document}

答案1

arrows你应该使用 preferred而不是arrows.meta。此包可以高度定制箭头。例如:

\documentclass{article}

\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\pgfplotsset{my style1/.style={clip = false, 
                               axis lines* = middle,
                               axis equal,
                               xtick = \empty, ytick = \empty,
  every axis plot post/.append style = {thick}} % thickness of function,
                                                % if you like to change curve thickness
            }
\usetikzlibrary{arrows.meta}

\begin{document}

    \begin{tikzpicture}[
> = {Straight Barb[line width=2pt, length=2mm]}% define arrow head features
                        ]
\begin{axis}[my style1, legend pos=outer north east, samples=200]
            \addplot [red, domain=-2:1] {0.5*sqrt(4-x^2)};
            \addplot [<-, red, domain=1:2] {0.5*sqrt(4-x^2)};

            \addplot [->, blue, domain=-2:-1] {-0.5*sqrt(4-x^2)};
            \addplot [blue, domain=-1:2]{-0.5*sqrt(4-x^2)};
        \legend{$\gamma_1$,,,$\gamma_2$}; % <---
        \end{axis}
    \end{tikzpicture}

\end{document}

编辑:

一开始我并没有理会图例,但是现在,在 @ferahfeza 评论之后(非常感谢),这里是解决方案:

  • 删除所有\addlegendentry {...};
  • \legend{$\gamma_1$,,,$\gamma_2$};用之前替换它们\end{axis}
  • 现在对上述 MWE 进行了相应修正,结果是:

在此处输入图片描述

附录: 为了好玩并展示另一种可能性,如何绘制你的函数:

\documentclass{article}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\pgfplotsset{my style1/.style={clip = false, 
                               axis lines* = middle,
                               axis equal,
                               xtick = \empty, ytick = \empty,
  every axis plot post/.append style = {very thick}% thickness of function,
                                                   % if you like to change default value
                               }                               
            }
\usetikzlibrary{arrows.meta,
                decorations.markings
                }

\begin{document}
    \begin{tikzpicture}[
->-/.style={decoration={markings,% switch on markings
                        mark=at position 0.75 with {\arrow[line width=2pt]{Straight Barb}}
                        },
            postaction={decorate}
            }
                        ]
\begin{axis}[
    my style1, 
    legend pos=outer north east, 
    samples=101]
\addplot [->-, red,  domain=2:-2] { 0.5*sqrt(4-x^2)};
\addplot [->-, blue, domain=-2:2] {-0.5*sqrt(4-x^2)};
        \legend{$\gamma_1$,$\gamma_2$};
\end{axis}
    \end{tikzpicture}
\end{document}

在此处输入图片描述

相关内容