- There is no data in the zoom region, as you can see > Select the zoom region as per the values.
- The zoom region and zoom range don't match > Modify the search parameters accordingly.
How plot 2D with inset zoom region?
8 views (last 30 days)
Show older comments
i want plot 2D with zooming region i try to change something and make it more easier but not give me a better view i want something like the picture 

but my code is so different
clear; clc; close all;
% Create high-resolution data for smooth curves
y = linspace(-20, 20, 5000);
% Create simple functions like in your image
exact_func = 0.12 * sech(y/3).^2;
ladm_func = 0.12 * sech(y/3).^2 .* (1 - 0.001*sin(y*3));
% Main plot
figure('Position', [100, 100, 800, 600]);
plot(y, exact_func, 'b-', 'LineWidth', 3); hold on;
plot(y, ladm_func, 'r--', 'LineWidth', 2);
xlabel('y', 'FontSize', 12);
ylabel('|U(y,t)|^2', 'FontSize', 12);
legend('Exact', 'LADM', 'Location', 'northwest', 'FontSize', 10);
grid on;
xlim([-20, 20]);
ylim([0, 0.13]);
% Define zoom region
zoom_x_center = 8;
zoom_y_center = 0.01;
% Draw simple circle around zoom region
theta = linspace(0, 2*pi, 100);
circle_x = zoom_x_center + 1.5*cos(theta);
circle_y = zoom_y_center + 0.003*sin(theta);
plot(circle_x, circle_y, 'k-', 'LineWidth', 1.5);
% Create inset
axes('Position', [0.6, 0.6, 0.3, 0.25]);
zoom_range = [7.6, 7.7];
zoom_idx = find(y >= zoom_range(1) & y <= zoom_range(2));
plot(y(zoom_idx), exact_func(zoom_idx), 'b-', 'LineWidth', 2); hold on;
plot(y(zoom_idx), ladm_func(zoom_idx), 'r--', 'LineWidth', 2);
xlim([7.60, 7.70]);
ylim([0.0102, 0.0110]);
grid on; box on;
set(gca, 'FontSize', 8);
% Simple arrow
annotation('arrow', [0.5, 0.6], [0.35, 0.6], 'LineWidth', 1.5);
fprintf('Simple inset zoom created!\n');
0 Comments
Accepted Answer
Dyuman Joshi
on 18 Jun 2025
Two things -
I have added comments in the code below, starting with %%
clear; clc; close all;
% Create high-resolution data for smooth curves
y = linspace(-20, 20, 5000);
% Create simple functions like in your image
exact_func = 0.12 * sech(y/3).^2;
ladm_func = 0.12 * sech(y/3).^2 .* (1 - 0.001*sin(y*3));
% Main plot
figure('Position', [100, 100, 800, 600]);
plot(y, exact_func, 'b-', 'LineWidth', 3); hold on;
plot(y, ladm_func, 'r--', 'LineWidth', 2);
xlabel('y', 'FontSize', 12);
ylabel('|U(y,t)|^2', 'FontSize', 12);
legend('Exact', 'LADM', 'Location', 'northwest', 'FontSize', 10);
grid on;
xlim([-20, 20]);
ylim([0, 0.13]);
% Define zoom region
%% Select zoom region center nearby data points
zoom_x_center = 5;
zoom_y_center = 0.02;
% Draw simple circle around zoom region
theta = linspace(0, 2*pi, 100);
circle_x = zoom_x_center + 1.5*cos(theta);
circle_y = zoom_y_center + 0.003*sin(theta);
plot(circle_x, circle_y, 'k-', 'LineWidth', 1.5);
% Create inset
axes('Position', [0.6, 0.6, 0.3, 0.25]);
%% Specify Zoom range nearby the zoom center
zoom_range = 5+[-0.5 0.5];
zoom_idx = y >= zoom_range(1) & y <= zoom_range(2);
plot(y(zoom_idx), exact_func(zoom_idx), 'b-', 'LineWidth', 2); hold on;
plot(y(zoom_idx), ladm_func(zoom_idx), 'r--', 'LineWidth', 2);
xlim(zoom_range);
%% y-limit will be automatically adjusted
grid on; box on;
set(gca, 'FontSize', 8);
% Simple arrow
%% You can edit the x,y coordinates for arrow as you wish
annotation('arrow', [0.62, 0.65], [0.26, 0.58], 'LineWidth', 1.5);
fprintf('Simple inset zoom created!\n');
0 Comments
More Answers (0)
See Also
Categories
Find more on Data Exploration in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
