TikZ:帶有間隙的函數的穿刺點

TikZ:帶有間隙的函數的穿刺點

我需要創建一個帶有間隙的函數圖,並使上部點被刺穿,如下圖所示。除了手動創建一個小圓圈並嘗試調整上面的線以適應之外,還有什麼方法可以做到這一點?

這是我的 MWE。

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
    \begin{tikzpicture}[declare function={sigma(\x)=1/(1+exp(-\x));}]
    \begin{axis}
    [
        grid=major,     
        xmin=-6,
        xmax=6,
        axis x line=bottom,
        ytick={0,.5,1},
        ymax=1,
        axis y line=middle,
        samples=100,
        domain=-6:6,
        legend style={at={(1,0.9)}}     
    ]
        \draw [dashed,black!50] (1,0.365) -- (1,0.865);
        \addplot[very thick,black,mark=none, samples=100,domain=-6:1]   (x, {.5 * sigma(x)});
        \addplot[very thick,black,mark=none, samples=100,domain=1:6]   (x, {.5 + .5 * sigma(x)});
    \end{axis}
    \end{tikzpicture}

\end{document}

在此輸入影像描述

答案1

不太明白你所說的調整一條線以適應什麼意思,但我會聲明一個函數並用它來繪製所有內容——繪圖、虛線和圓圈。

透過設定 ,可以pgfplots只在第一個點繪製標記mark repeat=N,其中N是大於或等於樣本數的數字。因此,你可以做

\addplot[very thick,black,mark=*,mark options={fill=white},domain=1:6, mark repeat=1000]  {F(x)+0.5};

繪製包括標記在內的下半部。F是一個用 宣告的函數declare function,請參考下面的程式碼。

在此輸入影像描述

\documentclass{article}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{compat=1.16}

\begin{document}
    \begin{tikzpicture}[
        declare function={
           sigma(\x)=1/(1+exp(-\x));
           F(\x) = 0.5 * sigma(\x);
           }]
    \begin{axis}
    [
        grid=major,     
        xmin=-6,
        xmax=6,
        axis x line=bottom,
        ytick={0,.5,1},
        ymax=1,
        axis y line=middle,
        samples=100,
        legend style={at={(1,0.9)}}     
    ]
        \addplot[very thick,black,mark=none, domain=-6:1]   {F(x)};
        \addplot[very thick,black,mark=*,mark options={fill=white},domain=1:6, mark repeat=1000]    {F(x)+0.5};

        \draw [dashed,black!50] (1,{F(1)}) -- (1,{F(1)+0.5});        
    \end{axis}
    \end{tikzpicture}

\end{document}

相關內容