Contour plot of cubic interpolation with set number of contour levels
1 回表示 (過去 30 日間)
古いコメントを表示
I have 2 sets of raw experimental data arranged in a grid of X- and Y- points with Z-values at each point representing intensity. Xvec, Yvec, and Zvec are vectors of the same length (24) where corresponding positions give the X- and Y- positions and Z values. I have used a cubic interpolation to fit a surface to this data, and have been able to plot it with a contour plot function. My code is as below, with an example Xvec, Yvec, and Zvec provided.
Xvec= [-1.8750 -1.1250 -0.3750 -1.8750 -1.1250 -0.3750 -1.8750 -1.1250 -0.3750 -1.8750 -1.1250 -0.3750 0.3750 1.1250 1.8750 0.3750 1.1250 1.8750 0.3750 1.1250 1.8750 0.3750 1.1250 1.8750]';
Yvec=[1.6250 1.6250 1.6250 0.5000 0.5000 0.5000 -0.6250 -0.6250 -0.6250 -1.8750 -1.8750 -1.8750 -1.8750 -1.8750 -1.8750 -0.6250 -0.6250 -0.6250 0.5000 0.5000 0.5000 1.6250 1.6250 1.6250]';
Zvec= [0.5516 0.7743 1.0936 1.1194 2.1194 2.6033 1.0840 2.1033 2.7903 0.4421 1.0936 0.7645 0.7420 0.4292 0.4292 2.6228 1.7775 0.9130 2.5453 1.7389 0.8743 1.0743 0.8358 0.6712]';
--Minimal coding example:
Zfit1=fit([Xvec Yvec],Zvec,'cubicinterp');
figure (1)
plot(Zfit1,[Xvec Yvec],Zvec,'Style','Contour')
c=colorbar;
cc=get(c,'Limits');
set(c,'Ticks',linspace(cc(1),cc(2),10))
set(gcf,'color','w');
--End example
Yields the following figure:
I would like to have both the contour of the fit and the X-Y data points on the chart, as above, but I would like to have 12 instead of 6 color/contour levels.
I would appreciate any assistance that can be provided by the community.
Thank you in advance.
JC
0 件のコメント
採用された回答
John D'Errico
2024 年 9 月 5 日
編集済み: John D'Errico
2024 年 9 月 5 日
Simple enough. You should have looked at the handle returned by plot. Though I guess it helps if you know what to look for. ;-)
Xvec= [-1.8750 -1.1250 -0.3750 -1.8750 -1.1250 -0.3750 -1.8750 -1.1250 -0.3750 -1.8750 -1.1250 -0.3750 0.3750 1.1250 1.8750 0.3750 1.1250 1.8750 0.3750 1.1250 1.8750 0.3750 1.1250 1.8750]';
Yvec=[1.6250 1.6250 1.6250 0.5000 0.5000 0.5000 -0.6250 -0.6250 -0.6250 -1.8750 -1.8750 -1.8750 -1.8750 -1.8750 -1.8750 -0.6250 -0.6250 -0.6250 0.5000 0.5000 0.5000 1.6250 1.6250 1.6250]';
Zvec= [0.5516 0.7743 1.0936 1.1194 2.1194 2.6033 1.0840 2.1033 2.7903 0.4421 1.0936 0.7645 0.7420 0.4292 0.4292 2.6228 1.7775 0.9130 2.5453 1.7389 0.8743 1.0743 0.8358 0.6712]';
Zfit1=fit([Xvec Yvec],Zvec,'cubicinterp');
figure (1)
H = plot(Zfit1,[Xvec Yvec],Zvec,'Style','Contour')
Note that H is an array of two handles. The first one is what we need.
c=colorbar;
cc=get(c,'Limits');
set(c,'Ticks',linspace(cc(1),cc(2),10))
set(gcf,'color','w');
If we take a look at what is stuffed in there,
get(H(1))
Look at what you can set there. The set of levels originally chosen by the plot code were (in LevelList):
[0.3945 0.5000 1 1.5000 2 2.5000]
It looks like the min was probably at 0.3945. So I'll choose a different set now.
H(1).LevelList = .4:.1:2.5;
3 件のコメント
John D'Errico
2024 年 9 月 5 日
As I said, these things are always simpler when you know to look there. ;-) The good thing about Answers is we all learn something new everyday. I know I do.
その他の回答 (1 件)
dpb
2024 年 9 月 5 日
編集済み: dpb
2024 年 9 月 6 日
Xvec= [-1.8750 -1.1250 -0.3750 -1.8750 -1.1250 -0.3750 -1.8750 -1.1250 -0.3750 -1.8750 -1.1250 -0.3750 0.3750 1.1250 1.8750 0.3750 1.1250 1.8750 0.3750 1.1250 1.8750 0.3750 1.1250 1.8750]';
Yvec=[1.6250 1.6250 1.6250 0.5000 0.5000 0.5000 -0.6250 -0.6250 -0.6250 -1.8750 -1.8750 -1.8750 -1.8750 -1.8750 -1.8750 -0.6250 -0.6250 -0.6250 0.5000 0.5000 0.5000 1.6250 1.6250 1.6250]';
Zvec= [0.5516 0.7743 1.0936 1.1194 2.1194 2.6033 1.0840 2.1033 2.7903 0.4421 1.0936 0.7645 0.7420 0.4292 0.4292 2.6228 1.7775 0.9130 2.5453 1.7389 0.8743 1.0743 0.8358 0.6712]';
Zfit1=fit([Xvec Yvec],Zvec,'cubicinterp');
[X,Y]=meshgrid(linspace(min(Xvec),max(Xvec)),linspace(min(Yvec),max(Yvec)));
Zpred=Zfit1(X,Y);
%whos X* Y* Z*
hC=contourf(X,Y,Zpred,12);
c=colorbar;
%cc=get(c,'Limits');
%set(c,'Ticks',linspace(cc(1),cc(2),10))
set(gcf,'color','w');
That seems strange that the builtin fit object contour option doesn't have the number of levels as a property, but doesn't seem to have.
It appears you are short of data to create a smooth plot and that there may be a case of the cubic rolling over, maybe???
figure
hC3=contour3(X,Y,Zpred,12);
figure
% repeat John's specific levels
LevelList = 0.4:0.1:2.5;
contourf(X,Y,Zpred,LevelList);
colorbar
ADDENDUM: Evaluating the fit at a finer grid smooths out the plot...
One more slight perturbation -- add a finer delta at the peak...
[min(Zpred,[],'all') max(Zpred,[],'all')]
figure
hC3=contour3(X,Y,Zpred,12);
figure
% augment John's specific levels to see shape of peak more fully
LevelList = [0.3:0.1:2.5 2.55:0.05:3.0];
contourf(X,Y,Zpred,LevelList,'ShowText',1);
colorbar
参考
カテゴリ
Help Center および File Exchange で Surface and Mesh Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!