plot(fit, 'Style', 'Contour') resolution when using larger grids?
6 次查看(过去 30 天)
显示 更早的评论
I am trying to plot a fit as a contour plot in my application analog to the contour plot in the curvefitting toolbox but I am running into what seems to be a rendering issue when using larger grids as can be observed below.
![Plot of fit in full grid](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1572747/Plot%20of%20fit%20in%20full%20grid.png)
![Plot of fit in cropped grid](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1572752/Plot%20of%20fit%20in%20cropped%20grid.png)
In both images the same fit is plotted once in a small grid (clipped at the border of the fit location) once in the whole grid. I am looking for a more detailed plot of the contours due to the fact that there are multiple fits on oither locations in this grid I would like to plot in the same axis.
It seems the rendering of the contour levels is less detailed.
I tried to find documentation on the plot(..., 'Style', 'Contour') option but so far have not found much in the help of Matlab, nor the community forums.
I would like to have the same rendering quality of the contours in the full grid as the cropped grid. Any pointers on how to do so?
In order to be able to reproduce my example
%% Spanning grid: this is normally extracted from the data, but this is for easy reproducibility
[xg, yg] = meshgrid([-130:5:130], [-130:5:130]);
xyg = reshape([xg yg], [], 2);
%% Setup fit type and fit options
ft = fittype(...
'a*exp(-((x-b).^2/(2*c^2)+(y-d).^2/(2*e^2)))', ...
'independent', {'x', 'y'}, ...
'dependent', 'z' );
fitopts = fitoptions(...
'Method', 'NonlinearLeastSquares', ...
'Algorithm', 'Levenberg-Marquardt', ...
'Display', 'Off');
%% Create fit object for reproducability
% NOTE: This is normally done using actual 2d gaussian fit, but for reproducability this should be enough
% Arbitrary fit to allow us to assign our values for generation of spots
xt = 3 - 6 * rand( 49, 1 );
yt = 3 - 6 * rand( 49, 1 );
fitobj = fit([xt, yt], peaks(xt, yt), ft, fitopts);
% Set our fit outcome to created fit for demonstrative purposes
[fitobj.a, fitobj.b, fitobj.d, fitobj.c, fitobj.e] = ...
deal(1.400, 80.8763, -80.2487, 5.0000, 5.0000);
%% Determine indexes for current spot
indexes = yg > -100 & yg < -60 & xg > 60 & xg < 100;
% Read dosis from file (normally from DICOM file), for source see file attached to post
DOSE = readmatrix('dose.txt');
%% Plot
figure('Name', 'Plot fit in cropped grid', 'NumberTitle', 'off');
plot(fitobj, ...
xyg(indexes, :), ...
DOSE(indexes), ...
'Style', 'Contour');
axis equal tight
figure('Name', 'Plot fit in full grid', 'NumberTitle', 'off');
plot(fitobj, ...
xyg, ...
DOSE(:), ...
'Style', 'Contour')
axis equal tight
0 个评论
采纳的回答
Voss
2023-12-19
You can specify XLim and YLim in the plot() call, and doing that gives you the higher resolution contour. The problem then is that the contour is all white outside of the region defined by those XLim and YLim, but you can correct for that by setting the axes Color.
Here's an example that plots two fit contours at high resolution on the same axes:
%% Spanning grid: this is normally extracted from the data, but this is for easy reproducibility
[xg, yg] = meshgrid([-130:5:130], [-130:5:130]);
xyg = reshape([xg yg], [], 2);
%% Setup fit type and fit options
ft = fittype(...
'a*exp(-((x-b).^2/(2*c^2)+(y-d).^2/(2*e^2)))', ...
'independent', {'x', 'y'}, ...
'dependent', 'z' );
fitopts = fitoptions(...
'Method', 'NonlinearLeastSquares', ...
'Algorithm', 'Levenberg-Marquardt', ...
'Display', 'Off');
%% Create fit object for reproducability
% NOTE: This is normally done using actual 2d gaussian fit, but for reproducability this should be enough
% Arbitrary fit to allow us to assign our values for generation of spots
xt = 3 - 6 * rand( 49, 1 );
yt = 3 - 6 * rand( 49, 1 );
fitobj = fit([xt, yt], peaks(xt, yt), ft, fitopts);
% Set our fit outcome to created fit for demonstrative purposes
fitobj1 = fitobj;
[fitobj1.a, fitobj1.b, fitobj1.d, fitobj1.c, fitobj1.e] = ...
deal(1.400, 80.8763, -80.2487, 5.0000, 5.0000);
fitobj2 = fitobj;
[fitobj2.a, fitobj2.b, fitobj2.d, fitobj2.c, fitobj2.e] = ...
deal(1.400, -40.8763, 40.2487, 5.0000, 5.0000);
% Read dosis from file (normally from DICOM file), for source see file attached to post
DOSE = readmatrix('dose.txt');
%% Plot (using a large figure so it shows up here properly)
f = figure('Name', 'Plot fit in full grid', 'NumberTitle', 'off', 'Position', [1 1 1000 1000]);
plot(fitobj1, ...
xyg, ...
DOSE(:), ...
'XLim',[65 95], ...
'YLim',[-95 -65], ...
'Style', 'Contour')
hold on
plot(fitobj2, ...
xyg, ...
DOSE(:), ...
'XLim',[-55 -25], ...
'YLim',[25 55], ...
'Style', 'Contour')
axis equal tight
cmap = get(f,'Colormap');
set(gca(),'Color',cmap(1,:))
3 个评论
Voss
2023-12-20
You're welcome!
There are two relevant toolbar buttons here: one in the CODE section of the toolbar that says "Insert a line of code (Alt+Enter)" , and one in the INSERT section that says "Insert MATLAB code example". I used the first one, which allows you to run the code, and you used the second one, which does not allow you to run the code.
更多回答(0 个)
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Get Started with Curve Fitting Toolbox 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!