# Using ColorBrewer in TikZ and pgfplots

Update: This package is no longer needed, as pgfplots now supports ColorBrewer schemes out of the box. Please see the latest version of pgfplots.

Choosing what colours to use for your plotting purposes is not always straightforward. Although most programs give you reasonable defaults for doing your day-to-day plots, perhaps you’d fancy a bit more style for your plots that will be published. Indeed, I often simply use gnuplot, matplotlib or the native plotting routines in R or matlab for doing my first visualizations. But for publications, I often switch to pgfplots, an excellent package built on top of TikZ which are implemented as LaTeX packages.

The documentation for both packages is comprehensive, and in addition a lot of examples can be found online. The pgfplots package already contains quite some colorbars of its own. Nevertheless, I often feel compelled to browse to ColorBrewer for its marvellous colours. For example, for some publications you need to ensure that the line colours of a plot are still discernible when printed in grayscale, which is even more relevant when you are making a density plot.

After having consulted the website regularly, I found myself constantly inserting lines such as

    \definecolor{color1}{rgb}{0.215686,0.494118,0.721569}
\definecolor{color2}{rgb}{0.894118,0.101961,0.109804}
\definecolor{color3}{rgb}{0.301961,0.686275,0.290196}


for my plotting purposes. Of course, this works perfectly well. But once you start exploring different colour maps for your density plots, it quickly becomes tedious to change these definitions all the time. That’s why I simply downloaded the Excel sheet listing all the colour schemes, and changed them into a TikZ library. On top of that, I created also pgfplots library in order to use the colours for my plotting purposes.

Using these packages now makes it straightforward to use the colorbrewer colours. Simply import the package using

    \usetikzlibrary{colorbrewer}


and you can simply use

\begin{tikzpicture}
\node[fill=Dark2-3-2,
minimum size=5mm] {};
\end{tikzpicture}


An overview of all the colours (of course automatically generated) can be seen in the picture on the right. The colour schemes are named exactly the same as on the ColorBrewer website. Each individual colour of the scheme simply gets an index number, starting from 1, in addition to the letters used to designate the colour. Hence, the colour Dark2-3-2 refers to the second colour of the Dark2 colour schemes using 3 colours. Alternatively, you can refer to the colour using Dark2-3-B. For more details, you might want to check out the ColorBrewer website.

It is now also easy to use the colour schemes in pfgplots. Simply include the
pgf package

\usepgfplotslibrary{colorbrewer}


Then specify which colour set to use, for example using the cycle multi list.

  \begin{tikzpicture}
\begin{axis}[smooth,
thick,
domain=-2*pi:2*pi,
cycle multi list={Set1-4}]
\end{axis}
\end{tikzpicture}


Or you can use easily use colour maps for your fancy 3d plots

\begin{tikzpicture}
\begin{axis}[domain=-2*pi:2*pi,
point meta min=-1,point meta max=1,
colorbar,colormap name={RdBu-11}]
gnuplot {sin(sqrt(x^2 + y^2))};
\end{axis}
\end{tikzpicture}


You can find the files at my GitHub repository: https://github.com/vtraag/tikz-colorbrewer. Don’t forget to put the files somewhere where TikZ can find them!

Update (July 24, 2014): I added cycle lists for pgfplots that use markers. It’s a shame that you cannot easily combine different lists, where you either specify the colour or the marker or the line style, because now you all have to redefine it. There should be a different cycle multi list that cycles through multiple lists in parallel!

## 12 thoughts on “Using ColorBrewer in TikZ and pgfplots”

1. Very nice work. Any intention to upload this to CTAN?

2. Quite a nice implementation! Much easier to use than \definecolor{color1} etc. Thanks.

3. Stefan Pinnow says:

Hello Vincent,

since I haven’t seen that you have uploaded your work to CTAN: What would you think of implementing your library into tikz and pgfplots directly? I already have asked Christian Feuersänger (the author of pgfplots) about his opinion on that and he also thinks it would be a nice idea.

Best regards,
Stefan

1. Hi Stefan,

No, I havent found the time for doing so. If Christian would be willing to include it in TikZ/pgfplots directlt, that would be great! Surely things could be made more efficiënt, but I guess Christian would know. There are also some other packages I believe that might be worth checking out for inclusion.

Best,

Vincent

1. Stefan Pinnow says:

Hello Vincent,

Christian told me that he wants to minimize his effort in implementation because of lack of time. So it would be really great if you could help.

Since I am — unfortunatelly — just a _user_ of LaTeX and cannot help in coding, could you perhaps state what you mean with “could be made more efficient”? Do you think it is worth to make the code more efficient before implementing?

And what other packages do you have in mind for inclusion?

Maybe it would be better to continue the discussion via email. Then it would be much easier for me to track stuff and also include Christian into discussion, when needed. After finishing discussion one of us can post a summary here.

Best regards,
Stefan

1. Ha, time, the eternally scarce resource… 🙂

In general, the loading time is a bit long. See the GitHub repository for details. There, also some related efforts are mentioned.

But let’s change the channel to mail then, just drop me a line and we’ll see what we can do.

4. Stefan Pinnow says:

Hello Vincent,

I have sent you an email to
v.a.traag@cwts.leidenuniv.nl
on Sunday but haven’t got an answer from you so far. Could you shortly reply if you have at least received it?

Many thanks and best regards,
Stefan

5. Hi Stefan,

Yes, I received your mail. It’s on the pile of things to do, and I havent had the time yet…

Best,

Vincent

6. Anna Weegels says:

Hi Vincent,

I was wondering how I can select the second until the last color of a color scheme. As the lightest color in PuBu-5 is not clearly visible in my tikz plot, I want to use all colors except the first one!

Best,
Anna

1. Hi Anna,

I’m not aware of an easy way to do that. You can of course always simply define your own color list (assuming you are using it as a list) by referring to the individual colors, which is probably the easiest.

Note that this package has become obsolete as pgfplots now supports Colorbrewer schemes out of the box. You can use \pgfplotscreateplotcyclelist to define your own cycle list. You can refer to the individual ColorBrewer colors as indicated in the manual (PuBu-A, PuBu-B, etc…).

Hope you get it working!

Best,

Vincent

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