File Exchange

image thumbnail

contourfcmap: filled contour plot with precise colormap

version 1.7 (757 KB) by

Creates a filled contour plot, with more precise control over colors than contourf.

9 Ratings



Create a filled contour plot in Matlab, with better color-to-value clarity. See full description, along with examples, at

Comments and Ratings (24)

Kelly Kearney

Kelly Kearney (view profile)

@Peter Hristov

Thanks for pointing out this bug; I think I've now corrected things to properly deal with single-contour plots, both pre- and post-R2014b. New version just pushed to Github.

@Kelly Kearney Great function (as seen by rating). There is however a simple bug. Line 368 in contourfcmap.m assumes that the shaded patch will have more than one child and attempts to convert their handles from a cell array to a matrix. This is not always the case, so maybe you could add a simple if to check the number of children and act accordingly. Thanks!

Kelly Kearney

Kelly Kearney (view profile)


Thanks for pointing that out; new version just uploaded that should fix that error for pre-2011b releases.

Greg King

This function is just what I need, but it reports an error when I run it using ...
MATLAB Version (R2010b)
on mac osx.

The error is that caused by a call to narginchk, which is not in the R2010 release.

Kelly Kearney

Kelly Kearney (view profile)


No, I haven't tested this in 2016b yet. If you ask because you've found an incompatibility, please follow the GitHub link (upper right) and enter it as an issue.


Is there a version of this that works for MATLAB R2016b?


I frequently find this submission of yours to be tremendously useful. I often have a suite of plots or a panel of subplots and this script is the only way I can get consistency of colouring throughout.

Meanwhile some other users may find the following tweak helpful (it took me a while to twig) - To get rid of the actual contour lines, use h.h.LineColor = 'none'.

I'd rate the script at 5 but I've already done earlier this year.

Kelly Kearney

Kelly Kearney (view profile)


Tracked that bug to line 211, where meshgrid-style input was being incorrectly classified as a vector (and therefore x/y weren't converted to vectors as they should be). Now fixed.

Kyle Wang

Found a possible bug at Line #468 in contourfcmap.m.

The code
F = griddedInterpolant({x,y},z');
shall be corrected as
F = griddedInterpolant(x',y',z');

Otherwise the built-in example won't run.

Kelly Kearney

Kelly Kearney (view profile)


Thanks for pointing out this shortfall. I've modified the code so it now mostly supports irregular data grids (there are some edge cases involving NaNs that don't work so well, but most contourf-friendly data should be fine).


In CONTOURF, "If X or Y [matrices] is irregularly spaced, then contourf calculates contours using a regularly spaced contour grid, and then transforms the data to X or Y."

Contourfcmap works fine when I try it with regular X and Y matrices, but not with Y as an irregular one. When I attempt to do so, then Contourfcmap returns this error messsage - 'Error using interp2>makegriddedinterp (line 237)
Input grid is not a valid MESHGRID.

Error in interp2 (line 136)
        F = makegriddedinterp(X, Y, V, method,extrap);

Error in contourfcmap (line 389)
                tmp = interp2(x,y,z,xyz{ii}(1,:), xyz{ii}(2,:));".

Is it possible to incorporate the capability for irregular Y matrix into contourfcmap? I have the feeling it might be a ridiculously massive amount of work.
Nevertheless, for regular grids, I find your function gives the desired superior graphic. So, thank you.

Kelly Kearney

Kelly Kearney (view profile)


You're right... I recently updated the code to support NaNs in the data and didn't thoroughly test the new bit for older versions of Matlab. Just added a fix for that.

ed zaron

Hi Kelly,

I just downloaded and tried out this package but received this error when I tried the basic demo:
contourfcmap(x,y,z,[-5 -3 -2:.5:2 35],jet(12),[.8 .8 .8], [.2 .2 .2], 'eastoutside')
[x,y] = meshgrid(linspace(0,1,100));

Undefined function or variable "isn".

Error in contourfcmap (line 344)
                elseif isn(ip)

I am running R2012b ( 64-bit (glnxa64).

The concept of the package looks great; I'm sure it is a version incompatibility or typo in the latest update.

All the best,


Kelly Kearney

Kelly Kearney (view profile)


Your x, y, and z data need to formatted in the same way as for the native contourf function, so you'll need to grid your data (not knowing the arrangement of the points in your vectors, I don't know whether you'd have to simply reshape, regrid, or use a scattered interpolant). Type help contourfcmap to see an example of use.


hello ,
  I have a matriz of 3 colums (x,y,z), how I should use the code to run the program. what of the three files I must to run firts
thanks a lot!


Jan (view profile)

Superb function, thanks.


Franzi (view profile)

exactly what I needed


peter (view profile)

great submission, thanks a lot

Kelly Kearney

Kelly Kearney (view profile)

@Earle Wilson,

Both saturated low and high values should be colored by the colors defined by the 'lo' and 'hi' inputs, respectively; by default these are white, but you can set them to be the same as the first and last cmap values if you want a saturated effect. If you see a different result please send me an example and I'll look into it.

Earle Wilson

great function! This saved me many hours of working around all the bugs in contourf.

The only (minor) short coming is that this function doesn't deal with saturated low values. Eg. if you enter:
and there are values in z < min(clev), they will be plotted as white spaces. This is an issue with contourf really, which I won't get into here. An easy fix is set all values in z outside the clev limits to min(clev) or max(clev). Eg.
toohigh = z>max(clev); z(toohigh) = max(clev);
loolow = z<min(clev); z(toolow) = min(clev);

Kelly Kearney

Kelly Kearney (view profile)

Added minmax subfunction into main code (overlooked this dependency on a simple helper function in previous upload). Thanks, Karin and Aurelien, for pointing out that mistake.

I wanted to test your code but:
the minmax function which is required by your code is not shipped .

Another remark:
You have forgotten a bracket in the given example in the header of M-file:
[x,y] = meshgrid(linspace(0,1,100)
should be:
[x,y] = meshgrid(linspace(0,1,100))

Now waiting for minmax.m before rating.



Great function! Thanks



Small update to description, pointing reader to github for readme.


Linked to GitHub repository


Corrected a bug in polygon generation where corner points of patches were out of order.


-Rewritten for R2014b, no longer relies on contourf object
-Added option to display all color levels evenly on the colorbar, regardless of value


Fixed bug where new colorbar stole focus from axis.


Fixed accidental omission of helper function in colorbar generation (minmax). Also corrected minor typo in example and added more informative example image.


minor grammatical fix in description

MATLAB Release
MATLAB 7.10 (R2010a)

Download apps, toolboxes, and other File Exchange content using Add-On Explorer in MATLAB.

» Watch video