4

I want to make a similar graph as given below but without x-axis labels.

enter image description here

This is what I got with the code given below: enter image description here

\documentclass{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
    \begin{axis}%
    [
        %grid=major,     
        xmin=-6,
        xmax=6,
        axis x line=bottom,
        ytick={0,0.1,...,1},
        ymax=1.1
       % axis y line=middle,
    ]
        \addplot%
        [
            blue,%
            mark=none,
            samples=100,
            domain=-6:6,
        ]
        (x,{1/(1+exp(-x))});
    \end{axis}
\end{tikzpicture}
\end{document}

I'm not able to get the tangent on the S-shaped curve and box around the figure as given in figure one without x-axis labels. Any help will be highly appreciated. Thanks

MYaseen208
  • 8,587

3 Answers3

4

If you want to remove the labels, then write

xticklabels={\empty}

If you wan to remove the ticks too, then add

xtick={\empty}

However, your graph appears slightly different than the image you posted in your question, so if you really want that kind of axes, remove or comment out this line

axis x line=bottom,

For drawing the tangent line, since it's only for illustration purpose, I placed a node along the plot using the decorations.markings library, and placed it somewhere where the south west and north east corners matched the plot the most, so placed a line along that diagonal, extending the two extremities, et voilà.

The nodes are added using labels, but you could use nodes too by using the decoration node as reference.

Finally, for setting the labels with a decimal, you can add the following code to your axis options:

y tick label style={
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=1,
        /tikz/.cd
    },

Output

example image

Code

\documentclass[margin=10pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.8}
\usetikzlibrary{decorations.markings}
\begin{document}
\begin{tikzpicture}
    \begin{axis}%
    [
        %grid=major,     
        xmin=-6, xmax=6,
        ymax=1.1,
        ytick={0,0.1,...,1},       
        xtick={\empty},
        xticklabels={\empty},
        y tick label style={
        /pgf/number format/.cd,
            fixed,
            fixed zerofill,
            precision=1,
        /tikz/.cd
        },
    ]
        \addplot%
        [
            blue,%
            mark=none,
            samples=100,
            domain=-6:6,
            decoration={
                markings,
                mark=at position 0.72 with {%
                    \node[minimum size=2.5mm, inner sep=0, 
                        label=90:{\scriptsize\sffamily 1}, 
                        label=180:{\scriptsize $\beta\pi(1-\pi)$}] (a) {};}
            },postaction=decorate
        ]
        (x,{1/(1+exp(-x))});
    \end{axis}
    \draw[blue, shorten <=-8mm, shorten >=-8mm] (a.south west) -- (a.north east);
    \draw[blue] (a.south west) -- (a.north west) -- (a.north east);
\end{tikzpicture}
\end{document}
Alenanno
  • 37,338
4

To remove ticks from top and right you can add xtick pos=left and ytick pos=left.

You can define two coordinates on your plot with coordinate [pos=0.66] (A) and coordinate [pos=0.7] (B) after addplot command.

\documentclass[margin=5mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{calc}
\pgfplotsset{compat=1.8}
\begin{document}
\begin{tikzpicture}
    \begin{axis}%
    [
        %grid=major,     
        xmin=-6,
        xmax=6,
        xtick pos=left,
        ytick pos=left,
        ytick={0,0.1,...,1.1},
        ymax=1.1,
        yticklabel style={
        /pgf/number format/precision=1,
        /pgf/number format/fixed,
        /pgf/number format/fixed zerofill}
       % axis y line=middle,
    ]
        \addplot%
        [
            blue,%
            mark=none,
            samples=100,
            domain=-6:6,
        ]
        (x,{1/(1+exp(-x))})
        coordinate [pos=0.66] (A)
        coordinate [pos=0.7] (B);
        \coordinate (C) at (A|-B) ;
        \draw (A)--(C)node[midway,left]{\scriptsize $\beta \pi(a-\pi)$};
        \draw (B)--(C)node[midway,above]{\scriptsize $1$};
        \draw ($(A)!-1!(B)$)--($(B)!-1!(A)$);
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

Salim Bou
  • 17,021
  • 2
  • 31
  • 76
  • Thanks @salim bou for your effort, time and help. Would appreciate if you guide how to get y-labels like 0.0, 0.1, ...,0.9, 1.0 rather than 0, 0.1, ...,0.9, 1. Thanks again. – MYaseen208 Dec 29 '15 at 18:50
  • With yticklabel style you can modify format of numbers with key number format, code updated. – Salim Bou Dec 29 '15 at 18:59
1

For fun: pstricks-add can easily plot tangent lines at specific points on curves with the \psplotTangent command:

\documentclass[svgnames, border=4pt]{standalone}
\usepackage[utf8]{inputenc}
\usepackage[T1]{fontenc}
\usepackage{fourier}
\usepackage{ pstricks-add}
\usepackage{auto-pst-pdf}
\def\F{1/(1 + EXP(-x))}
\def\Fx{(EXP(-x))/((1 + EXP(-x))^2)}

\begin{document}

\psset{yunit=10cm, arrowinset=0.15, labelFontSize=\footnotesize, tickwidth =0.6pt}
\begin{pspicture*}(-7,-1)(6.1,1.06)
    \psaxes[axesstyle=frame, linecolor=SteelBlue, tickcolor=SteelBlue, labels=y, Dy=0.1, ticks=y, yticksize=-3pt 0pt, %xticksize=0pt -3pt,
    yDecimals=1,](-6,0)(-6,-0.06)(6,1.06)%(-6,0.06)
    \psset{linewidth=1.2pt, plotpoints=200, plotstyle=curve,algebraic, labelsep=0.5em}
    \psplot[linecolor =IndianRed]{-6}{6}{\F}%{1/(1 + EXP(-x))}%
    \psplotTangent[Derive=(\Fx), linewidth=0.6pt, linecolor=SteelBlue, showpoints]{2}{3}{\F}
    \psCoordinates[linestyle=none, linewidth=0.4pt, showpoints=false](*2 {\F})
    %\psset{origin = {0,-0.06}}
    \psaxes[ linewidth=0.6pt, linecolor=SteelBlue, tickcolor=SteelBlue, labels=x, Ox=-6, ticks=x, xticksize=0pt -3pt, xlabelPos =bottom](-6,-0.06)(-6,-0.06)(6,-0.06)%
\end{pspicture*}

\end{document}

enter image description here

Bernard
  • 271,350