Using Contourf to Create Contour Plot with XYZ Data

Hi,
I am struggling to create a filled contour plot with XYZ data -- I see a lot of related questions to this on the forum, however the solutions I have tried all have returned an empty figure (with nothing plotted). Both ndgrid and meshgrid are returning a matrix with a bunch of NaN elements (this is supposed to be my 11x11 'Z' matrix); I cannot figure out the problem. I have attached a screenshot of the grid that is returned by the aforementioned commands.
The data I am trying to make the contour plot with is also shown below, as is the code I have been trying. It is also worth mentioning that I have tried the following previous answers to no avail (which my provided code is based off of):
https://www.mathworks.com/matlabcentral/answers/525251-contour-from-xlsx-getting-z-must-be-at-least-a-2x2-matrix-error
Lastly, a screenshot of the empty graph is also attached. If I can provide anything else to make helping easier, please let me know.
I am a novice in MATLAB, and would really appreciate any help regarding this.
x = [
185
168
151
134
117
100
83
66
49
32
15];
y = [
0.0284858387799564
0.0875980392156863
0.146770152505447
0.205893246187364
0.264934640522876
0.324106753812636
0.383311546840959
0.442500000000000
0.501683006535948
0.560991285403050
0.620419389978213];
z = [
0.0142768873834954
0.0153598427015410
0.0141841636377691
0.0143561269759432
0.0136126555180121
0.0150840480423058
0.0135176601415866
0.0123291499588930
0.0120082946593416
0.0148351459675182
0.0193117591922549];
load micro302info.mat
load micro302infotime.mat
load All_parameters.mat
figure(1);
% scatter(micro302infoNigelCaprotti06272022.Distancefromcathodeum,All_parameters(4:14,5));
xv = linspace(min(micro302infoNigelCaprotti06292022S1.EMtimehrs(1:11)), max(micro302infoNigelCaprotti06292022S1.EMtimehrs(1:11)), numel(micro302infoNigelCaprotti06292022S1.EMtimehrs(1:11)));
yv = linspace(min(micro302infoNigelCaprotti06272022.Distancefromcathodeum(1:11)), max(micro302infoNigelCaprotti06272022.Distancefromcathodeum(1:11)), numel(micro302infoNigelCaprotti06272022.Distancefromcathodeum(1:11)));
% [Xm,Ym] = meshgrid(xv, yv);
[Xm,Ym] = ndgrid(xv, yv);
Zm = griddata(micro302infoNigelCaprotti06292022S1.EMtimehrs(1:11),micro302infoNigelCaprotti06272022.Distancefromcathodeum(1:11), All_parameters(4:14,5), Xm, Ym);
contourf(Xm, Ym, Zm)
grid

6 Comments

If x is 11x1 and y is 11x1, z must be 11x11. Your z is only 11x1.
dpb
dpb on 6 Jul 2022
Edited: dpb on 6 Jul 2022
In future, for anybody to do anything without a lot of effort, attach the needed .mat files -- use the paperclip icon. We can't do a thing with images and it's a lot of bother to create files manually.
I did convert your data to code format and added [] so can be copied, at least...
I have attached the .mat files. Thank you for this suggestion. It is important to note that currently I am only trying to do the first 11 data points, which corresponds to rows 4 through 15 in the All_parameters table. This is rows 1 through 11 for the other two tables (micro302info.mat and micro302infotime.mat).
@Torsten ; my z (Zm) is 11x11 using the griddata command.
my z (Zm) is 11x11 using the griddata command.
And what do you get from griddata ? The NaN matrix except for the antidiagonal ?

Sign in to comment.

 Accepted Answer

You don't have enough data -- to use contourf, you must have size(Z) = x by y points -- you've got
>> whos x y z
Name Size Bytes Class Attributes
x 11x1 88 double
y 11x1 88 double
z 12x1 96 double
where only have one more z value than x and y -- you need 121.
And, as can be see with
plot3d(x,y,z(1:11))
shows essentially a trace along the diagonal of the xy plane; there's no information scattered anywhere else from which to try to interpolate to fill in something. You just don't have enough data from which to create a contour.

