How do I color in two different colors one square function?
In Metapost you could just wrap the colour specification in a if ... else ... fi
construct. Like this:
draw ... if odd i: dashed evenly withcolor marmelade else: withcolor sky fi;
Here's a complete version of your plot.
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.
There are some ways you can do it:
Same as Thruston's solution to this same question, lets you use
\ifodd
in TikZ as well. But we add a counter usingcount=\xi
(default starting from 1) so that it alternates between blue+solid and orange+dashed.You can set a
cycle list name
, defined here asmycycle
and there write your various style in order. Since we write two, they will alternate. Don't forget to write your command as\addplot+[...]
, note the+
and the optional arguments[...]
.
Here's the output, it's the same for all the solutions provided. The codes for the various solutions are provided below.
Output
Using \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}
Using 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}