3

I have this pgfplots plot and I want every single graph to have a different color. A gradient from red to blue or something else (m=2 should be red, m=11 should be blue and all the other m should be between this).

I need this to work even when I change the amount of graphs.

I already tried to transfer the code lines from this example, but I can't get it working: Plotting a graph with several values of a parameter

The minimum working example is added below.

Thank you.

enter image description here

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{pgfplots}    
\pgfplotsset{compat=newest}

\pgfplotsset{
    gainplot/.style={
        axis x line*=box,
        xmax=10,
        xmin=0.1,
        xmode=log,
        width=14cm,
        xlabel=$F_\textup{x}$,
        xticklabel style={yshift=-0.2cm,},
        xtick={0.1,1.0,10.0},
        xticklabels={{$0.1$},{$1.0$},{$10.0$}},
        axis y line*=box,
        ymax=3,
        ymin=0,
        ymode=normal,
        height=7cm,
        ylabel=$K$,
        yticklabel style={xshift=-0.2cm,},
        ytick={0,1,2,3 },
        yticklabels={{$0$},{$1$},{$2$},{$3$}},
        grid = both,
        grid style={line width=0.2pt,},
        legend style={
            font=\scriptsize,
            at={(0.5,1.03)},
            anchor=south,
            draw=none,
        },
        legend columns=5,
    },
    gainplot/.belongs to family=/pgfplots/scale,
}

\pgfmathdeclarefunction{gaincurve}{2}{%
    \pgfmathparse{%
        (x^2*(#2-1)/(sqrt((#2*x^2-1)^2+x^2*(x^2-1)^2*(#2-1)^2*#1^2)))
    }%
}

\tikzstyle{gaincurvestyle}=[
    smooth,
    %thick,
    mark=none,
    domain=0.1:10,
    samples=100,
]


\begin{document}

\begin{figure}[]
\centering

\begin{tikzpicture}
\begin{axis}[gainplot]
\foreach \m in {2,3,...,11}{
    \addplot[gaincurvestyle,red]{gaincurve(0.2,\m)}; %%% Here help is needed.
    \addlegendentryexpanded{$m=\m$}
}
\end{axis}
\end{tikzpicture}

\caption{Placeholder.}
\end{figure}

\end{document}
1
4

You can use the \edef trick mentioned in the pgfplots manual, combined with evaluate in the \foreach, and the red!<value>!blue color syntax.

If you want to normalize based on the values of the loop, you can use

\foreach [evaluate=\m as \redfrac using (\m-<minimum>)*100/(<maximum>-<minimum>)] ...

and then set the color of the plot to red!\redfrac!blue. For example, if the lowest value in the loop is 2 and the highest is 11, as in your example, you use (\m-2)*100/(11-2).

If you instead want to normalize based on the index of the loop, you can do

\foreach [count=\i from 0,evaluate=\i as \redfrac using \i*100/<number of elements in list>)] ...

For example, if you're looping over a list of 15 values, use \i*100/15. In both cases, it's just a matter of normalizing a value so that it runs from 0 to 100.

enter image description here

\documentclass[12pt]{article}
\usepackage{amsmath}
\usepackage{amsfonts}
\usepackage{amssymb}
\usepackage{graphicx}
\usepackage{pgfplots}    
\pgfplotsset{compat=newest}

\pgfplotsset{
    gainplot/.style={
        axis x line*=box,
        xmax=10,
        xmin=0.1,
        xmode=log,
        width=14cm,
        xlabel=$F_\textup{x}$,
        xticklabel style={yshift=-0.2cm,},
        xtick={0.1,1.0,10.0},
        xticklabels={{$0.1$},{$1.0$},{$10.0$}},
        axis y line*=box,
        ymax=3,
        ymin=0,
        ymode=normal,
        height=7cm,
        ylabel=$K$,
        yticklabel style={xshift=-0.2cm,},
        ytick={0,1,2,3 },
        yticklabels={{$0$},{$1$},{$2$},{$3$}},
        grid = both,
        grid style={line width=0.2pt,},
        legend style={
            font=\scriptsize,
            at={(0.5,1.03)},
            anchor=south,
            draw=none,
        },
        legend columns=5,
    },
    gainplot/.belongs to family=/pgfplots/scale,
}

\pgfmathdeclarefunction{gaincurve}{2}{%
    \pgfmathparse{%
        (x^2*(#2-1)/(sqrt((#2*x^2-1)^2+x^2*(x^2-1)^2*(#2-1)^2*#1^2)))
    }%
}

\tikzstyle{gaincurvestyle}=[
    smooth,
    %thick,
    mark=none,
    domain=0.1:10,
    samples=100,
]


\begin{document}

\begin{figure}[]
\centering

\begin{tikzpicture}
\begin{axis}[gainplot]
\foreach [evaluate=\m as \redfrac using (\m-2)*100/(11-2)] \m in {2,3,...,11}{
    \edef\temp{\noexpand\addplot[gaincurvestyle,red!\redfrac!blue]{gaincurve(0.2,\m)};
    \noexpand\addlegendentry{$m=\m$}}
    \temp
}
\end{axis}
\end{tikzpicture}

\caption{Placeholder.}
\end{figure}

\end{document}
16
  • Swap red and blue in the \addplot to change the direction of the gradient. Feb 14 '17 at 12:04
  • I have a question yet again. In your solution the color gradient depends on the value of \m. But if I have something like this {0.1,0.2,0.3,0.4,0.6, 0.8,1.0,1.5,2.0,3.0,5.0} for \m, all graphs are almost blue. Just a little red is visible. Feb 14 '17 at 12:38
  • Is it possible, that the color gradient depends on the amount of graphs? So the first graph is blue and the last one is red. I think I would call it fixed step width (ignoring the values). Feb 14 '17 at 12:39
  • 1
    @Benjamin1956 Ah, just not me thinking clearly. You'd want to normalize based on the index, not the value. So with 15 values in the loop, [count=\i from 0,evaluate=\i as \redfrac using \i*100/15)] Feb 14 '17 at 12:56
  • 1
    @Diaa For each step in the loop, the expression (m-2)*etc. is evaluated, and the result stored in \redfrac. Aug 27 '19 at 5:37

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.