4 Comments

I feel as though I am asking the same question as this post:
This post only has 3 arrays of 11x1 length. I am confused.
The other poster had data in x and y directions that did at least cover a portion of the xy plane other than a diagonal so the computed Zm array from griddata had a fair number of points without extrapolation. In your case, applying identically the same scheme there are only
>> sum(isfinite(Zm))
ans =
1 0 0 1 0 0 0 0 0 0 1
>> sum(isfinite(Zm(:)))
ans =
3
>>
three(!) points out of the original 11 that were remaining; everything else was extrapolated and so was returned as NaN. The three scattered points aren't enough for the contour algorith to draw anything useful.
If you let griddata extrapolate, then you get something, but it's essentially worthless because you just don't cover the xy plane sufficiently to do any good -- in that case you can get a plot...
but as you see, it's just 1D along the x axis.
I didn't try, but I don't think Delauney triangles can do magic with such an input arrangement of the x-y points, either; they just don't cover the plane; they're perfectly correlated along the diagonal as shown by\
>> corr(x,y)
ans =
-1.0000
>>
or
plot(x,y)
So, in essence, you have only one independent variable, not two since Y is identically and linearly correlated with X.
A thorough analysis and very helpful. Much appreciated, dpb!
If anyone is interested in the resolution, I found success using the pcolor command in MATLAB to create a pseudocolor plot (instead of contour plot).

Sign in to comment.

More Answers (1)

This will work, but I don't think the interpolation of the Z data fits your needs.
x = [ 185
168
151
134
117
100
83
66
49
32
15];
y = [ 0.0284858387799564
0.0875980392156863
0.146770152505447
0.205893246187364
0.264934640522876
0.324106753812636
0.383311546840959
0.442500000000000
0.501683006535948
0.560991285403050
0.620419389978213];
z = [0.0142768873834954
0.0153598427015410
0.0141841636377691
0.0143561269759432
0.0136126555180121
0.0150840480423058
0.0135176601415866
0.0123291499588930
0.0120082946593416
0.0148351459675182
0.0193117591922549];
[X,Y] = ndgrid(x,y);
Z = griddata(x,y,z,X,Y,'nearest')
Z = 11×11
0.0143 0.0143 0.0143 0.0143 0.0143 0.0143 0.0143 0.0143 0.0143 0.0143 0.0143 0.0154 0.0154 0.0154 0.0154 0.0154 0.0154 0.0154 0.0154 0.0154 0.0154 0.0154 0.0142 0.0142 0.0142 0.0142 0.0142 0.0142 0.0142 0.0142 0.0142 0.0142 0.0142 0.0144 0.0144 0.0144 0.0144 0.0144 0.0144 0.0144 0.0144 0.0144 0.0144 0.0144 0.0136 0.0136 0.0136 0.0136 0.0136 0.0136 0.0136 0.0136 0.0136 0.0136 0.0136 0.0151 0.0151 0.0151 0.0151 0.0151 0.0151 0.0151 0.0151 0.0151 0.0151 0.0151 0.0135 0.0135 0.0135 0.0135 0.0135 0.0135 0.0135 0.0135 0.0135 0.0135 0.0135 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0123 0.0120 0.0120 0.0120 0.0120 0.0120 0.0120 0.0120 0.0120 0.0120 0.0120 0.0120 0.0148 0.0148 0.0148 0.0148 0.0148 0.0148 0.0148 0.0148 0.0148 0.0148 0.0148

1 Comment

I appreciate this response, as I reached this step to no avail as well. If anyone is interested in the resolution, I found success using the pcolor command in MATLAB to create a pseudocolor plot (instead of contour plot).

Sign in to comment.

Categories

Products

Release

R2020a

Community Treasure Hunt

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

Start Hunting!