pgf plot and foreach loop

I have this function y=a*x^2 where a changes (-2.4,-2.1,-1.8,....,2.1,2.4) and i want to draw it with pgf plots. Problem is i dont know how to implement this i think i'll need foreach loop or something. I've found a solution but it's not what i want because i have to enter 17 functions manually like this:

\begin{tikzpicture}
\begin{axis}[grid=major, xmin=-5, xmax=5, ymin=-30, ymax=30, xlabel=$t$, ylabel=$y$];
\end{axis}
\end{tikzpicture}


To get graph

I want to replace this 17 functions with one where a changes in interval (-2.4,...,2.4) but don't know how. I'm new to latex.

Replace all the \addplot by

\foreach \a in {-2.4,-1.8,...,2.4}{
}
\foreach \a in {-2.1,-1.5,...,2.4}{
}


Code

\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}{
}
\foreach \a in {-2.1,-1.5,...,2.4}{
}
\end{axis}
\end{tikzpicture}


• +1. Or with \pgfplotsinvokeforach, one can say \begin{axis}[grid=major, xmin=-5, xmax=5, ymin=-30, ymax=30, xlabel=$t$, ylabel=$y$]; \pgfplotsinvokeforeach{-2.4,-1.8,...,2.4}{ \addplot [domain=-5:5, samples=100, color=cyan]{#1*x^2}; } \pgfplotsinvokeforeach{-2.1,-1.5,...,2.4}{ \addplot [domain=-5:5, samples=100, color=red,dashed]{#1*x^2}; } \end{axis} – Gonzalo Medina Dec 26 '14 at 22:35

Another way, using a single \foreach and a user-defined cycle list. This cycle list can then be used throughout your document if desired.

\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}{
}
\end{axis}
\end{tikzpicture}

\end{document}


Note: As pointed out by Kpym in a comment, \addplot suffices since I moved [domain=-5:5,samples=100] into the main axis environment options. (This is good practice anyway; to save typing, you should let the plots inherit as many keys as possible and override locally as needed.) If you have any local keys set (that is, \addplot[<local options>]), you must use \addplot+ for the cycle list to work properly within \foreach.

• why you put \addplot+ and not \addplot ? – Kpym Dec 27 '14 at 10:00
• @Kpym, good catch. It would be required for cycle lists to work with \foreach, but only if I had left [domain=-5:5,samples=100] in the options to \addplot. Now that I've moved those options to the axis environment, \addplot+ is no longer required. – Paul Gessler Dec 27 '14 at 14:15

Or with just one \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{%