-
Notifications
You must be signed in to change notification settings - Fork 0
/
magnifyPlot.m
66 lines (65 loc) · 2.16 KB
/
magnifyPlot.m
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
function magnifyPlot(xlm,ylm,pos,lwid,showAxisLabels,XTicks,YTicks,lgndsize)
% This function magnifies a specified rectangular area [xlm, ylm]
% in the current figure,
% and plots the magnified area on the same figure at axis defined by
% position pos
%
% Inputs:
% xlm = 1x2 vector specifying limits of x-axis
% ylm = 1x2 vector specifying limits of y-axis
% pos = 4x1 vector specifying position of new axis
% lwid = linewidth of lines in magnified plot
% showAxisLabels = boolean to turn axis labels on/off
%
% Example usage:
% x = 0:10; y1 = x; y2 = 3 - 0.5*x;
% figure;
% plot(0:10,y1,'k--','linewidth',2); hold on;
% plot(0:10,y2,'ko-','linewidth',2); hold on;
% axis square;
% xlm = [1.5,2.5];
% ylm = [1.5,2.5];
% pos = [.25 .6 .25 .25];
%
% magnifyPlot(xlm,ylm,pos,2,false)
%
% (c) Rishabh Datta, 2021-02-12
ch = get(gca,'children'); % get current figure children
for ii = 1:numel(ch)
X{ii} = get(ch(ii),'xdata'); % store all data on current figure
Y{ii} = get(ch(ii),'ydata');
clr{ii} = get(ch(ii),'Color');
ls{ii} = get(ch(ii),'LineStyle');
mkr{ii} = get(ch(ii),'Marker');
mk_clr{ii} = get(ch(ii),'MarkerFaceColor');
end
% Plot magnification rectangle
v = [xlm(1) ylm(1); ...
xlm(2) ylm(1); ...
xlm(2) ylm(2);...
xlm(1) ylm(2)]; % vertices of rectangle
patch('Faces',[1,2,3,4],'Vertices',v,...
'Edgecolor','k',...
'Facecolor','k',...
'Facealpha',0.00,...
'Linewidth',0.2,'HandleVisibility','off');% plot rectangle
axes('pos',pos) % Create axis
for ii = 1:numel(X) % plot each child
plot(X{ii},Y{ii},'Color',clr{ii},'LineStyle',ls{ii},'LineWidth',1.2,...
'Marker',mkr{ii},'MarkerFaceColor',mk_clr{ii},'Linewidth',lwid);...
hold on;grid on; grid minor;
end
xlim(xlm); ylim(ylm); % set limits of zoomed plot
set(gca,'XColor','k', 'YColor','k','fontsize',lgndsize);
set(gca, 'LineWidth',1.2);
grid on; grid minor;
if ~showAxisLabels % turn on/off labels
set(gca,'xticklabel',[])
set(gca,'yticklabel',[])
grid on; grid minor;
else
set(gca,'XTick',XTicks,'YTick',YTicks)
grid on; grid minor;
xtickangle(20); ytickangle(-20)
end
end