Inverting a Matlab colormap

Today I was making filled contour plot in Matlab in which the majority of the space is flat and at values close to zero. For the majority of Matlab’s built in colormap‘s values that are small are given a dark colour e.g. for the default colormap it’s dark blue, or for the gray colormap it’s black. I wanted to invert this so that the small values are lighter, or white and the large values are dark. I’m sure there are other ways to do this (like defining my own colormap), but here’s what I did to essentially invert the colormap:
>> % make your plot:
>> contourf(data);
>> % select the colormap you wish to invert:
>> colormap gray;
>> % get the matrix containing that colormap:
>> cmap = colormap;
>> % flip the matrix:
>> cmap = flipud(cmap);
>> % apply the new inverted colormap:
>> colormap(cmap);

Below is an example of what this looks like. This plot:
colormapexample1
becomes this plot:
colormapexample2

6 Replies to “Inverting a Matlab colormap”

Leave a Reply