plot multiple time plots

2 次查看(过去 30 天)
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