使用 pgfplots 和 tikz 创建带标签的虚线交叉点

使用 pgfplots 和 tikz 创建带标签的虚线交叉点

问题:

我遇到了几个小问题,似乎无法解决。

  1. 将标签添加并定位到 x/y 轴
  2. 添加虚线交叉口
  3. 显示 -1 和 1 的 x/y 轴数字

最小工作示例(MWE):

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}

\begin{document}

\begin{tikzpicture}
        \begin{axis}[%
            axis on top = true,
            domain = -13:7,
            samples = 150,
            xmin=-13,
            xmax=7,
            ymin=-13,
            ymax=7,
            minor x tick num=4,
            minor y tick num=4,
            axis lines = middle,
            xtick distance = 5,
            xticklabel style = {font=\footnotesize},
            yticklabel style = {font=\footnotesize},
            xlabel = $x$,
            ylabel = $y$
            ]
            \addplot[very thick, blue] {2*abs(\x-1)-3*abs(\x+2)} node[pos=-.7,left] {\footnotesize $y=|2x-1|-3|x+2|$};
            \addplot[thick, red, dashed] (x,4);
        \end{axis}
\end{tikzpicture}

\end{document}

电流输出:

在此处输入图片描述

期望输出:

在此处输入图片描述

答案1

使用intersectionsTiz库,可以得到以下解决方案。

\documentclass{article}
\usepackage{pgfplots}
\usepackage{tikz}
\usetikzlibrary{intersections}

\begin{document}
\begin{tikzpicture}
        \begin{axis}[%
            axis on top = true,
            domain = -13:7,
            samples = 150,
            xmin=-13,
            xmax=7,
            ymin=-13,
            ymax=7,
            minor x tick num=4,
            minor y tick num=4,
            axis lines = middle,
            xtick distance = 5,
            xticklabel style = {font=\footnotesize},
            yticklabel style = {font=\footnotesize},
            xlabel = $x$,
            ylabel = $y$,
        extra x ticks={-1, 1},
        extra y ticks={1},
            ]
            \addplot[very thick, blue,name path=curve] {2*abs(\x-1)-3*abs(\x+2)};%
            % node[pos=-.7,left] {\footnotesize $y=|2x-1|-3|x+2|$};
            \addplot[thick, red, dashed,name path=line,domain=-13:1] (x,4);
            \path [name intersections={of=curve and line, by={a,b}}];
            \path [name path=x](axis cs:-13,0)--(axis cs:7,0);
           \path [name path=y1](a)|-(axis cs:0,-13);
           \path [name path=y2](b)|-(axis cs:0,-13);
           \path [name intersections={of=y1 and x, by={x1}}];
           \path [name intersections={of=y2 and x, by={x2}}];
           \draw [thick,dashed,red](a)--(x1);
           \draw [thick,dashed,red](b)--(x2);
 \addplot[very thick, red,dashed,domain=-13:1,name path=x8] {(-\x-8)};
 \path [name intersections={of=x8 and line, by={x3}}];
 \path [name path=y3](x3)|-(axis cs:0,-13);
 \path [name intersections={of=y3 and x, by={x4}}];
 \draw [thick,dashed,red](x3)--(x4);
 \node at (axis cs:2.5,4){$y=4$};
 \node[blue] at (axis cs:-7,6) {\footnotesize $y=|2x-1|-3|x+2|$};
        \end{axis}

\end{tikzpicture}

\end{document}

在此处输入图片描述

相关内容