2

Is it possible for PGFplots to choose (automatically) such or such cycle list according to the number of curves to display ?

To illustrate, and using the colobrewer library, PGFplots could be configured to choose (automatically) the style BuGn-3 if there are 3 or less curves to display, BuGn-4 if there are 4 curves to display, BuGn-5 if there are 5, etc.
Of course, this could work with any cycle list.

Here is a non-working MWE :

\documentclass{standalone}
\usepackage{etoolbox}
\usepackage{xstring}
\usepackage{pgfplots}
\usepgfplotslibrary{colorbrewer}

\newcounter{colorCycleCounter}
\preto\addplot{\stepcounter{colorCycleCounter}}
\AtBeginEnvironment{axis}{\setcounter{colorCycleCounter}{0}}

\newcommand{\colorCycle}{%
    \IfEqCase*{\value{colorCycleCounter}}{%
        {1} {cycle list name=BuGn-3}%
        {2} {cycle list name=BuGn-3}%
        {3} {cycle list name=BuGn-3}%
        {4} {cycle list name=BuGn-4}%
        {5} {cycle list name=BuGn-5}%
        {6} {cycle list name=BuGn-6}%
        {7} {cycle list name=BuGn-7}%
        {8} {cycle list name=BuGn-8}%
        {9} {cycle list name=BuGn-9}%
    }[]%
}%

\pgfplotsset{
cycle list/BuGn-3,
cycle list/BuGn-4,
cycle list/BuGn-5,
cycle list/BuGn-6,
cycle list/BuGn-7,
cycle list/BuGn-8,
cycle list/BuGn-9,
}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[\colorCycle]%"cycle list name=BuGn-3"
        \addplot+[very thick] coordinates {(0,0)(1,1)};
        \addplot+[very thick] coordinates {(1,0)(2,1)};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[\colorCycle]%"cycle list name=BuGn-3"
        \addplot+[very thick] coordinates {(0,0)(1,1)};
        \addplot+[very thick] coordinates {(1,0)(2,1)};
        \addplot+[very thick] coordinates {(2,0)(3,1)};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[\colorCycle]%"cycle list name=BuGn-5"
        \addplot+[very thick] coordinates {(0,0)(1,1)};
        \addplot+[very thick] coordinates {(1,0)(2,1)};
        \addplot+[very thick] coordinates {(2,0)(3,1)};
        \addplot+[very thick] coordinates {(3,0)(4,1)};
        \addplot+[very thick] coordinates {(4,0)(5,1)};
    \end{axis}
\end{tikzpicture}
\end{document}

Here is for example the expected result (by manually specifying the desired cycle list) for 4 graphs of 2, 3, 5 and 9 curves which will use the cycle list :

  • cycle list/BuGn-3 for the first two graphs,
  • cycle list/BuGn-5 for the third,
  • cycle list/BuGn-9 for the last one.

enter image description here

  • See if I understand your problem: You want in the first figure all 3 lines to be colored with 1 color and second figure all 3 lines to be colored with 1 color and last figure all 5 lines to be colored with 5 colors: 3 color like before and 2 new color? – user108724 Sep 30 at 11:14
  • @C.F.G Not exactly, the goal is to create a command allowing PGFplots to choose automatically (default choice for each new graph) a cycle list according to the number of curves to display. If there is 1 curve to be displayed, use cycle list A, if there are 2 curves, use cycle list B, if there are 3, use cycle list C and so on. In the example I've taken, the cycle lists are all of the type list/BuGn- n, but I could also have used user-defined cycle lists. – B Legrand Sep 30 at 17:45
  • Of course, we could do all this manually using the command cycle list name=, but automating this task would make things easier. In addition, we can easily forget to update our cycle list choice when adding a curve to the graph, and use cycle list/BuGn-3 when we have 4 curves for example. – B Legrand Sep 30 at 17:51
  • If the above output (image in your post) is not your desired you can set pgf color before each tikzpicture i.e. using \pgfplotsset{ – user108724 Sep 30 at 18:12
  • For example you can add \pgfplotsset{ 4 times before each plot. – user108724 Sep 30 at 18:13
5
+25

There is a chance to use \numplots to obtain the number of total plots.

However, the cycle list (seems to be) is processed before \numplots. So we have to compute the color barehanded (without the help of cycle list).

That also means we have to break the + part of \addplot+, so the marks are now gone. Maybe other people know how to add them back.

\documentclass[tikz]{standalone}
\usepackage{pgfplots}
\pgfplotsset{compat=1.17}
\usepgfplotslibrary{colorbrewer}
\pgfplotsset{colormap/BuGn-9}

\begin{document}

\makeatletter

\def\plotnum{0} % emergency default value
\def\numplots{9} % emergency default value
\pgfplotsset{
    color according to numplots/.style={
        every axis plot/.style={
            /utils/exec={
                \pgfmathsetmacro\safenumberofplot{max(\numplots,2)}
                \pgfmathsetmacro\metavalue{1000*(\plotnum+1)/\safenumberofplot}
            },
            color of colormap=\metavalue of BuGn-9,.
        }
    }
}

\begin{tikzpicture}
    \begin{axis}[color according to numplots]
        \addplot[very thick] coordinates {(0,0)(1,1)};
        \addplot[very thick] coordinates {(1,0)(2,1)};
        \addplot[very thick] coordinates {(2,0)(3,1)};
    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}[color according to numplots]
        \addplot[very thick] coordinates {(0,0)(1,1)};
        \addplot[very thick] coordinates {(1,0)(2,1)};
        \addplot[very thick] coordinates {(2,0)(3,1)};
        \addplot[very thick] coordinates {(3,0)(4,1)};
        \addplot[very thick] coordinates {(4,0)(5,1)};
    \end{axis}
\end{tikzpicture}

\begin{tikzpicture}
    \begin{axis}[color according to numplots]
        \addplot[very thick] coordinates {(0,0)(1,1)};
        \addplot[very thick] coordinates {(1,0)(2,1)};
        \addplot[very thick] coordinates {(2,0)(3,1)};
        \addplot[very thick] coordinates {(3,0)(4,1)};
        \addplot[very thick] coordinates {(4,0)(5,1)};
        \addplot[very thick] coordinates {(5,0)(6,1)};
        \addplot[very thick] coordinates {(6,0)(7,1)};
    \end{axis}
\end{tikzpicture}

\end{document}
| improve this answer | |
0
  1. Resetting \pgfplotsset{ before each \begin{tikzpicture}:
\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colorbrewer}

\begin{document}
    \pgfplotsset{
        cycle list/BuGn-3,
    }
    \begin{tikzpicture}
    \begin{axis}
    \addplot+[very thick] coordinates {(0,0)(1,1)};
    \addplot+[very thick] coordinates {(1,0)(2,1)};
    \end{axis}
    \end{tikzpicture}
    \pgfplotsset{
        cycle list/BuGn-3,
    }
    \begin{tikzpicture}
    \begin{axis}
    \addplot+[very thick] coordinates {(0,0)(1,1)};
    \addplot+[very thick] coordinates {(1,0)(2,1)};
    \addplot+[very thick] coordinates {(2,0)(3,1)};
    \end{axis}
    \end{tikzpicture}
    \pgfplotsset{
        cycle list/BuGn-5,
    }
    \begin{tikzpicture}
    \begin{axis}
    \addplot+[very thick] coordinates {(0,0)(1,1)};
    \addplot+[very thick] coordinates {(1,0)(2,1)};
    \addplot+[very thick] coordinates {(2,0)(3,1)};
    \addplot+[very thick] coordinates {(3,0)(4,1)};
    \addplot+[very thick] coordinates {(4,0)(5,1)};
    \end{axis}
    \end{tikzpicture}
\end{document}

enter image description here

  1. Defining new color list by \pgfplotscreateplotcyclelist and calling it by cycle list name=
\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colorbrewer}

