# Automatically choosing preset color schemes in pgfplots based on number of entries

This is really more of a question to satisfy personal curiosity than to solve a real problem.

When choosing the colour schemes for my plots, I try to select colours that will work well with the amount of items involved in the plot. This means using sets of colours that are above all aesthetically pleasing, but also that contrast well, etc. (eg. schemes created with a website like this).

However, I was wondering if it would be possible to define a set of 2,3,4,...,n colours that work well, and have pgfplots (or some other package?) select automatically from the one that matches the amount of entries.

I looked around for similar questions, but I didn't seem to find anything. The colour sets could be defined as cycle lists, but it's the automatic selection that I'm wondering about. Can this be done? Any ideas?

-

This is just a longish comment. I might be wrong in understanding the workflow and hopefully I'll be corrected if so.

In pgfplots things do not happen all at once. First your plot commands together with the axis options are parsed and stored as PGF paths. Then a visualization scheme kicks in (and I'm not 100% sure about the order) and path colorings, legend styles etc. are assigned. Then they are drawn with low level TikZ/PGF commands.

In order to select a cycle list, the plots need to be collected already such that they are counted. But that would probably be too late to change the drawing options. I don't think this functionality is supported off-the-shelf.

Note that this is a very rough description, there are many things checked in each layer so don't take this too literal.

But if you are willing to provide the number of plot items then it's a matter of defining a style with an argument that selects the predefined plot cycle list.

-
It shouldn't be that much of a problem to provide the number of items in the list beforehand as an option of sorts I think. Although I think you're probably right with the points you make. – Mythio Feb 25 '13 at 21:53
I guess that is the real problem: the amount of information that is available to pgfplots at any given time, and the steps it takes in processing its data. I was thinking, for example, of plots that get their input from files that may even be automatically generated. I agree that in most cases this is not really a problem, but hey, I thought it'd be cool to ask and see what solutions (if any) come up. :) – jja Feb 25 '13 at 23:01
@jja Don't get me wrong, it's a nice question. It's just seems quite tedious to hack into the right place in the right time. But I've been wrong before so I'll say let's wait for Christian's opinion. Then probably I'll need to delete this :) – percusse Feb 26 '13 at 2:16
@percusse your answer is correct and complete: the cycle list is evaluated while surveying the plots and the number of plots is unknown until that survey has been finished. The expected way for such a style would be to make a couple of cycle lists which match and select them manually in advance, like size <10 cycle list or so. – Christian Feuersänger Mar 8 '13 at 19:50

As percusse and Christian Feuersänger have mentioned in the answer and comment, there is no way to automatic selection of a cycle list containing an appropriate number of items.

But at least since PGFPlots v1.13 the colorbrewer library is added to the package (see section 5.2 on page 397 of the manual), which brings the color schemes created by Cynthia Brewer published at http://colorbrewer2.org to PGFPlots as colormaps as well as as cycle lists. Here are two more or less random examples from that library.

\documentclass[border=2mm]{standalone}
\usepackage{pgfplots}
\usetikzlibrary{
pgfplots.colorbrewer,
}
\pgfplotsset{
cycle from colormap manual style/.style={
x=3cm,y=10pt,ytick=\empty,
stack plots=y,
every axis plot/.style={line width=2pt},
},
}
\begin{document}
\begin{tikzpicture}
\begin{axis}[
cycle list/RdYlBu-6,
cycle from colormap manual style,
]
\end{axis}
\end{tikzpicture}
\begin{tikzpicture}
\begin{axis}[
cycle list/Set1,
cycle from colormap manual style,
]