plot multiple time plots

8 views (last 30 days)
John
John on 10 Apr 2017
Answered: Kelly Kearney on 10 Apr 2017
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 Comment
Adam
Adam on 10 Apr 2017
Edited: Adam on 10 Apr 2017
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.

Sign in to comment.

Accepted Answer

Kelly Kearney
Kelly Kearney on 10 Apr 2017
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.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!