How to create static vertical line in plotting window

5 次查看(过去 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!

采纳的回答

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
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
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