How to create static vertical line in plotting window
조회 수: 6 (최근 30일)
이전 댓글 표시
Hi there,
I'm very new to matlab, so this might be super easy. I want to create a static vertical line in the middle of a graph so that when I pan the graph, the line stays in the same place in the plotting window. Ideally, the line would stay in the middle of the plotting window as I pan the graph (so not xline). This would allow me to look at two graphs, side by side, and know which points correlate with eachother. Is there any easy way to do this? I have tried to look it up but no luck. Thanks for any help!
댓글 수: 0
채택된 답변
Meg Noah
2020년 1월 7일
편집: Meg Noah
2020년 1월 7일
This example shows one way. If you don't want the tickmarks on the vertical line, you can remove them.
clc
close all
clear all
x = -pi:0.01:pi;
y = sin(x) + 0.01*rand(size(x));
ha(1) = subplot(1,2,1);
plot(x,y);
ha(2) = subplot(1,2,2);
plot(x,y);
linkaxes(ha, 'y'); % Link all axes in y
dx = max(x)-min(x);
midpoint = (max(x)+min(x))/2;
pos1=get(ha(1),'position');
pos2=get(ha(2),'position');
Newpos = [0.1 pos1(2)-0.1 0.8 0.05];
set(ha(1),'position',[pos1(1) pos1(2) 0.5-pos1(1) 0.75]);
set(ha(2),'position',[0.5 pos2(2) (pos1(3)+pos2(1)-pos1(1))/2 0.75]);
set(ha(1),'xlim',[x(1) midpoint]);
set(ha(2),'xlim',[midpoint x(end)]);
set(ha(1),'ylim',[min(y) max(y)]);
set(ha(2),'ylim',[min(y) max(y)]);
xmax=max(x);
xticks(ha(1),[-pi -pi*0.75 -pi*0.5 -pi*0.25 0 pi*0.25 pi*0.5 pi*0.75 pi]);
xticks(ha(2),[-pi -pi*0.75 -pi*0.5 -pi*0.25 0 pi*0.25 pi*0.5 pi*0.75 pi]);
xticklabels(ha(1),{'-\pi';'-3\pi/4';'-\pi/2';'-\pi/4';'0';'\pi/4';'\pi/2';'3\pi/4';'\pi'});
xticklabels(ha(2),{'-\pi';'-3\pi/4';'-\pi/2';'-\pi/4';'0';'\pi/4';'\pi/2';'3\pi/4';'\pi'});
S=['set(ha(2),''xlim'',get(gcbo,''value'')+[0 ' num2str(dx/2) ']);' ...
'xticks(ha(2),[-pi -pi*0.75 -pi*0.5 -pi*0.25 0 pi*0.25 pi*0.5 pi*0.75 pi]);' ...
'set(ha(1),''xlim'',get(gcbo,''value'')+[' num2str(-dx/2) ' 0]);' ...
'xticks(ha(1),[-pi -pi*0.75 -pi*0.5 -pi*0.25 0 pi*0.25 pi*0.5 pi*0.75 pi])' ...
];
%% slider which is just underneath the axis
% callback string to modify xlim of the axes
h1=uicontrol('style','slider',...
'units','normalized','position',Newpos,...
'callback',S,'min',min(x),'max',max(x));
댓글 수: 5
Meg Noah
2020년 1월 8일
Here is a zoomer slider for the y-axis. But it is limited to zooming equally around the 0 point, it does not handle scrolling the y-axis. Do you need to scroll the y-axis as well?
clc
close all
clear all
x = -pi:0.01:pi;
y = sin(x) + 0.01*rand(size(x));
ha(1) = subplot(1,2,1);
plot(x,y);
ha(2) = subplot(1,2,2);
plot(x,y);
linkaxes(ha, 'y'); % Link all axes in y
dx = max(x)-min(x);
midpoint = (max(x)+min(x))/2;
pos1=get(ha(1),'position');
pos2=get(ha(2),'position');
posSliderRange = [0.1 pos1(2)-0.1 0.8 0.05];
posSliderZoom = [0.03 pos1(2) 0.05 pos1(4)-0.05];
set(ha(1),'position',[pos1(1) pos1(2) 0.5-pos1(1) 0.75]);
set(ha(2),'position',[0.5 pos2(2) (pos1(3)+pos2(1)-pos1(1))/2 0.75]);
set(ha(1),'xlim',[x(1) midpoint]);
set(ha(2),'xlim',[midpoint x(end)]);
set(ha(1),'ylim',[min(y) max(y)]);
set(ha(2),'ylim',[min(y) max(y)]);
xmax=max(x);
xticks(ha(1),[-pi -pi*0.75 -pi*0.5 -pi*0.25 0 pi*0.25 pi*0.5 pi*0.75 pi]);
xticks(ha(2),[-pi -pi*0.75 -pi*0.5 -pi*0.25 0 pi*0.25 pi*0.5 pi*0.75 pi]);
xticklabels(ha(1),{'-\pi';'-3\pi/4';'-\pi/2';'-\pi/4';'0';'\pi/4';'\pi/2';'3\pi/4';'\pi'});
xticklabels(ha(2),{'-\pi';'-3\pi/4';'-\pi/2';'-\pi/4';'0';'\pi/4';'\pi/2';'3\pi/4';'\pi'});
SRange=['set(ha(2),''xlim'',get(gcbo,''value'')+[0 ' num2str(dx/2) ']);' ...
'xticks(ha(2),[-pi -pi*0.75 -pi*0.5 -pi*0.25 0 pi*0.25 pi*0.5 pi*0.75 pi]);' ...
'set(ha(1),''xlim'',get(gcbo,''value'')+[' num2str(-dx/2) ' 0]);' ...
'xticks(ha(1),[-pi -pi*0.75 -pi*0.5 -pi*0.25 0 pi*0.25 pi*0.5 pi*0.75 pi])' ...
];
SZoom=['set(ha(1),''ylim'',[-get(gcbo,''value'') get(gcbo,''value'')]);'];
%% slider which is just underneath the axis
% callback string to modify xlim of the axes
h1=uicontrol('style','slider',...
'units','normalized','position',posSliderRange,...
'callback',SRange,'min',min(x),'max',max(x));
h2=uicontrol('style','slider',...
'units','normalized','position',posSliderZoom,...
'callback',SZoom,'min',0.1,'max',10,'value',1);
추가 답변 (1개)
Devon Fisher-Chavez
2020년 1월 9일
댓글 수: 1
Meg Noah
2020년 1월 9일
I shared a project that does two graphs on one plot here:
참고 항목
카테고리
Help Center 및 File Exchange에서 Axes Appearance에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!