在 TikZ 数据可视化中绘制阶乘函数

在 TikZ 数据可视化中绘制阶乘函数

我正在尝试复制这张图按照以下提示此解决方案TikZ 数据可视化包(见第六部分)

[编辑]相关问题:如何制作一个显示函数增长但带有缩放的 y 轴的图形?

这是所需的输出:

期望输出

奖金:如果可以复制就太好了这张图添加彩色区域。

这是我的输出:

实际产量

我有两个问题不知道如何解决:

  1. x我必须手动计算函数的停止值x^2,否则2^n它们x!会从顶部边缘溢出;
  2. 我不知道如何调用Gamma函数来绘制实数的阶乘正如这里建议的那样,另外我在 Windows 上排版此文档而我的同事使用 Linux,因此解决方案必须是跨平台的(不使用gnuplot)。

这是我目前所做的:

\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);
}
data [set=quadratic] {
    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}

答案1

我放弃了 TikZ 数据可视化,并在 Windows 上安装了 gnuplot从这里

然后我安排这个答案满足我的需要。

结果如下

在此处输入图片描述

\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}

相关内容