Why doesn't the figure show the text and fitting line?
조회 수: 26 (최근 30일)
이전 댓글 표시
Text in the left corner and fitting line is missing from my fiures, please make correction to my code:
% Define heights, FOVs, and SNR values to test
heights = [1000, 2000, 3000, 4000];
fovs = [0.2, 0.5, 1, 2, 5, 10];
snr_values = [0, 25, 50, 75, 100];
% Function to calculate performance metrics
calculate_r_squared = @(x, y) 1 - sum((y - x).^2) / sum((y - mean(y)).^2);
calculate_rmse = @(x, y) sqrt(mean((y - x).^2));
calculate_mape = @(x, y) mean(abs((y - x) ./ y)) * 100;
calculate_mae = @(x, y) mean(abs(y - x));
calculate_made = @(x, y) mean(abs(y - mean(x)));
% Initialize arrays to store performance metrics
performance_metrics = struct();
% Loop over height values
for h = heights
% Filter the data for the current height
idx = (lookup_table(:, 1) == h);
data_filtered = lookup_table(idx, :);
% Initialize arrays to store performance metrics for each FOV and SNR value
performance_metrics(h).r_squared_r = zeros(length(fovs), length(snr_values));
performance_metrics(h).rmse_r = zeros(length(fovs), length(snr_values));
performance_metrics(h).mape_r = zeros(length(fovs), length(snr_values));
performance_metrics(h).mae_r = zeros(length(fovs), length(snr_values));
performance_metrics(h).made_r = zeros(length(fovs), length(snr_values));
performance_metrics(h).r_squared_a = zeros(length(fovs), length(snr_values));
performance_metrics(h).rmse_a = zeros(length(fovs), length(snr_values));
performance_metrics(h).mape_a = zeros(length(fovs), length(snr_values));
performance_metrics(h).mae_a = zeros(length(fovs), length(snr_values));
performance_metrics(h).made_a = zeros(length(fovs), length(snr_values));
% Plot optimal_r_input vs. optimal_r_interp
figure;
hold on;
colors = jet(length(fovs) * length(snr_values));
c_idx = 1;
for fov_idx = 1:length(fovs)
for snr_idx = 1:length(snr_values)
fov = fovs(fov_idx);
snr = snr_values(snr_idx);
% Filter data for the current FOV and SNR
idx_fov_snr = (data_filtered(:, 2) == fov) & (data_filtered(:, 3) == snr);
optimal_r_input = data_filtered(idx_fov_snr, 4);
optimal_r_interp = data_filtered(idx_fov_snr, 5);
% Scatter plot
if ~isempty(optimal_r_input)
scatter(optimal_r_input, optimal_r_interp, 50, colors(c_idx, :), 'filled');
% Fit and plot linear regression line if there is sufficient data
if length(optimal_r_input) > 1
model_r = fitlm(optimal_r_input, optimal_r_interp);
plot(model_r.Variables.x1, model_r.Fitted, 'Color', colors(c_idx, :), 'LineWidth', 2);
% Calculate additional performance metrics
r_squared_r = model_r.Rsquared.Ordinary;
rmse_r = calculate_rmse(optimal_r_input, optimal_r_interp);
mape_r = calculate_mape(optimal_r_input, optimal_r_interp);
mae_r = calculate_mae(optimal_r_input, optimal_r_interp);
made_r = calculate_made(optimal_r_input, optimal_r_interp);
% Store the performance metrics for this FOV and SNR value
performance_metrics(h).r_squared_r(fov_idx, snr_idx) = r_squared_r;
performance_metrics(h).rmse_r(fov_idx, snr_idx) = rmse_r;
performance_metrics(h).mape_r(fov_idx, snr_idx) = mape_r;
performance_metrics(h).mae_r(fov_idx, snr_idx) = mae_r;
performance_metrics(h).made_r(fov_idx, snr_idx) = made_r;
% Display text with performance metrics
text(mean(optimal_r_input), mean(optimal_r_interp), ...
{['SNR = ', num2str(snr), ' dB'], ...
['R^2 = ', num2str(r_squared_r)], ...
['RMSE = ', num2str(rmse_r)], ...
['MAPE = ', num2str(mape_r), '%'], ...
['MAE = ', num2str(mae_r)], ...
['MADE = ', num2str(made_r)]}, ...
'FontSize', 10, 'Color', colors(c_idx, :));
end
end
c_idx = c_idx + 1;
end
end
xlabel('Optimal R_{e} (\mum)');
ylabel('Optimal R_{e} interp (\mum)');
title(['Plot of optimal R_{e} and optimal R_{e} interp for Height = ', num2str(h)]);
grid on;
hold off;
% Plot optimal_a_input vs. optimal_a_interp
figure;
hold on;
c_idx = 1;
for fov_idx = 1:length(fovs)
for snr_idx = 1:length(snr_values)
fov = fovs(fov_idx);
snr = snr_values(snr_idx);
% Filter data for the current FOV and SNR
idx_fov_snr = (data_filtered(:, 2) == fov) & (data_filtered(:, 3) == snr);
optimal_a_input = data_filtered(idx_fov_snr, 6);
optimal_a_interp = data_filtered(idx_fov_snr, 7);
% Scatter plot
if ~isempty(optimal_a_input)
scatter(optimal_a_input, optimal_a_interp, 50, colors(c_idx, :), 'filled');
% Fit and plot linear regression line if there is sufficient data
if length(optimal_a_input) > 1
model_a = fitlm(optimal_a_input, optimal_a_interp);
plot(model_a.Variables.x1, model_a.Fitted, 'Color', colors(c_idx, :), 'LineWidth', 2);
% Calculate additional performance metrics
r_squared_a = model_a.Rsquared.Ordinary;
rmse_a = calculate_rmse(optimal_a_input, optimal_a_interp);
mape_a = calculate_mape(optimal_a_input, optimal_a_interp);
mae_a = calculate_mae(optimal_a_input, optimal_a_interp);
made_a = calculate_made(optimal_a_input, optimal_a_interp);
% Store the performance metrics for this FOV and SNR value
performance_metrics(h).r_squared_a(fov_idx, snr_idx) = r_squared_a;
performance_metrics(h).rmse_a(fov_idx, snr_idx) = rmse_a;
performance_metrics(h).mape_a(fov_idx, snr_idx) = mape_a;
performance_metrics(h).mae_a(fov_idx, snr_idx) = mae_a;
performance_metrics(h).made_a(fov_idx, snr_idx) = made_a;
% Display text with performance metrics
text(mean(optimal_a_input), mean(optimal_a_interp), ...
{['SNR = ', num2str(snr), ' dB'], ...
['R^2 = ', num2str(r_squared_a)], ...
['RMSE = ', num2str(rmse_a)], ...
['MAPE = ', num2str(mape_a), '%'], ...
['MAE = ', num2str(mae_a)], ...
['MADE = ', num2str(made_a)]}, ...
'FontSize', 10, 'Color', colors(c_idx, :));
end
end
c_idx = c_idx + 1;
end
end
xlabel('Optimal \alpha_{e} (m^{-1})');
ylabel('Optimal \alpha_{e} interp (m^{-1})');
title(['Plot of optimal \alpha_{e} vs optimal \alpha_{e} interp for Height = ', num2str(h)]);
grid on;
hold off;
end
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1757409/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1757414/image.png)
댓글 수: 7
Walter Roberson
대략 3시간 전
편집: Walter Roberson
대략 3시간 전
It is not possible for you to get the desired output with that code.
Your input table is such that it goes through all possible combinations of height, fov, and snr, with each combination appearing exactly once. But you only emit the text() if there is more than one match for a combination of height, fov, and snr -- which is something that never occurs in your table.
If you examine your table, you see that it holds the height constant for a while. Within height, it holds fov constant for a while, iterating over all snr. Then it moves on to the next fov within the height, and lists all snr. Then it moves on to the next fov, and so on. When all of the combinations of fov and snr have been run through, it moves on to the next height. At no point is there a duplicate combination of height, fov, and snr.
답변 (1개)
arushi
2024년 8월 22일 4:38
Hi Wiqas,
The code seems to be correct.To ensure that the text annotations and fitting lines are properly displayed on your figures, please consider the following adjustments and suggestions:
Ensure Text is Visible:
- Make sure the text is positioned within the plot limits. If the text is outside the axes limits, it won't be visible. Use the xlim and ylim functions to adjust the axes limits if necessary.
Adjust Text Position:
- Position the text based on the data range to ensure it is visible. You can use relative positions or fixed offsets to ensure the text doesn't overlap with data points.
Ensure Fitting Line is Plotted:
- Verify that the fitlm function is being called with sufficient data points. If there are too few points, the fitting line may not be plotted.
Hope this helps.
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Annotations에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!