3

I'm trying to draw 10 functions of ten different colors (actually starting from red and ending with yellow) inside a tikzpicture environment. Problem is that the code reads only the first color (red) and then plot all 10 functions in green Here's the code:

\documentclass{amsart}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage[a4paper, margin=2.5cm]{geometry}
\usepackage{pgfplots}
\usepackage{xcolor}
\pgfplotsset{compat=1.18}
\usepackage{mdframed}
\usepackage{subcaption}

\begin{document}

\begin{figure}[htb]
\begin{mdframed} % un solo riquadro esterno per coerenza visiva
  \centering
  % --- PRIMA SUBFIGURE ---
  \begin{subfigure}[t]{0.45\textwidth}
    \centering
    \begin{tikzpicture}
      \begin{axis}[
        axis lines=none,
        grid=none,
        width=\linewidth,
        height=0.8\linewidth, % stessa altezza per entrambi
        xmin=-1, xmax=4,
        ymin=-2, ymax=3,
        samples=200,
        domain=0:3.1416
      ]
        \addplot[thick,smooth]{2*sin(deg(x))};
        \addplot[only marks, mark=*] coordinates {(0,0)};
        \addplot[only marks, mark=*] coordinates {(3.1416,0)};
        \node[below left]  at (axis cs:0,0) {$x_0$};
        \node[below right] at (axis cs:3.1416,0) {$x_1$};
        \node[below] at (axis cs:1.5,1.9) {$\gamma(t)$};
      \end{axis}
    \end{tikzpicture}

  \end{subfigure}%
  \hfill
  % --- SECONDA SUBFIGURE ---
  \begin{subfigure}[t]{0.45\textwidth}
    \centering
    \begin{tikzpicture}
      \begin{axis}[
        axis lines=none,
        grid=none,
        width=\linewidth,
        height=0.8\linewidth,
        xmin=-1.5, xmax=1.5,
        ymin=-1.5, ymax=1.5,
        samples=200,
        domain=0:360
      ]
        \addplot[thick, parametric] ({-0.5+0.5*cos(x)}, {0.5*sin(x)});
        \addplot[only marks, mark=*] coordinates {(0,0)};
        \node[right]  at (axis cs:0,0) {$x_0$};
        \node[left]  at (axis cs:-1,0) {$\mu(t)$};
      \end{axis}
    \end{tikzpicture}
  \end{subfigure}
  \hfill
  \begin{subfigure}[t]{0.45\textwidth}
    \centering
    \begin{tikzpicture}
      \begin{axis}[
        axis lines=none,
        grid=none,
        width=\linewidth,
        height=0.8\linewidth, 
        xmin=-1, xmax=4,
        ymin=-3, ymax=3,
        samples=200,
        domain=0:3.1416
      ]
        \addplot[thick,smooth]{2*sin(deg(x))};
        \addplot[thick,smooth]{-sin(deg(x))};
        \addplot[only marks, mark=*] coordinates {(0,0)};
        \addplot[only marks, mark=*] coordinates {(3.1416,0)};
        \foreach \n in {1,...,9} {
        \pgfmathsetmacro{\t}{\n/10}
        \pgfmathsetmacro{\coef}{2 - 3*\t}
        \pgfmathtruncatemacro{\perc}{100*\t}
        
       

        
        \addplot[
            smooth, thick,
            red!\perc!black, % gradiente dal verde al blu
            domain=0:3.1416,
            samples=200
         ] {\coef*sin(deg(x))};
         }

        
        \node[below left]  at (axis cs:0,0) {$x_0$};
        \node[below right] at (axis cs:3.1416,0) {$x_1$};
        \node[above] at (axis cs:1.5,1.9) {$\gamma_0(t)$};
        \node[below right] at (axis cs:2.5,-0.4) {$\gamma_1(t)$};
      \end{axis}
    \end{tikzpicture}
  \end{subfigure}
  \hfill
  \begin{subfigure}[t]{0.45\textwidth} %is here in this subfigure the problem
    \centering
    \begin{tikzpicture}
      \begin{axis}[
        axis lines=none,
        grid=none,
        width=\linewidth,
        height=0.8\linewidth,
        xmin=-1.5, xmax=1.5,
        ymin=-1.5, ymax=1.5,
        samples=200,
        domain=0:360
      ]
        \foreach \n in {1,...,9} {
        \pgfmathsetmacro{\t}{\n/10}
        \pgfmathsetmacro{\coefa}{-0.5 + 0.25*\t}
        \pgfmathsetmacro{\coefb}{-\coefa}
        \pgfmathtruncatemacro{\perc}{100*\t}
        
        \addplot[thick, parametric]({\coefa +\coefb*cos(x)},{\coefb*sin(x)});
        };


        
      
        \addplot[thick, parametric] ({-0.5+0.5*cos(x)}, {0.5*sin(x)});
        \addplot[only marks, mark=*] coordinates {(0,0)};
        \node[right]  at (axis cs:0,0) {$x_0$};
        \node[left]  at (axis cs:-1,0) {$\mu_0(t)$};
        \node[right]  at (axis cs:-0.55,0) {$\mu_1(t)$};
      \end{axis}
    \end{tikzpicture}

  \end{subfigure}
  \hfill
