4

http://i.imgur.com/Eul9nq0.png?1

In answering this question I actually caught myself unknowing how to solve the following issue.

In the code below I'm dealing with many graphs, which I intend to gradually change in color based on how many graphs I'm using. The answer doesn't need to be fully automatic (though it would be nice), but I ended up being unable to iteratively, gradually change the color of each consecutive plot.

I tried a few things like:

\foreach \w in {.05,.1,...,1}
{
\pgfmathparse{100*\w}
\xdef\x{\pgfmathresult}
\addplot[blue!\x!white] {(4.9/(\w^2))*(cosh(\w*x)-cos(\w*x))};
}

Result: the code compiles, but it doesn't use the right color for each plot.

I also tried using \w as a direct argument to the optional argument of addplot, but it didn't really do anything,

\foreach \w in {.05,.1,...,1}
{
\addplot[blue!\w!white] {(4.9/(\w^2))*(cosh(\w*x)-cos(\w*x))};
}

I could also somehow try to append more colors with each foreach loop to pgfplots's cyclelist (I think), but I couldn't find any command or code to do so.

So I'd like ask: how to avoid the necessary 20 lines long pgfplotscyclelist and write this code more efficiently?

\documentclass{standalone}
\usepackage{pgfplots}

\def\mycolone{yellow}
\def\mycoltwo{green}

\pgfplotscreateplotcyclelist{mycolorlist}{%
\mycolone!95!\mycoltwo\\%
\mycolone!90!\mycoltwo\\%
\mycolone!85!\mycoltwo\\%
\mycolone!80!\mycoltwo\\%
\mycolone!75!\mycoltwo\\%
\mycolone!70!\mycoltwo\\%
\mycolone!65!\mycoltwo\\%
\mycolone!60!\mycoltwo\\%
\mycolone!55!\mycoltwo\\%
\mycolone!50!\mycoltwo\\%
\mycolone!45!\mycoltwo\\%
\mycolone!40!\mycoltwo\\%
\mycolone!35!\mycoltwo\\%
\mycolone!30!\mycoltwo\\%
\mycolone!25!\mycoltwo\\%
\mycolone!20!\mycoltwo\\%
\mycolone!15!\mycoltwo\\%
\mycolone!10!\mycoltwo\\%
\mycolone!5!\mycoltwo\\%
}

%\pgfplotsset{every axis legend/.append style={
%at={(.5,-.2)},
%anchor=north}} 

\begin{document}
\begin{tikzpicture} 
\begin{axis}[xmin=-5,xmax=5,ymin=-0.5,ymax=100,no markers, grid=both,cycle list name=mycolorlist]
\foreach \w in {.05,.1,...,1} {
\addplot {(4.9/(\w^2))*(cosh(\w*x)-cos(\w*x))};
%\edef\legendentry{\noexpand\addlegendentry{$\omega =\noexpand\pgfmathprintnumber[fixed,fixed zerofill, precision=2]{\w}$}};
%       \legendentry
}
\end{axis}
\end{tikzpicture}
\end{document}
1010011010
  • 6,357

1 Answers1

5
\documentclass{standalone}
\usepackage{pgfplots}

\def\mycolone{yellow}
\def\mycoltwo{green}

%\pgfplotsset{every axis legend/.append style={
%at={(.5,-.2)},
%anchor=north}} 

\begin{document}
\begin{tikzpicture} 
\begin{axis}[xmin=-5,xmax=5,ymin=-0.5,ymax=100,no markers, grid=both]
\foreach \w in {5,10,...,100} {
\edef\tmp{\noexpand\addplot[\mycolone!\w!\mycoltwo]}
\tmp{(4.9/((\w/100)^2))*(cosh(\w*x/100)-cos(\w*x/100))};
%\edef\legendentry{\noexpand\addlegendentry{$\omega =\noexpand\pgfmathprintnumber[fixed,fixed zerofill, precision=2]{\w}$}};
%       \legendentry
}
\end{axis}
\end{tikzpicture}
\end{document}
David Carlisle
  • 757,742