# 2-D Contour plot for non-uniformly spaced data points

74 views (last 30 days)
A on 26 May 2015
Commented: John D'Errico on 1 Jul 2020
I have arrays data from COMSOL for a non-uniform mesh: x,y node positions and u for each node, where the nodes are refined in one area. I want to create a 2-D contour plot of this data in MatLab on an x-y graph and colors representing u. All the information have found seems to involve creating a rectangular mesh via 'meshgrid' and then letting it interpolate the data to this mesh. I'm wondering if there is a simpler way where I can just plot the raw data so I don't eliminate any values?
Any help greatly appreciated.
##### 2 CommentsShow 1 older commentHide 1 older comment
John D'Errico on 1 Jul 2020
@ Petr Michalek:
A Irecall, there are two tools one the file exchange to do contouring of scattered data. Both are probably calledd tricontour, so either should work.

Ashish Gudla on 27 May 2015
If I understand you correctly, you are trying to plot the raw data, instead of creating a regular mesh using 'meshgrid'.
You can use the 'contour' or the 'contourf' functions as explained in the documentation:
Also the 'mesh' function can take irregularly spaced vector inputs. See this for an example.
##### 2 CommentsShow 1 older commentHide 1 older comment
A on 28 May 2015
Contour only works when 'u' (z) is a matrix of x and y points.
"If X and Y are vectors, then length(X) must equal size(Z,2) and length(Y) must equal size(Z,1). The vectors must be strictly increasing or strictly decreasing and cannot contain any repeated values".
My data is three vectors of thousands of points, but there could be more x values at y = 0 than at y = 1 because the mesh from ComSol is not uniform. So I can't simply reshape u into a matrix without losing data. That is my issue.

Mike Garrity on 27 May 2015
The contour commands can handle curvilinear grids, but not multi-resolution grids. So I could do this:
z = peaks;
contourf(x,y,z)
To get a contour of a 2D grid which wraps around a half annulus, but the X, Y, and Z inputs are all 2D arrays.
One option is to convert your data into a triangle mesh and then use Darren Engwirda's tricontour function from the File Exchange .
Another would be to use something other than a contour. For example, if your grid is fine enough, you could just use scatter to draw colored circles at each location:
scatter(x,y,120,c,'filled')
That'll give you no interpolation at all, just the raw data.
Manoj on 19 Dec 2018
will any one help me for 2d contour plot with the attached data for non uniformly spaced data points ???

### Categories

Find more on Contour Plots in Help Center and File Exchange

### Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!