# Inverting a colormap in pgfplots

I am trying to obtain an inverted version of a given colormap to use in the colorbar of a plot.

The code below achieves this specifically for the blackwhite colormap. However, I was looking for a more general method which would only require the name of the colormap to be inverted.

\documentclass{standalone}
\usepackage{pgfplots}

\begin{document}

\begin{tikzpicture}
\begin{axis}[colorbar, colormap={}{ gray(0cm)=(1); gray(1cm)=(0);}]
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[colorbar, colormap={}{ gray(0cm)=(0); gray(1cm)=(1);}]
\end{axis}
\end{tikzpicture}

\end{document}


• If I understand your question correctly. You could define a new colormap named {<nameINV>} via colormap={<name>}{<color specification>} in pgfplotsset{...} and activate it by using colormap name=<nameINV>. See pages 87-88 in PGFPLOTS for detail. – Jesse Oct 29 '13 at 5:21
• colormap={<name>}{<color specification>} is used in the example above, but this has the inconvenience of having to manually define the colour specification. I was looking for something that would provide a general solution for inverting any existing colormap. For example, in Matlab, this is achieved by flipud(colormap). – Pedro Oct 29 '13 at 8:42

## EDIT

Recent versions of pgfplots allow to answer the question in simpler form without resorting to custom macro coding. Here is the answer based on a recent version of pgfplots:

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}

\begin{document}

\pgfplotsset{
%colormap={X}{ gray(0cm)=(1); gray(1cm)=(0);},
colormap/winter,
}
\begin{tikzpicture}
\begin{axis}[colorbar]
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
\begin{axis}[colorbar,
colormap={reverse winter}{
indices of colormap={
\pgfplotscolormaplastindexof{winter},...,0 of winter}
},
]
\end{axis}
\end{tikzpicture}

\end{document}


Details about this approach can be found in the pgfplots manual, subsection "Building Colormaps based on other Colormaps".

## This here is the original answer

One could write such a utility macro as follows.

Note that this comes without any warranty, i.e. if the internals might change sometime, this will break (although it is unlikely that they will change soon).

\documentclass{standalone}
\usepackage{pgfplots}
\usepgfplotslibrary{colormaps}

\makeatletter
\def\customrevertcolormap#1{%
\pgfplotsarraycopy{pgfpl@cm@#1}\to{custom@COPY}%
\c@pgf@counta=0
\c@pgf@countb=\pgfplotsarraysizeof{custom@COPY}\relax
\c@pgf@countd=\c@pgf@countb
\pgfutil@loop
\ifnum\c@pgf@counta<\c@pgf@countb
\pgfplotsarrayselect{\c@pgf@counta}\of{custom@COPY}\to\pgfplots@loc@TMPa
\pgfplotsarrayletentry\c@pgf@countd\of{pgfpl@cm@#1}=\pgfplots@loc@TMPa
\pgfutil@repeat
%\pgfplots@colormap@showdebuginfofor{#1}%
}%

\makeatother

\begin{document}

\pgfplotsset{
%colormap={X}{ gray(0cm)=(1); gray(1cm)=(0);},
colormap/winter,
}
\begin{tikzpicture}
\begin{axis}[colorbar]
\end{axis}
\end{tikzpicture}

\begin{tikzpicture}
%\customrevertcolormap{X}
%\customrevertcolormap{jet}
\customrevertcolormap{winter}
\begin{axis}[colorbar]

• This only seems to work for the colormap hot. For other colormaps, say winter, the macro fails, probably because \pgfplotscolormapassertexists{winter} gives an undefined error. Any ideas? – Pedro Oct 30 '13 at 8:44
• The color maps need to be defined when you invoke the command. In my example, you can include the X colormap by uncommenting the associated lines. You can also say colormap/winter right where the X colormap is currently uncommented - that will work. – Christian Feuersänger Oct 30 '13 at 19:24
• Using a key such as colormap/winter would be better than having to manually define each colormap, but I can't get it to work. Could you please update the example for this case? – Pedro Oct 30 '13 at 22:04
• see my edit. Note the use of \usepgfplotslibrary{colormaps} – Christian Feuersänger Oct 31 '13 at 18:35