10

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$];    
\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}

To get graph

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.

13

Replace all the \addplot by

\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};
}

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}{
\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}

enter image description here

  • 1
    +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
9

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}{
    \addplot{\a*x^2};
  }
\end{axis}
\end{tikzpicture}

\end{document}

enter image description here

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
7

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{%   
    \noexpand\addplot[domain=-5:5, samples=100,color=\Color,\Shape]{\Valor*x^2};
  }
  \temp
}
\end{axis}
\end{tikzpicture}

\end{document}

The result:

enter image description here

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.