我有这个函数 y=a*x^2,其中 a 变化(-2.4、-2.1、-1.8、....、2.1、2.4),我想用 pgf 图绘制它。问题是我不知道如何实现它,我想我需要 foreach 循环或类似的东西。我找到了一个解决方案,但这不是我想要的,因为我必须手动输入 17 个函数,如下所示:
\begin{tikzpicture}
\begin{axis}[grid=major, xmin=-5, xmax=5, ymin=-30, ymax=30, xlabel=$t$, ylabel=$y$];
\addplot [domain=-5:5, samples=100, color=cyan,]{-2.4*x^2};
\addplot [domain=-5:5, samples=100, color=red,dashed]{-2.1*x^2};
\addplot [domain=-5:5, samples=100, color=cyan]{-1.8*x^2};
\addplot [domain=-5:5, samples=100, color=red,dashed]{-1.5*x^2};
\addplot [domain=-5:5, samples=100, color=cyan]{-1.2*x^2};
\addplot [domain=-5:5, samples=100, color=red,dashed]{-0.9*x^2};
\addplot [domain=-5:5, samples=100, color=cyan]{-0.6*x^2};
\addplot [domain=-5:5, samples=100, color=red,dashed]{-0.3*x^2};
\addplot [domain=-5:5, samples=100, color=cyan]{0*x^2};
\addplot [domain=-5:5, samples=100, color=red,dashed]{0.3*x^2};
\addplot [domain=-5:5, samples=100, color=cyan,]{0.6*x^2};
\addplot [domain=-5:5, samples=100, color=red,dashed]{0.9*x^2};
\addplot [domain=-5:5, samples=100, color=cyan,]{1.2*x^2};
\addplot [domain=-5:5, samples=100, color=red,dashed]{1.5*x^2};
\addplot [domain=-5:5, samples=100, color=cyan,]{1.8*x^2};
\addplot [domain=-5:5, samples=100, color=red,dashed]{2.1*x^2};
\addplot [domain=-5:5, samples=100, color=cyan,]{2.4*x^2};
\end{axis}
\end{tikzpicture}
获取图表
我想用一个间隔变化的函数(-2.4,...,2.4)替换这 17 个函数,但不知道如何替换。我是 Latex 新手。
答案1
全部替换\addplot
为
\foreach \a in {-2.4,-1.8,...,2.4}{
\addplot [domain=-5:5, samples=100, color=cyan]{\a*x^2};
}
\foreach \a in {-2.1,-1.5,...,2.4}{
\addplot [domain=-5:5, samples=100, color=red,dashed]{\a*x^2};
}
代码
\begin{tikzpicture}
\begin{axis}[grid=major, xmin=-5, xmax=5, ymin=-30, ymax=30, xlabel=$t$, ylabel=$y$];
\foreach \a in {-2.4,-1.8,...,2.4}{
\addplot [domain=-5:5, samples=100, color=cyan]{\a*x^2};
}
\foreach \a in {-2.1,-1.5,...,2.4}{
\addplot [domain=-5:5, samples=100, color=red,dashed]{\a*x^2};
}
\end{axis}
\end{tikzpicture}
答案2
另一种方法是使用单个\foreach
和用户定义的cycle list
。cycle list
如果需要,可以在整个文档中使用它。
\documentclass{article}
\usepackage{pgfplots}
\pgfplotscreateplotcyclelist{mylist}{{color=cyan},{color=red,dashed}} % can now be used throughout the document
\begin{document}
\begin{tikzpicture}
\begin{axis}[
grid=major,
xmin=-5, xmax=5,
ymin=-30, ymax=30,
xlabel=$t$, ylabel=$y$,
domain=-5:5,
samples=100,
cycle list name=mylist
]
\foreach \a in {-2.4,-2.1,...,2.4}{
\addplot{\a*x^2};
}
\end{axis}
\end{tikzpicture}
\end{document}
注意:正如 Kpym 在评论中指出的那样,\addplot
自从我进入[domain=-5:5,samples=100]
主axis
环境选项以来, 就足够了。(无论如何,这是一个很好的做法;为了节省输入,您应该让图继承尽可能多的键,并根据需要在本地覆盖。)如果您设置了任何本地键(即\addplot[<local options>]
),则必须使用\addplot+
才能cycle list
在 内正常工作\foreach
。
答案3
或者只用一个\foreach
:
\documentclass{article}
\usepackage{pgfplots}
\begin{document}
\begin{tikzpicture}
\begin{axis}[grid=major, xmin=-5, xmax=5, ymin=-30, ymax=30, xlabel=$t$, ylabel=$y$]
\foreach \Valor [count=\Cont] in {-2.4,-2.1,...,2.4}
{
\ifodd\Cont\relax
\def\Color{cyan}
\def\Shape{}
\else
\def\Color{red}
\def\Shape{dashed}
\fi
\edef\temp{%
\noexpand\addplot[domain=-5:5, samples=100,color=\Color,\Shape]{\Valor*x^2};
}
\temp
}
\end{axis}
\end{tikzpicture}
\end{document}
结果: