我需要绘制多条曲线来说明运行时间,但其中一些曲线不起作用,而且我不知道如何在图中命名这些曲线。
\documentclass{standalone}
\usepackage{pgfplots}
\tikzset{>=stealth}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=10,
ymin=0,ymax=10,
axis lines=center,
axis line style=->]
\addplot[->] expression[domain=0:10,samples=100]{x^(2)} ;
\addplot[->] expression[domain=0:10,samples=100]{\sqrt(x)} ;
\addplot[->] expression[domain=0:10,samples=100]{x!} ;
\end{axis}
\end{tikzpicture}
\end{document}
我仍然需要绘制常数、对数和指数,但现在我如何让代码工作,以及如何在每条曲线旁边显示名称?(解释所有这些的额外材料也会有所帮助)
答案1
您可以在这里看到开始使用 pgfplots 所需要了解的内容:
\documentclass{standalone}
\usepackage{pgfplots}
\tikzset{>=stealth}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
xmin=0,xmax=6.9,
ymin=0,ymax=30,
axis lines=center,
axis line style=->]
\addplot[dashed,red] expression[domain=0:5,samples=100]{(x)^2} ; \addlegendentry{$x^2$}
\addplot[-,green] expression[domain=0:5,samples=100]{sqrt(x)} ; \addlegendentry{$\sqrt{x}$}
\addplot[dotted,blue] expression[domain=0:5,samples=100]{x!} ; \addlegendentry{$x!$}
\end{axis}
\end{tikzpicture}
\end{document}
正如@Stefan Pinnow 所说,域对于阶乘来说太大了,所以我必须改变它......
结果如下:
答案2
不幸的是,你在问题中并没有非常精确地说明你真正想要实现的目标,所以在我的回答中,我假设你想坚持给定的轴限制。
正如问题下方的评论中所述,如果不改变“某些东西”而只用 进行纠正,\sqrt(x)
您sqrt(x)
会得到“维度过大”的错误,因为x!
函数产生的值的大小为10^6
,而轴极限为10
。
也不清楚如何你想要“命名曲线”。如果你指的是添加图例,那么请查看koleygr 的答案。如果你想添加名字在图表本身,这就是我在这里展示的内容。我在这里也使用了“硬”情况,在“图表末尾”添加名称,即 at pos=1
。如果标签应该出现在“图表中间”的某个地方,请随意pos
向标签node
s 添加相应的语句。
有关更多详细信息,请查看代码中的注释。(如果还有不清楚的地方,请随时询问。)
% used PGFPlots v1.15
\documentclass[border=5pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{
/tikz/>=stealth,
% use this `compat' level or higher to use Lua for calculations
% (this is not required though)
compat=1.12,
% ---------------------------------------------------------------------
% please skip this block for now
% (copied from <https://tex.stackexchange.com/a/375348>)
/pgf/declare function={
% declare the main function(s)
f(\x) = sqrt(\x);
% state (or calculate) the lower and upper boundaries (the domain values)
lb = 0;
ub = 10;
%
% ---------------
%%% nonlinear spacing: <https://stackoverflow.com/a/39140096>
% "non-linearity factor"
a = 0.4;
% function to use for the nonlinear spacing
Y(\x) = exp(a*\x);
% rescale to former limits
X(\x) = (Y(\x) - Y(lb))/(Y(ub) - Y(lb)) * (ub - lb) + lb;
},
% ---------------------------------------------------------------------
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
% use the above defined variables here as well, so you only have to
% change them in one place, if you would like to change these values
xmin=lb,xmax=ub,
ymin=0,ymax=10,
axis lines=center,
axis line style=->,
% move common options here
% also use the above defined variables here
domain=lb:ub,
% you don't want to show markers
no markers,
% show the plots "smoothly"
smooth,
% because the label nodes will be outside the axis "rectangle" they
% would be clipped, so we disable clipping
clip=false,
]
% % Using these two functions have some downsides ...
% \addplot+ [domain=0:4] {x^2};
% \addplot {sqrt(x)};
% Instead of increasing the number of samples "to make it look good",
% we use nonlinear sampling instead.
% (For more details have a look at <https://tex.stackexchange.com/a/375348>)
\addplot ({X(x)},{f(X(x))})
% add a label (node)
% (the default position is at `pos=1.0`)
node [right] {$\sqrt{x}$}
;
% Because $x^2$ is the inverse function of $\sqrt{x}$ we simply
% exchange the $x$ and $y$ arguments of the previous `\addplot'.
% By doing that we also avoid that the function looks bad and we don't
% have to increase the number of `samples'
\addplot ({f(X(x))},{X(x)})
node [above]{$x^2$}
;
% Here we also use the minimalistic approach.
\addplot+ [
% Because values only change at integer x values, we can use
% `const plot'.
const plot,
% $4!$ already yields $24$ and so we don't need a higher max
% `domain' value.
domain=0:4,
% Use a number of samples that assures that also samples are at
% integer x values to give a right result. This makes $5$ at minimum.
samples=5,
% So the plot stops at $y = 10$ use the following key--value.
% (Otherwise, because we use `clip=false' the line would be drawn
% up to 24.)
restrict y to domain*=0:10,
] {x!}
node [above] {$x!$}
;
\end{axis}
\end{tikzpicture}
\end{document}
答案3
也许您对对数-对数图感兴趣?
输出
残酷阴谋
受限y
域
和\addplot+[mark=none,const plot,restrict y to domain=0:42] {x!};
仅显示exp(0)=1
和之间的值exp(42)~10^17
。
我认为这足以说明相对增长的观点。
实际上:设置ymax
更好。
代码
残酷阴谋
\documentclass[12pt,tikz,border=0pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}
[
%domain=2:30,
ymin=1,
axis lines = center,
samples at ={1,2,...,100},
grid=both,
legend entries={$y=x!$,$y=x^2$,$y=\sqrt{x}$},
every axis legend/.append style=
{
at={(current axis.above origin)},
anchor=north west, xshift=1cm, yshift=-1mm,
draw=none,
cells={anchor=west},
%font=\scriptsize,
}
]
\addplot+[mark=none,const plot] {x!};
\addplot+[mark=none] {x^2};
\addplot+[mark=none] {sqrt(x)};
\end{loglogaxis}
\end{tikzpicture}
\end{document}
受限域
\documentclass[12pt,tikz,border=0pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.14}
\begin{document}
\begin{tikzpicture}
\begin{loglogaxis}
[
height=13cm,
ymin=1,
ymax=exp(40),
hide obscured x ticks=false,
hide obscured y ticks=false,
axis lines = center,
samples at ={1,2,...,100},
grid=both,
legend entries={$y=x!$,$y=\exp(x)$,$y=x^2$,$y=\sqrt{x}$,$y=\ln(x)$},
every axis legend/.append style=
{
at={(current axis.above origin)},
anchor=north west, xshift=3mm, yshift=-1mm,
draw=none,
cells={anchor=west},
%font=\scriptsize,
},
]
\addplot+[mark=none,const plot,] {x!};
\addplot+[mark=none] {exp(x)};
\addplot+[mark=none] {x^2};
\addplot+[mark=none] {sqrt(x)};
\addplot+[mark=none] {ln(x)};
\end{loglogaxis}
\end{tikzpicture}
\end{document}