How to create static vertical line in plotting window

조회 수: 6 (최근 30일)
Devon Fisher-Chavez
Devon Fisher-Chavez 2020년 1월 2일
댓글: Meg Noah 2020년 1월 9일
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!

채택된 답변

Meg Noah
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
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);
Devon Fisher-Chavez
Devon Fisher-Chavez 2020년 1월 8일
Hi there, thanks so much for your help! Unfortunately, I need to be able to zoom in both the x and y direction. Being able to pan in the y direction would be useful as well. I am trying to QC raw environmental data over a year long period that varies considerably. So the solution would need to be pretty flexible.
I like the integrated matlab zooming and paning options.. I was hoping there would be an easy way to just transpose a vertical line down the middle of a graphing window. I guess this is a more difficult challenge than I anticipated! Thank you so much again though!

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Devon Fisher-Chavez
Devon Fisher-Chavez 2020년 1월 9일
I found this website that shows how to layer two sets of data on a single graph. Will hopefully allow me to do what I want with the whole static line thing. I will post any code I come up with.

카테고리

Help CenterFile Exchange에서 Axes Appearance에 대해 자세히 알아보기

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!

Translated by