MATLAB Answers

plot multiple time plots

19 views (last 30 days)
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.
nt = length(tSec);
ny = 5;
tJulian = tSec/(60*60*24) + datenum('2016-11-06 10:00');
for iy=1:ny
plot(tJulian ,y(:,iy),'k.','markersize',2)
if (iy~=ny)
  1 Comment
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.

Answers (1)

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]);
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:
nt = length(tSec);
ny = 5;
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(, 'x');
set(, 'xticklabel', '');
xlabel(, '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.

Community Treasure Hunt

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

Start Hunting!