Using Contourf to Create Contour Plot with XYZ Data

56 visualizzazioni (ultimi 30 giorni)
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 Commenti
Torsten
Torsten il 6 Lug 2022
my z (Zm) is 11x11 using the griddata command.
And what do you get from griddata ? The NaN matrix except for the antidiagonal ?

Accedi per commentare.

Risposta accettata

dpb
dpb il 6 Lug 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 Commenti
Nigel Caprotti
Nigel Caprotti il 6 Lug 2022
A thorough analysis and very helpful. Much appreciated, dpb!
Nigel Caprotti
Nigel Caprotti il 8 Lug 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).

Accedi per commentare.

Più risposte (1)

Torsten
Torsten il 6 Lug 2022
Modificato: Torsten il 6 Lug 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 Commento
Nigel Caprotti
Nigel Caprotti il 8 Lug 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).

Accedi per commentare.

Categorie

Scopri di più su Contour Plots in Help Center e File Exchange

Prodotti


Release

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by