plot multiple time plots

조회 수: 6 (최근 30일)
John
John 2017년 4월 10일
답변: Kelly Kearney 2017년 4월 10일
I want to plot multiple channels of data, all against time, with one channel per plot. Possibly in more than one column if there are two many columns. Also, would like to zoom on all plots in the x axis at once. I would like the plots to be touching vertically, with x tick marks on all plots.
tSec=0:0.01:20;
nt = length(tSec);
ny = 5;
y=rand(nt,ny);
tJulian = tSec/(60*60*24) + datenum('2016-11-06 10:00');
figure;
for iy=1:ny
subplot(ny,1,iy);
plot(tJulian ,y(:,iy),'k.','markersize',2)
datetick('x','MM:SS');
if (iy~=ny)
set(gca,'xticklabel',[]);
else
xlabel('Time');
end
end
  댓글 수: 1
Adam
Adam 2017년 4월 10일
편집: Adam 2017년 4월 10일
doc linkaxes
will help with having all plots zoom together. You can specify with axis you want to be linked e.g.
linkaxes( hAxes, 'x' )
to only link the x axes of multiple axes.
To get axes where you want them you'll likely have to create them programmatically, removing ticks and things you don't want from all but e.g. the left-most plot. Subplot tends to leave a lot of blank space around plots which is not always desirable so creating your own axes and positioning them so they are right next to each other gives you greater flexibility.

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

채택된 답변

Kelly Kearney
Kelly Kearney 2017년 4월 10일
What version of Matlab are you running? Zooming and panning with a time axis is much easier with datetimes than datenumbers, but requires R2014b+.
To set all your subplots touching each other vertically, you can manually position them
hax(1) = axes('position', [0.1 0.74 0.8 0.16]);
hax(2) = axes('position', [0.1 0.58 0.8 0.16]);
etc.
There are functions on the File Exchange (e.g. subaxis.m) that make this setup very simple.
I've also written a function, plotgrid.m (which relies on subaxis.m) to combine axis setup with plotting of data to the axes. For your example, you could use it like this:
tSec=0:0.01:20;
nt = length(tSec);
ny = 5;
y=rand(nt,ny);
tJulian = seconds(tSec) + datetime('2016-11-06 10:00'); % R2014b+
% tJulian = tSec/(60*60*24) + datenum('2016-11-06 10:00');
h = plotgrid('function', {@(y) plot(tJulian,y,'k.','markersize',2), ...
num2cell(y,1)'}, 'spacingvert', 0);
linkaxes(h.ax, 'x');
set(h.ax(1:end-1), 'xticklabel', '');
xlabel(h.ax(end), 'Time');
If running a pre-datetime version of Matlab, you'll need to update the formatting of the tick labels on zoom and pan, since datetick doesn't do this; I used to use tlabel.m for this.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Grid Lines, Tick Values, and Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by