# How do I color in two different colors one square function?

I need to color every other line of this same function in orange and make it dashed, but this is only one square function, so can anyone help me?

-
Can you copy that part of the code from your document and edit your question to add it? – percusse Jan 6 at 12:56
there you go, that's the function I need to draw with tikz, and I need to have every second line of the function orange dashed – Denis Muratovic Jan 6 at 13:38
@DenisMuratovic: It would be great if you could add the code as copyable text, not as an image, so people don't have to re-type the code by hand if they want to work on an answer. – Jake Jan 6 at 13:46
Copy as in paste the text, not the image. :) Also, post the full MWE (minimal code necessary to create that image), not just the snippet. – Alenanno Jan 6 at 13:50
This seems to be part of an assignment. Out of interest, can you link the course material? – Johannes_B Jan 11 at 14:29

There are some ways you can do it:

1. Same as Thruston's solution to this same question, lets you use \ifodd in TikZ as well. But we add a counter using count=\xi (default starting from 1) so that it alternates between blue+solid and orange+dashed.

2. You can set a cycle list name, defined here as mycycle 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.

## 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}

-
Sorry guys for uploading only an image, first time here asking questions, next time I will put the text of code and thank you very much, im new to latex and still trying to figure out all possibilities, and again, thank you very much. – Denis Muratovic Jan 6 at 15:10
@DenisMuratovic No problem. :D – Alenanno Jan 6 at 15:11
@Alenanno is there any way to "inline" the if construct? So that you did not need to repeat the long and complex expression? – Thruston Jan 6 at 17:30
@Thruston Let me look into it. – Alenanno Jan 6 at 17:36
@Thruston Added one alternative solution. I'm working on another one, but it refuses to work. :D – Alenanno Jan 6 at 23:36

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;

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.

-
PS. I know the question is about pgfplots, but I thought it would be useful to show how it could be done in MP. Plain MP gives you lots of flexibility, but you have to do all the things like the grid and the axes from scratch. You pays your money, and you takes your choice. – Thruston Jan 6 at 14:28
+1 for Beatles reference ;) – Huugo Jan 6 at 16:43
@Huugo Where is the reference? The colors? – Alenanno Jan 6 at 16:46
Check the color definitions @Alenanno – Huugo Jan 6 at 16:46
@Huugo Lucy in the sky with diamonds? – Alenanno Jan 6 at 16:50