\end{mdframed}
\caption{Main Caption}
\end{figure}
\end{document}

        

And the result is Where the problem is in the

New contributor
Liam Seddio is a new contributor to this site. Take care in asking for clarification, commenting, and answering. Check out our Code of Conduct.
5
  • 1
    I think it may be something to do with using foreach inside pgfplots. I don't use pgfplots though, so I am uncertain. pgfplots has specialized commands for for loops. In your code \perc is showing up as undefined. Commented yesterday
  • 1
    Please make your code into a compilable document. It is missing a documentclass and a/some package(s). Commented yesterday
  • 1
    welcome! please make your snippet so we can copy-paste and compile it. but what @Jasper said: you need to look at the pgfplots method for loops. Commented yesterday
  • 1
    I updated the code, thank you very much for the advice Commented yesterday
  • 1
    Welcome to TeX.SX! Commented 22 hours ago

2 Answers 2

4

You can maybe define a colormap for that

colormap = {my-gradient}{color=(red) color=(yellow)},

and use a cycle list based on the colormap, with

cycle list={[samples of colormap={9} of my-gradient]},

since you have 9 curves.

Two adjustments are also necessary:

  1. Replace \addplot with \addplot+ for the curves that use the cycle list, as, quoting the manual,

the currently active cycle list will be used if you write either \addplot+[keys] ...; or if you don’t use square brackets as in \addplot[[explicit plot specification] ...;.

  1. Use cycle list shift=-4 to ignore the index increment generated by the previous \addplot commands, since, quoting the manual again,

This indexing does also include plot commands which don’t use the cycle list.

Example:

\documentclass{article}

\usepackage{pgfplots}

\pgfplotsset{
  compat=1.18,
  colormap = {my-gradient}{color=(red) color=(yellow)},
}

\begin{document}

\begin{tikzpicture}

  \begin{axis}[
      axis lines=none,
      grid=none,
      width=\linewidth,
      height=0.8\linewidth, % stessa altezza per entrambi
      xmin=-1, xmax=4,
      ymin=-3, ymax=3,
      samples=20, smooth,
      domain=0:3.1416,
    ]
    \addplot[thick,smooth]{2*sin(deg(x))};
    \addplot[thick,smooth]{-sin(deg(x))};
    \addplot[only marks, mark=*] coordinates {(0,0)};
    \addplot[only marks, mark=*] coordinates {(3.1416,0)};
    
    \pgfplotsset{
      cycle list shift=-4,
      cycle list={[samples of colormap={9} of my-gradient]},
    }
    
    \foreach \n in {1,...,9} {
      \pgfmathsetmacro{\t}{\n/10}
      \pgfmathsetmacro{\coef}{2 - 3*\t}
      
      \addplot+[
        smooth, thick,
        domain=0:3.1416,
      ] {\coef*sin(deg(x))};
      
    }
    
    \node[below left]  at (axis cs:0,0) {$x_0$};
    \node[below right] at (axis cs:3.1416,0) {$x_1$};
    \node[above] at (axis cs:1.5,1.9) {$\gamma_0(t)$};
    \node[below right] at (axis cs:2.5,-0.4) {$\gamma_1(t)$};
  \end{axis}

\end{tikzpicture}

\end{document}

Example

1
  • 1
    Thank you very much, that's perfect Commented 3 hours ago
1

You could use the same trick as in https://tex.stackexchange.com/a/353794/36296

\documentclass{amsart}
\usepackage[english]{babel}
\usepackage{amsmath}
\usepackage{hyperref}
\usepackage[a4paper, margin=2.5cm]{geometry}
\usepackage{pgfplots}
\usepackage{xcolor}
\pgfplotsset{compat=1.18}
\usepackage{mdframed}
\usepackage{subcaption}

\begin{document}

\begin{tikzpicture}
  \begin{axis}[
    axis lines=none,
    grid=none,
    width=\linewidth,
    height=0.8\linewidth, 
    xmin=-1, xmax=4,
    ymin=-3, ymax=3,
    samples=200,
    domain=0:3.1416
  ]
    \addplot[thick,smooth]{2*sin(deg(x))};
    \addplot[thick,smooth]{-sin(deg(x))};
    \addplot[only marks, mark=*] coordinates {(0,0)};
    \addplot[only marks, mark=*] coordinates {(3.1416,0)};
    
    \foreach \n  in {1,...,9} {
    \pgfmathsetmacro{\t}{\n/10}
    \pgfmathsetmacro{\coef}{2 - 3*\t}
    \pgfmathtruncatemacro{\perc}{100*\t}
    
   
    \edef\temp{\noexpand\addplot[
            smooth, thick,
            yellow!\perc!red, % gradiente dal verde al blu
            domain=0:3.1416,
            samples=200
         ] {\coef*sin(deg(x))};};
    \temp
    

     }

    
    \node[below left]  at (axis cs:0,0) {$x_0$};
    \node[below right] at (axis cs:3.1416,0) {$x_1$};
    \node[above] at (axis cs:1.5,1.9) {$\gamma_0(t)$};
    \node[below right] at (axis cs:2.5,-0.4) {$\gamma_1(t)$};
  \end{axis}
\end{tikzpicture}

\end{document}

enter image description here

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.