%% Region-of-Interest Plot
% A plot can span multiple tiles of a layout. You can use this feature to display a main plot with an accompanying inset plot. In this case, the inset plot shows a zoomed-in view of a region of interest.
% Create a 3-by-3 tiled chart layout, and create an axes that spans all of the tiles. Then display a scatter plot in the axes.

figure
t = tiledlayout(3,3,'Padding','compact');
ax1 = nexttile(1,[3 3]); 
x = randn(2000,1);
y = randn(2000,1);
scatter(ax1,x,y,1,'filled');

%
% Next, create a rectangle that defines the region of interest in the scatter plot. Set properties on the axes last, to ensure that those properties persist.
% Define the bounds of the rectangle. Define left and bottom as the left and bottom edges of the rectangle. Specify left in x-axis units, and specify bottom in y-axis units. Similarly, define width and height as the width and height of the rectangle, also in axis units.
% Display the rectangle. Call the hold function to preserve the contents of the axes, and then call the rectangle function.
% Set properties on the axes. Set the font size to 10 points, set the x- and y-axis limits, and turn the grid on.

% Define bounds of the rectangle
left = -0.5;
bottom = -0.7;
width = 0.4;
height = 0.4;

% Display the rectangle
hold(ax1,'on');
r = rectangle('Position',[left bottom width height], ...
    'EdgeColor','red','LineWidth',1.5);

% Set properties on the axes
ax1.FontSize = 10;
ax1.XLim = [-4.5 4.5];
ax1.YLim = [-4.5 4.5];
grid(ax1,'on')

%
% Display a smaller, zoomed-in plot with x- and y-axis limits that match the region of interest.
% Create the axes for the zoomed-in view. Call the axes function to create the axes. Move the axes to the third tile by setting the Layout.Tile property. Then plot the entire set of x and y data.
% Adjust the axis limits to match the region of interest. Set the XLim and YLim properties of ax2 to match the region of interest. Then remove the ticks from the plot box.
% Set other properties on the axes. Turn the axes box on, set the x- and y- axis colors to red, and display a title.

% Create axes for zoomed-in view
ax2 = axes(t);
ax2.Layout.Tile = 3;
scatter(ax2,x,y,10,'filled');

% Adjust axis limits and remove ticks
ax2.XLim = [left left+width];
ax2.YLim = [bottom bottom+height];
ax2.XTick = [];
ax2.YTick = [];

% Set other properties on the axes
ax2.Box = 'on';
ax2.XAxis.Color = 'red';
ax2.YAxis.Color = 'red';
title(ax2,'100x Magnification','Color','red');

% Copyright 2020 The MathWorks, Inc.