Using Contourf to Create Contour Plot with XYZ Data

14 views (last 30 days)
Nigel Caprotti
Nigel Caprotti on 6 Jul 2022
Commented: Nigel Caprotti on 8 Jul 2022
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

Sign in to comment.

Accepted Answer

dpb
dpb on 6 Jul 2022
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
Nigel Caprotti
Nigel Caprotti on 8 Jul 2022
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)

Torsten
Torsten on 6 Jul 2022
Edited: Torsten on 6 Jul 2022
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
Nigel Caprotti
Nigel Caprotti on 8 Jul 2022
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.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by