I have a $80\times200$ matrix, and I want to plot its contents with the contourf command in MATLAB, as follows:

\begin{align} &\text{[C,h] = contourf(mat,100);}\\ &\text{set(h,'LineColor','none');} \end{align}

The resulting image, as you can see, has some step-like discontinuities where the white part meets the coloured part. Is there any way to smooth this region with curve-fitting tool or other methods?


  • $\begingroup$ I like getting very high count of levels before truncating the data. What if you used 300 or more? $\endgroup$ Commented Oct 5, 2015 at 21:20

2 Answers 2


Is the white part NaNs?

If so, then you will need to use some sort of extrapolation to smooth that region. The function inpaint_nans may be appropriate (it smoothly fills in NaN regions, essentially by solving a Laplace equation). If more smoothing is needed, you could then follow Juan's approach (i.e. gaussian blur).

Another thing you might consider: I do not know what the purpose of your display is, but you might consider just using imagesc() instead of contourf(). For example


should be similar to

imagesc(I); colormap(jet(100));

which just quantizes the colormap used to render the image data. (Quantizing the colormap will typically be much faster though, compared to computing 100 contours.)


You can regard your original matrix as an image (i.e., a real-valued function $I = I(x, y)$), then you can apply a Gaussian filter (convolve $I$ with a Gaussian kernel), and finally you can get the contours of the filtered image/matrix. MATLAB's Image Processing toolbox already has the functionality available.


Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.