File Exchange

image thumbnail

Non-uniform contourf/imagesc/co​lorbar

version (130 KB) by Yuxin Jiang
This function allows non-uniform contour levels contourf/imagesc/pcolor.


Updated 11 Sep 2018

View License

Editor's Note: This file was selected as MATLAB Central Pick of the Week

The colormap in Matlab supports only linear mapping, which is inconvenient when plotting data spaning multiple orders, e.g., peaks(100).^4. This function uses the trick of piecewise-linearly mapping to allow non-uniform contour levels contourf/imagesc/pcolor.

--- Usage ---
varargout = contourfnu(x,y,data,v,cmap,pos_colorbar,overticklabel,method,ninterp,nancolor)

--- Input Variables ---
x : x-coordinates of grid, vector or 2d matrix
y : y-coordinates of grid, vector or 2d matrix
If x and y are vectors, then length(x)==size(z,2) and length(y)==size(Z,1).
If x and y are 2d matrix, they are generated by meshgrid
data : 2d matrix to be ploted
v : vector of contour levels (default:linspace(datamin,datamax,10))
cmap : color map array (default:jet)
pos_colorbar : 'none', or location with respect to the axes (default:'eastoutside')
overticklabel : whether or not label the overrange ticks at colorbar (default:true)
method : imagesc, contourf, contour or pcolor (default:imagesc)
ninterp : repeatedly interp times in each dimension (default:0)
nancolor : axis backgroud color

--- Output Variable ---
hout : structure with handles
.h plot handle
.c contour matrix (method='contourf')
.hc colorbar handle (pos_colorbar~='none')

Cite As

Yuxin Jiang (2020). Non-uniform contourf/imagesc/colorbar (, MATLAB Central File Exchange. Retrieved .

Comments and Ratings (3)


it's suitable for uneven colorbar


For some reason the colorbar sometimes takes for lowest value that of the data's lowest value...

Stefano Colpani

Yuxin, nice one-stop solution to both the colormap-colorbar color correspondence and colormap-data non-linear scaling issues, well done! Note method needs 'method' as input, nancolor needs the values (such as rgb('white') instead of 'w'), and a slight code modification allows for the use of m_pcolor (M_Map mapping package) instead or beside the standard pcolor. Also the colorbar is better positioned than when using cbarf. And with just one function you do all the plotting job. Thanks, Stefano


Add support for pcolor

Update demo and description.
Fix a bug when pos_colorbar='southourside', 'northourside', 'south' or 'north'.
Add support for inf in v. This is useful for common colorbar plots.

handle Inf(s)

handle NaNs

update comments

add a parameter to control the max/min ticklabel(s)
update demo

update description

MATLAB Release Compatibility
Created with R2017a
Compatible with any release
Platform Compatibility
Windows macOS Linux