# Plot factorial function in TikZ datavisualization

I'm trying to replicate this graph following the tips in this solution with TikZ datavisualization package (see Part VI).

 related question: How to make a figure showing growth of functions but with scaled y-axis?

This is the desired output:

bonus: it would be wonderful if it would be possible to reproduce this graph adding colored regions.

This is my output:

I have two problems that I don't know how to resolve:

1. I have to manually compute the values at which to stop the x of the functions x^2, 2^n, x! because otherwise they will overflow from the top edge;
2. I don't know how to invoke the Gamma function to plot the factorial on real numbers as suggested here, also I'm typesetting this document on Windows and my colleagues are on linux, so the solution has to be cross-platform (without using gnuplot).

This is what I've done so far:

\documentclass[border=2mm,tikz]{standalone}
\usepackage{tikz}
\usetikzlibrary{datavisualization}
\usetikzlibrary{datavisualization.formats.functions}
\pgfkeys{/pgf/number format/.cd, use comma, set thousands separator={\,}}

\begin{document}
\begin{tikzpicture}
\datavisualization [scientific axes=inner ticks,
style sheet=strong colors,
x axis={label={\textbf{\textsf{Elements}}}, length=8cm, min value=0, max value=100},
y axis={label={\textbf{\textsf{Operations}}}, length=5cm, min value=0, max value=2500},
data/format=function,
visualize as smooth line/.list={direct, logarithmic, linear, nlogarithmic, quadratic, exponential, factorial},
direct={label in legend={text=$\mathcal{O}(1)$}},
logarithmic={label in legend={text=$\mathcal{O}(\log n)$}},
linear={label in legend={text=$\mathcal{O}(n)$}},
nlogarithmic={label in legend={text=$\mathcal{O}(n \log n)$}},
quadratic={label in legend={text=$\mathcal{O}(n^2)$}},
exponential={label in legend={text=$\mathcal{O}(2^n)$}},
factorial={label in legend={text=$\mathcal{O}(n!)$}},
]
data [set=direct] {
var x : interval [0:100];
func y = 1;
}
data [set=logarithmic] {
var x : interval [1:100];
func y = ln(\value x);
}
data [set=linear] {
var x : interval [0:100];
func y = \value x;
}
data [set=nlogarithmic] {
var x : interval [1:100];
func y = (\value x) * ln(\value x);
}
var x : interval [0:50];
func y = \value x^2;
}
data [set=exponential] {
var x : interval [0:11];
func y = 2^(\value x);
}
data [set=factorial] {
var x : interval [0:7];
func y = factorial(\value x);
};
\end{tikzpicture}
\end{document}

• For your gamma function this answer (tex.stackexchange.com/questions/371255/…) might help. Additionally, you can use restrict y to domain=ymin:ymax in the axis environment to set the borders of your plot Commented Apr 15, 2021 at 17:25

I ditched TikZ datavisualization and I installed gnuplot on windows from here.

Then I arranged this answer for my needs.

The result is the following

\PassOptionsToPackage{dvipsnames}{xcolor}
\documentclass[tikz]{standalone}

\definecolor{direct}{HTML}{004518}
\definecolor{logarithmic}{HTML}{00A54F}
\definecolor{linear}{HTML}{C9DA2A}
\definecolor{nlogarithmic}{HTML}{FFC30D}
\definecolor{squared}{HTML}{F8931D}
\definecolor{cubed}{HTML}{F36523}
\definecolor{factorial}{HTML}{ED1B24}

\usepackage{pgfplots}
\pgfplotsset{samples = 200}

\usepackage[utf8]{inputenc}
\newcommand{\Omicron}{\mathcal{O}}

% arara: pdflatex: { shell: yes }
% arara: gnuplot: { input: [ bigO-complexity.pgf-plot.gnuplot ] }
% arara: clean: { extensions: [aux, log, pgf-plot.gnuplot, pgf-plot.table] }
\begin{document}

\begin{tikzpicture}[
mystyle/.style={above, style = {font=\large}}
]
\begin{axis}[
% grid = major,
clip = true, ticks = none,
width = 15cm, height = 10cm,
enlargelimits = false,
scale only axis = true,
every axis plot/.append style = {very thick},
axis line style = ultra thick,
clip mode = individual,
domain = 0:10,
restrict y to domain=0:10,
restrict x to domain=0:10,
axis x line = left, axis y line = left,
xmin = 0, xmax = 11,
ymin = 0, ymax = 11,
xlabel = {Elements}, ylabel = {Operations},
label style = {font=\large\bfseries\sffamily},
xlabel style = {at={(axis description cs:0.5,-0.05)}, anchor=south},
ylabel style = {at={(axis description cs:0.05,0.5)}, anchor=south},
]
\addplot[color=direct]       {1}                 node[mystyle]{$$\Omicron(1)$$};
\addplot[color=logarithmic]  {log2 x}            node[mystyle]{$$\Omicron(\log n)$$};
\addplot[color=linear]       {x}                 node[mystyle]{$$\Omicron(n)$$};
\addplot[color=squared]      {x^2}               node[mystyle]{$$\Omicron(n^2)$$};
\addplot[color=nlogarithmic] {x*(log2 x)}        node[mystyle]{$$\Omicron(n\log n)$$};
\addplot[color=cubed]        {x^3}               node[mystyle]{$$\Omicron(n^3)$$};
\addplot[color=factorial]    gnuplot{gamma(x+1)} node[mystyle, yshift=0.6cm]{$$\Omicron(n!)$$};
\end{axis}
\end{tikzpicture}
\end{document}

• Just as in the question/answer you refer to, you have the problem that the small interval is not suitable to show the fast growth of n! . I would think that the purpose for such a graph is to show non-technical people different growths and not leave them guessing weather n! or n^3 is growing faster. Commented Apr 17, 2021 at 18:00
• I have read the comment on the other question and I agree with you but I don't find a range that satisfies me, however I think the colors can help. @hpekristiansen Could you suggest a solution? Commented Apr 18, 2021 at 8:16
• The solution is simple - enlarge the domain. See you own very first picture as an example - it perfectly shows the different growths with light blue n! as a clear winner. Your colors and no numbers are better. Commented Apr 18, 2021 at 8:39