Tikz Figure for a Function With Parameters

Tags: Tikz , Logistic

(Español: Figura en TikZ para una función con parámetros)

I promised in another post that I would show the code for the TikZ figure I included in that post, reproduced below:

What is very nice about TikZ and its pfgplots package is that the curve in the figure is really a logistic curve, and not just a Bezier curve, or a spline that attempts to mimic the shape of a logistic. Specifically, the function whose graph shows up in the figure is given by $$ f(t) = \frac{1}{1+e^{-4(x-2.5)}}. $$

If one were to plot this function in TikZ with minimal commands, the output would look like this:

The code for this last figure is very simple and pretty:

\documentclass[border=5pt, 12pt, tikz]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture} 
\begin{axis}%   
   [ axis lines = middle
   , width=200pt
   , samples=75 
   , xlabel = {$t$} 
   , ymax=1.2       
   ] 
   \addplot%
      [ color=blue
      , domain=0:4.5
      , style=thick
      ] {1/(1+exp(-4*(x-2.5))}%
      node
      [ above left
      , pos=0.7
      ] {$ \displaystyle f(t)=\frac{1}{1+e^{-4(t-2.5)}}$};
\end{axis} 
\end{tikzpicture}
\end{document}

The actual code for the figure is only between the \begin{tikzpicture} and the \end{tikzpicture}, with all the rest being the required commands to tell $\LaTeX$ to create the document containing the figure. Thus, in reality the code is just the following:

\begin{axis}%   
   [ axis lines = middle
   , width=200pt
   , samples=75 
   , xlabel = {$t$} 
   , ymax=1.2       
   ] 
   \addplot%
      [ color=blue
      , domain=0:4.5
      , style=thick
      ] { 1/(1+exp(-4*(x-2.5)) }%
      node
      [ above left
      , pos=0.7
      ] {$ \displaystyle f(t)=\frac{1}{1+e^{-2(t-2.5)}}$};
\end{axis} 

And this itself only contains a couple of pieces:

  • The \begin{axis}[....] which instructs TikZ to create a set of axes with some properties (what is inside the [....]).
  • The \addplot[...]{...} command, which instructs TikZ to add the plot of {...} with the properties inside the [...] (like color blue, thick line, etc.).
  • The second part in \addplot[...]{...}node[...]{....}; which instructs TikZ to add a label with the node command, where the label text is inside the {...} and the properties are once more inside the [...].

Getting from this figure to the one with the labels and parameters is a matter of gradually making tweaks to get there.

Getting axis labels to be general parameters instead of numbers

These are the fancier axis properties that accomplish this.

\begin{axis}%   
[ axis lines = middle
, width=200pt
, samples=75 
, xlabel = {$t$} 
, ymax=1.2
, xtick={2.5}
, xticklabel={$a$}
, ytick ={1/2,1}
, yticklabels={$L/2$,$L$}
, every axis x label/.style={at={(current axis.right of origin)},anchor=north west}        
] 

Here:

  • The xtick={2.5}, xticklabel={$a$} options remove the $1,2,3,4$ tickmarks on the horizontal axis and replace them with only one tickmark at $x=2.5$ with label $a$.
  • The ytick ={1/2,1}, yticklabels={$L/2$,$L$}, do the same but with the vertical axis labels.
  • The strange looking every axis x label/.style ... changes the placement of the $t$ label for the horizontal axis (a matter of taste).

This is the resulting figure:

Adding dashed lines and arrow to the inflection point

Top horizontal line, with its label: is done by graphing the function $f(x)=1$ (the {1} in the code below).

\addplot%
   [ style=dashed
   , color=black
   , domain=0:5
   ] {1} 
   node
   [ above
   , pos=0.6
   ] {top horizontal asymptote};

Middle horizontal line: is done by graphing the function $f(x)=1$ (the {1/2} in the code below).

\addplot
   [ style=dashed
   , color=black
   , domain=0:2.5
   ] {1/2};

Bottom horizontal line, with its label: is done by graphing the function $f(x)=1$ (the {0} in the code below).

\addplot
   [ color=black
   , domain=0:4
   ] {0} 
   node
   [above
   , pos = 0.2
   , text width = 2cm
   , align=center
   ] {bottom horizontal asymptote};

Vertical line at $x=2.5$: this cannot be obtained from the graph of a function, so a new strategy is needed (draw command).

\draw
   [ style = dashed
   ] (axis cs: 2.5,1/2) -- (axis cs: 2.5,0);

Here:

  • The \draw is the main command.
  • The [style = dashed] is the line style.
  • The (...) -- (...) draws a line between the two points.
  • The (axis cs: allows one to use the axis coordinate system instead of the absolute coordinate system of the figure (related to the width of the figure, for example).

The label “inflection point” pointing with an arrow to the point (axis cs:2.5,0.5):

\draw
   [ ->
   , > = latex
   ] (axis cs:1.5,0.7) 
      node 
      [ left
      , text width = 1cm
      , align=center
      ] {inflection point}
      -- (axis cs:2.5,0.5);

Complete code

And that is all!

Here is the complete code, without so much horizontal indentation

\documentclass[border=5pt, 12pt, tikz]{standalone}
\usepackage{pgfplots}

\begin{document}
\begin{tikzpicture} 
\begin{axis}%   
[ axis lines = middle
, width=200pt
, samples=75 
, xlabel = {$t$} 
, ymax=1.2
, xtick={2.5}
, xticklabel={$a$}
, ytick ={1/2,1}
, yticklabels={$L/2$,$L$}
, every axis x label/.style={at={(current axis.right of origin)},anchor=north west}        
]

\addplot[color=blue, domain=0:4.5,style=thick]{1/(1+exp(-4*(x-2.5))} 
   node[right,pos=0.6]{$ \displaystyle\frac{L}{1+e^{-k(t-a)}}$};

\addplot[style=dashed, color=black, domain=0:5]{1} 
   node[above,pos=0.6]{top horizontal asymptote};

\addplot[style=dashed, color=black, domain=0:2.5]{1/2};

\addplot[color=black, domain=0:4]{0} 
   node[above,pos=0.2,text width=2cm, align=center]{bottom horizontal asymptote};

\draw[style=dashed] (axis cs: 2.5,1/2) -- (axis cs: 2.5,0);

\draw[->,>=latex]
   (axis cs:1.5,0.7) 
      node [left, text width=1cm, align=center]{inflection point}
   --(axis cs:2.5,0.5);
\end{axis} 
\end{tikzpicture}
\end{document}

Subscribe

Want to get an email when a new post is added? If so, subscribe here.