绘制具有跳跃的分段函数

绘制具有跳跃的分段函数

是否可以使用一条线绘制分段函数\addplots?以下是演示我想要做的事情的 MWE:

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

\begin{document}
  \begin{tikzpicture}[declare function={
    g(\x)=\x<0 ? x+2 : 0.5*x-2;}]
    \begin{axis}[
      grid=both, 
      grid style={line width=0.35pt, draw=gray!75},
      axis lines=center,
      axis line style={-},
      xmin=-5, xmax=5,
      ymin=-5, ymax=5,
      ticklabel style={font=\footnotesize,inner sep=0.5pt,fill=white,opacity=1.0, text opacity=1},
      every axis plot/.append style={line width=0.95pt, color=red, samples=500},
      ]
        \addplot[domain=-5:0] {g(x)};
        \addplot[domain= 0:5] {g(x)};
    \end{axis}
  \end{tikzpicture}
\end{document}

理想情况下,我希望进行\addplot[domain=-5:5] {g(x)};某种修改,以便上半场结束和下半场开始之间没有界线。

编辑: 下面是我实际修改的代码。我采纳了建议,samples at并将函数定义为inf存在跳跃不连续性的任何地方:

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.15}
\pgfplotsset{soldot/.style={only marks,mark=*, line width=0.2pt, mark size=1.5pt}}
\pgfplotsset{holdot/.style={fill=white,only marks,mark=*, line width=1.0pt, mark size=1.5pt}}

\begin{document}
  \begin{tikzpicture}[declare function={
    g(\x)=\x<-2 ? 1/(2*(\x+2))+3 : 
      (\x==-2 ? inf :
      (\x< -1 ? 1/(\x+2)-2:
      (\x==-1 ? inf : 
      (\x<  1 ? -\x^2+1: 
      (\x<  3 ? 2*(\x-2)^2-2: -0.5*exp(-\x+3.7)+1))));}]
    \begin{axis}[
      grid=both, 
      grid style={line width=0.35pt, draw=gray!75},
      axis lines=center,
      axis line style={black,-},
      xmin=-5, xmax=5,
      ymin=-5, ymax=5,
      xtick={-6,-5,...,6},
      ytick={-6,-5,...,6},
      ticklabel style={font=\footnotesize,inner sep=0.5pt,fill=white,opacity=1.0, text opacity=1},
      every axis plot/.append style={line width=0.95pt, color=red},
      ]
        %% Using 'samples at' instead of domain to control plotting discontinuities
        \addplot[samples at={-5,-4.95,...,-2.005,-2,-1.95,...,-1.05,-1.005,-1,-0.95,...,5}, unbounded coords=jump] {g(x)};
        \addplot[holdot] coordinates{(-1,0)(2,-2)};
        \addplot[soldot] coordinates{(-1,-1)(2,-1)};
        \draw[dashed, red, line width=0.95pt] ({axis cs:-2,0}|-{rel axis cs:0,0}) -- ({axis cs:-2,0}|-{rel axis cs:0,1});
        \addplot[dashed, samples at={-5,-3}]{3};
        \addplot[dashed, samples at={3,5}]{1};
      \end{axis}
    \end{tikzpicture}
\end{document}

得出以下图表: 具有 2 个跳跃不连续点的分段图

答案1

您可以使用unbounded coords=jump。为此,让我们在 0 处引入一个无界坐标,

g(\x)=\x<0 ? x+2 : (\x>0 ? 0.5*x-2 : inf)

假设你不想使用不必要的大量样本,你可能想要使用

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}
\begin{document}
  \begin{tikzpicture}[declare function={
    g(\x)=\x<0 ? x+2 : (\x>0 ? 0.5*x-2 : inf);}]
    \begin{axis}[unbounded coords=jump,
      grid=both, 
      grid style={line width=0.35pt, draw=gray!75},
      axis lines=center,
      axis line style={-},
      xmin=-5, xmax=5,
      ymin=-5, ymax=5,
      ticklabel style={font=\footnotesize,inner sep=0.5pt,fill=white,opacity=1.0, text opacity=1},
      every axis plot/.append style={line width=0.95pt, color=red, samples=500},
      ]
        \addplot[samples at={-5,-0.01,0,0.01,5}] {g(x)};
    \end{axis}
  \end{tikzpicture}
\end{document}

在此处输入图片描述

如果你真的坚持domain=-5:5,使用

 \addplot[samples=501,domain=-5:5] {g(x)};

但这有大量的样本,但它产生的结果几乎相同。

相关内容