# Setting gnuplot-like colormaps with pgfplots

I would to use the gnuplot default colormap with pgfplots. This colormap is defined in gnuplot with set palette rgb 7,5,15 where the numbers refer to a function for the red, green and blue colours respectively with this mapping:

 * there are 37 available rgb color mapping formulae:
0: 0               1: 0.5             2: 1
3: x               4: x^2             5: x^3
6: x^4             7: sqrt(x)         8: sqrt(sqrt(x))
9: sin(90x)       10: cos(90x)       11: |x-0.5|
12: (2x-1)^2       13: sin(180x)      14: |cos(180x)|
15: sin(360x)      16: cos(360x)      17: |sin(360x)|
18: |cos(360x)|    19: |sin(720x)|    20: |cos(720x)|
21: 3x             22: 3x-1           23: 3x-2
24: |3x-1|         25: |3x-2|         26: (3x-1)/2
27: (3x-2)/2       28: |(3x-1)/2|     29: |(3x-2)/2|
30: x/0.32-0.78125 31: 2*x-0.84       32: 4x;1;-2x+1.84;x/0.08-11.5
33: |2*x - 0.5|    34: 2*x            35: 2*x - 0.5
36: 2*x - 1
* negative numbers mean inverted=negative colour component
* thus the ranges in set pm3d rgbformulae' are -36..36


For this specific map the RGB component behaves as in the graph passing from the minimum (rgb=(0,0,0) black) to the maximum (rgb=(1,1,0) yellow) of the palette. I add also the palette so you can see the correspondence with the graph.

Is there a way of specify colormap functions also in pgfplots or it is only possible to have linear interpolation between colors?

-
Could you please clarify what do you want? You can write raw gnuplot with pgfplots, see this thread for example. Lookup the command \addplot gnuplot[raw gnuplot] in the manual. –  juliohm Sep 30 '13 at 9:41
@Red: You can't directly specify the colormaps in the same form as in gnuplot, but you can approximate the colormaps using piecewise linear functions. However, do you have a particular reason for wanting those kinds of colormaps? Simple functions of RGB channels don't typically lead to very good colormaps for visualizations, as they don't take into account the human perception of colour. Take a look at tex.stackexchange.com/questions/133351/… for a (IMHO) much better way of generating colormaps for scientific visualization. –  Jake Sep 30 '13 at 11:44
@Jake no particular reason, only my curiosity. I think gnuplot is a very good scientific plotting program so since it has this feature I thought it was a useful feature.. I'll take a look at the question you linked, thanks! –  Red Oct 1 '13 at 7:37

Pgfplots supports colormaps by means of interpolated tables, i.e. it expects something like

\documentclass{standalone}

\usepackage{pgfplots}
\usepgfplotslibrary{patchplots}

\begin{document}
\begin{tikzpicture}
\begin{axis}[
colorbar horizontal,
color=(black) color=(blue) color=(red) color=(yellow)
},
]
\end{axis}
\end{tikzpicture}

\end{document}


Clearly, this is an imprecise version of your variant as it only contains four colors. You could generate any number of entries to sample the gnuplot output to the desired degree.

Note that pgfplots knows sampled RGB values only in the context of surface plots with explicit color. This is the closest thing that pgfplots currently knows regarding color functions and interpolation in-between.

This section of the answer is more about connecting keywords "color function" and "pgfplots", it is largely unrelated to the question about colormaps. I hope it is useful, though.

The following is an example which attempts to reproduce the color interpolation above.

\documentclass{standalone}

\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\def\exprB{sin(360*x)}
\begin{axis}[
view={0}{90},
mesh/color input=explicit mathparse,
point meta={symbolic={%
sqrt(x),% R
x^3,% G
%\exprB < 0 ? 1+\exprB : \exprB% B
abs(\exprB)% B
}},
`