我想要绘制y = x^n
这样n = 1,2,3,4,5
的x in [0,1]
和y in [0,1]
。
类似这样的图片:
当我想设置xmin
和xmax
限制域时,出现错误!哪里错了!
\documentclass{standalone}
\usepackage{tikz}
\usepackage[customcolors]{hf-tikz}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[xmin = 0, ymin = 0, xmax = 1, ymax = 1]
\addplot[color=red,smooth] {x};
\addplot[color=red,smooth] {x^2};
\addplot[color=red,smooth] {x^3};
\addplot[color=red,smooth] {x^4};
\end{axis}
\end{tikzpicture}
\end{document}
答案1
另一种方法是\pgfplotsinvokeforeach
重复\addplot
:
\documentclass[tikz,margin=2mm]{standalone}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[domain=0:1,xmin=0,xmax=1,ymin=0,ymax=1]
\pgfplotsinvokeforeach{1,2,...,15}{
\addplot[red,smooth] {x^#1};
}
\end{axis}
\end{tikzpicture}
\end{document}
看起来更像你想要的情节:
和:
\documentclass[tikz,margin=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
domain=0:1,
axis equal,
width=8cm,height=8cm,
xmin=-0.05,xmax=1.05,
ymin=-0.05,ymax=1.05,
axis y line=middle,
axis x line=middle,
xtick=\empty,
ytick=\empty,
]
\pgfplotsinvokeforeach{1,...,20}{
\addplot[red,smooth] {x^#1};
}
\draw[dashed] (1,0) -- (1,1);
\draw[dashed] (0,1) -- (1,1);
\end{axis}
\end{tikzpicture}
\end{document}
最后,来看一个有趣的动画:
和:
\documentclass[tikz,margin=2mm]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=newest}
\begin{document}
\foreach \numPowers in {1,...,20}{
\begin{tikzpicture}
\begin{axis}[
domain=0:1,
axis equal,
width=8cm,height=8cm,
xmin=-0.05,xmax=1.05,
ymin=-0.05,ymax=1.05,
axis y line=middle,
axis x line=middle,
xtick=\empty,
ytick=\empty,
]
\pgfplotsinvokeforeach{1,...,\numPowers}{
\addplot[red,smooth] {x^##1};
}
\draw[dashed] (1,0) -- (1,1);
\draw[dashed] (0,1) -- (1,1);
\end{axis}
\end{tikzpicture}
}
\end{document}
如果有人可以评论为什么当它包含在普通中时我需要##1
而不是#1
在中,我将非常感激。\pgfplotsinvokeforeach
\foreach
答案2
我希望这有帮助
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
{
\pgfplotsset{xmin=0, xmax=1, ymin=0, ymax=1}
\begin{tikzpicture}
\begin{axis}[domain=0:1]
\addplot[color=red, smooth] {x};
\addplot[color=blue, smooth] {x^2};
\addplot[color=green, smooth] {x^3};
\addplot[color=yellow, smooth] {x^4};
\end{axis}
\end{tikzpicture}
}
\end{document}
这将给你