Using Contourf to Create Contour Plot with XYZ Data
Show older comments
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
Torsten
on 6 Jul 2022
If x is 11x1 and y is 11x1, z must be 11x11. Your z is only 11x1.
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...
Nigel Caprotti
on 6 Jul 2022
Edited: Nigel Caprotti
on 6 Jul 2022
Nigel Caprotti
on 6 Jul 2022
Torsten
on 6 Jul 2022
my z (Zm) is 11x11 using the griddata command.
And what do you get from griddata ? The NaN matrix except for the antidiagonal ?
Nigel Caprotti
on 6 Jul 2022
Accepted Answer
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')
1 Comment
Nigel Caprotti
on 8 Jul 2022
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!