\pgfplotscreateplotcyclelist{newcycle1}{blue!20\\blue!30\\blue!40\\}
\pgfplotscreateplotcyclelist{newcycle2}{blue!20\\blue!60\\blue!90\\}
\pgfplotscreateplotcyclelist{newcycle3}{green\\red\\yellow\\purple\\black\\}


\begin{document}
\begin{tikzpicture}
    \begin{axis}[cycle list name=newcycle1]%"cycle list name=BuGn-3"
        \addplot+[very thick] coordinates {(0,0)(1,1)};
        \addplot+[very thick] coordinates {(1,0)(2,1)};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[cycle list name=newcycle2]%"cycle list name=BuGn-3"
        \addplot+[very thick] coordinates {(0,0)(1,1)};
        \addplot+[very thick] coordinates {(1,0)(2,1)};
        \addplot+[very thick] coordinates {(2,0)(3,1)};
    \end{axis}
\end{tikzpicture}
\begin{tikzpicture}
    \begin{axis}[cycle list name=newcycle3]%"cycle list name=BuGn-5"
        \addplot+[very thick] coordinates {(0,0)(1,1)};
        \addplot+[very thick] coordinates {(1,0)(2,1)};
        \addplot+[very thick] coordinates {(2,0)(3,1)};
        \addplot+[very thick] coordinates {(3,0)(4,1)};
        \addplot+[very thick] coordinates {(4,0)(5,1)};
    \end{axis}
\end{tikzpicture}
\end{document}

enter image description here

You can define a long list of colors like \pgfplotscreateplotcyclelist{newcycle}{blue!20\\blue!60\\blue!90\\blue!20!green\\blue!60!green\\blue!90!green\\blue!20!red\\blue!60!red\\blue!90!red\\} and call this cycle list name=newcycle in all plots.

enter image description here

| improve this answer | |
  • Having already read the documentation of PGFplots, I know these commands. This does not solve my problem, since you specify manually for each chart which cycle list you want to use. For example, if I add two curves (materialized by the \addplot commands) to your chart number 2, the cycle list will not be changed automatically, I should change it manually (or possibly forget to change it). – B Legrand Sep 30 at 19:30
  • @BLegrand: Please please read the last para. The last image in my answer has been compiled just by one color list. (all 3 figures) – user108724 Sep 30 at 19:45
  • I’m sorry, but that still doesn’t answer my initial question. I know the commands you use and I use them myself on a regular basis. Whatever the number of curves, the color alternation will always be: blue!20\\blue!60\\blue!90\. . ., something I don’t necessarily want. Example: For a graph of two curves, I would like red and then blue, but for a graph of 3 curves, I would like the alternance red, purple and then blue. In this case, I should create 2 cycle list which I should call manually. Back to square one. . . – B Legrand Sep 30 at 20:18
  • Sorry I am complaining, but why you don't tell all of these once in the question? – user108724 Sep 30 at 20:20
  • Just look at your example that is your desired one. Is that colored by red, blue, ...? – user108724 Sep 30 at 20:24

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.