答案1
有几种方法可以实现此目的:
与...一样Thruston 的解决方案对于这个问题,您也可以
\ifodd
在 TikZ 中使用。但我们添加了一个计数器count=\xi
(默认从 1 开始),以便它在蓝色+实线和橙色+虚线之间交替。您可以设置一个
cycle list name
,在此处将其定义为 ,mycycle
然后按顺序编写各种样式。由于我们编写了两个,因此它们将交替出现。不要忘记将您的命令编写为\addplot+[...]
,注意+
和可选参数[...]
。
这是输出,对于提供的所有解决方案都是相同的。下面提供了各种解决方案的代码。
输出
使用\ifodd
\documentclass[tikz,margin=15pt]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.12}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
grid=major,
xmin=-5,xmax=5,
ymin=-20,ymax=20,
xlabel=$x$,
ylabel=$y$
]
\foreach \a [count=\xi] in {-2.4,-2.1,...,2.4}
{%
\ifodd\xi
\addplot[blue, line width=.5pt] plot expression {\a*(\x^2)-3*\x +cos(90*\x)};
\else
\addplot[orange, dashed, line width=.5pt] plot expression {\a*(\x^2)-3*\x+cos(90*\x)};
\fi
}
\end{axis}
\end{tikzpicture}
\end{document}
使用cycle list name
\documentclass[tikz,margin=15pt]{standalone}
\usepackage{pgfplots}
\pgfplotscreateplotcyclelist{mycycle}{%
{blue},
{dashed,orange}% <--- % must be there for this to work properly!
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[%
grid=major,
xmin=-5,xmax=5,
ymin=-20,ymax=20,
xlabel=$x$,
ylabel=$y$,
cycle list name=mycycle
]
\foreach \a in {-2.4,-2.1,...,2.4}
{%
\addplot+[smooth,line width=.5pt] plot expression {\a*(\x^2)-3*\x +cos(90*\x)};
}
\end{axis}
\end{document}
答案2
在元帖子你可以将颜色规范包装在一个if ... else ... fi
构造中。像这样:
draw ... if odd i: dashed evenly withcolor marmelade else: withcolor sky fi;
这是您的情节的完整版本。
prologues:=3;
outputtemplate := "%j%c.eps";
beginfig(1);
vardef f(expr a,x) = a*(x*x)-3x+cosd(90x) enddef;
numeric u, v, s, a, xmin, xmax, ymin, ymax;
-xmin = xmax = 5; u = 1cm;
-ymin = ymax = 20; v = 2mm;
s = 1/8;
color marmelade, sky;
marmelade = 0.67(red + 1/2 green);
sky = 0.54[blue,white];
for i=-8 upto 8:
a := 0.3i;
draw ((xmin,f(a,xmin))
for x = xmin+s step s until xmax:
-- (x,f(a,x))
endfor)
xscaled u yscaled v
if odd i: dashed evenly withcolor marmelade else: withcolor sky fi;
endfor
clip currentpicture to ( (xmin,ymin) -- (xmax,ymin) -- (xmax, ymax) -- (xmin,ymax) -- cycle) xscaled u yscaled v;
path xx, yy, bar;
xx = ((xmin,0)--(xmax,0)) scaled u;
yy = ((0,ymin)--(0,ymax)) scaled v;
defaultfont := "texnansi-lmr10";
string minus_sign; minus_sign = char 143;
for i=-20 step 10 until 20:
bar := xx shifted (0,i*v);
draw bar if i<>0: withcolor .7 white fi;
label.lft(if i<0: minus_sign & fi decimal abs(i), point 0 of bar);
endfor
for i= -4 step 2 until 4:
bar := yy shifted (i*u,0);
draw bar if i<>0: withcolor .7 white fi;
label.bot(if i<0: minus_sign & fi decimal abs(i), point 0 of bar);
endfor
setbounds currentpicture to bbox currentpicture scaled 1.03;
endfig;
end.