Contour plot of cubic interpolation with set number of contour levels

1 visualización (últimos 30 días)
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

Respuesta aceptada

John D'Errico
John D'Errico el 5 de Sept. de 2024
Editada: John D'Errico el 5 de Sept. de 2024
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')
H =
2x1 graphics array: Contour (curvefit.gui.FunctionContour) Line
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))
Annotation: [1x1 matlab.graphics.eventdata.Annotation] BeingDeleted: off BusyAction: 'queue' ButtonDownFcn: '' Children: [0x0 GraphicsPlaceholder] Clipping: on ContextMenu: [0x0 GraphicsPlaceholder] ContourMatrix: [2x560 double] CreateFcn: '' DataTipTemplate: [1x1 matlab.graphics.datatip.DataTipTemplate] DeleteFcn: '' DisplayName: '' EdgeAlpha: 1 EdgeColor: [0.0706 0.0706 0.0706] FaceAlpha: 1 FaceColor: 'flat' HandleVisibility: 'on' HitTest: off Interruptible: on LabelColor: [0.1300 0.1300 0.1300] LabelFormat: @string LabelSpacing: 144 LevelList: [0.3945 0.5000 1 1.5000 2 2.5000] LevelListMode: 'auto' LevelStep: 0.5000 LevelStepMode: 'auto' LineStyle: '-' LineWidth: 0.5000 Parent: [1x1 Axes] PickableParts: 'none' Selected: off SelectionHighlight: on ShowText: off Tag: 'curvefit.gui.FunctionContour' TextList: [0.5000 1 1.5000 2 2.5000] TextListMode: 'auto' TextStep: 0.5000 TextStepMode: 'auto' Type: 'contour' UserData: [] Visible: on XData: [51x49 double] XDataMode: 'manual' XDataSource: '' YData: [51x49 double] YDataMode: 'manual' YDataSource: '' ZData: [51x49 double] ZDataSource: '' ZLocation: 0
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 comentarios
Joseph Cunningham
Joseph Cunningham el 5 de Sept. de 2024
Thank you both for your suggestions and assistance. I'll play around with this a bit tomorrow, but it looks to be performing as I hoped! :-)
John D'Errico
John D'Errico el 5 de Sept. de 2024
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.

Iniciar sesión para comentar.

Más respuestas (1)

dpb
dpb el 5 de Sept. de 2024
Editada: dpb el 6 de Sept. de 2024
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')]
ans = 1x2
0.3937 2.9458
<mw-icon class=""></mw-icon>
<mw-icon class=""></mw-icon>
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

Categorías

Más información sobre Surface and Mesh Plots en Help Center y File Exchange.

Productos


Versión

R2024a

Community Treasure Hunt

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

Start Hunting!

Translated by