#StackBounty: #pgfplots #plot PGFplots – Automatic choice of a cycle list according to the number of curves

Bounty: 50

Is it possible for PGFplots to choose 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 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}
pretoaddplot{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

This what I get using only using pgfplotsset{cycle list/BuGn-3,...,cycle list/BuGn-9,} as suggested.

enter image description here


Get this bounty!!!

Leave a Reply

This site uses Akismet to reduce spam. Learn how your comment data is processed.