How plot 2D with inset zoom region?
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');
Accepted Answer
More Answers (0)
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!
