# Having smoother contour plots in MATLAB

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?

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

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

contourf(I,100);


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.