我對 tikz 和 pgf 很陌生。我正在繪製 Big O 時間複雜度,在查看了足夠的範例之後,我已經能夠準確地創建我想要的圖表(階乘除外)。簡單地繪製x!創建這個奇怪的階梯圖。我希望它是一條平滑的曲線。我發現這個問題其中有一個使用的答案semilogyaxis
。然而,簡單地切換到這個並沒有幫助,階乘圖看起來是一樣的。我還嘗試按照問題中的範例答案繪製一個全新的圖,它確實重疊,但階乘圖看起來不正確,我認為它與對數 y 軸有關,我不確定如何調整座標。我只需要類似的東西f(x) = x!
應該產生如下圖:
這是我迄今為止所擁有的 MWE:
\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid = major,
clip = true,
ticks = none,
width=0.8\textwidth,
height=0.6\textwidth,
every axis plot/.append style={very thick},
axis line style = ultra thick,
clip mode=individual,
restrict y to domain=0:10,
restrict x to domain=0:10,
axis x line = left,
axis y line = left,
domain = 0.00:10,
xmin = 0,
xmax = 11,
ymin = 0,
ymax = 11,
xlabel = n,
ylabel = no. of operations,
xlabel style = {at={(axis description cs:0.5,-0.1)},anchor=south},
ylabel style = {at={(axis description cs:-0.08,0.5)},anchor=north},
label style = {font=\LARGE\bf},
]
\addplot [
samples=100,
color=red,
]
{x^2}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n^2)$};
\addplot [
samples=100,
color=blue,
]
{x}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n)$};
\addplot [
samples=100,
color=orange,
]
{log2 x}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(\log{}n)$};
\addplot [
samples=100,
color=black,
]
{x*(log2 x)}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n\log{}n)$};
\addplot [
samples=100,
color=magenta,
]
{1}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(1)$};
\addplot [
samples=100,
color=cyan,
]
{x^3}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n^3)$};
%Creates stair-step like plot
\addplot [
samples=100,
color=green,
]{x!}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n!)$};
\end{axis}
\end{tikzpicture}
\end{document}
產生以下劇情:
答案1
我重新創建了 @haver 解決方案https://tex.stackexchange.com/a/520121/8650帶有 OP 代碼的 Gamma 函數 - 幫助人們搜尋連續階乘。實階乘僅針對整數定義 - 請參閱https://en.wikipedia.org/wiki/階乘。x! = Γ(x + 1)
該解決方案需要gnuplot
並且--shell-escape
:
\documentclass{article}
\usepackage[margin=0.5in]{geometry}
\usepackage[utf8]{inputenc}
\usepackage{pgfplots}
\pgfplotsset{width=10cm,compat=1.9}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid = major,
clip = true,
ticks = none,
width=0.8\textwidth,
height=0.6\textwidth,
every axis plot/.append style={very thick},
axis line style = ultra thick,
clip mode=individual,
restrict y to domain=0:10,
restrict x to domain=0:10,
axis x line = left,
axis y line = left,
domain = 0.00:10,
xmin = 0,
xmax = 11,
ymin = 0,
ymax = 11,
xlabel = n,
ylabel = no. of operations,
xlabel style = {at={(axis description cs:0.5,-0.1)},anchor=south},
ylabel style = {at={(axis description cs:-0.08,0.5)},anchor=north},
label style = {font=\LARGE\bf},
]
\addplot [
samples=100,
color=red,
]
{x^2}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n^2)$};
\addplot [
samples=100,
color=blue,
]
{x}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n)$};
\addplot [
samples=100,
color=orange,
]
{log2 x}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(\log{}n)$};
\addplot [
samples=100,
color=black,
]
{x*(log2 x)}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n\log{}n)$};
\addplot [
samples=100,
color=magenta,
]
{1}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(1)$};
\addplot [
samples=100,
color=cyan,
]
{x^3}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n^3)$};
%Creates stair-step like plot
\addplot [
samples=100,
color=green,
]{x!}node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n!)$};
\addplot [
samples=100,
color=green,
] gnuplot{gamma(x+1)} node[above,pos=1,style={font=\Large}]{$\mathcal{O}(n!)$};
\end{axis}
\end{tikzpicture}
\end{document}