Help using grid in nexttile and tiledlayout
12 次查看(过去 30 天)
显示 更早的评论
Hello Matlab Community
I will be appreciated if someone could help me with the follow inquiry:
After reading information about tiledlayout (https://www.mathworks.com/help/matlab/ref/tiledlayout.html) and nexttile (https://www.mathworks.com/help/matlab/ref/nexttile.html) I could not found the way to show the grid in all sub plots and show the numbers in all the sub plots (look attached file: matlab1.png) in the following code
clear;
data = readmatrix('GeoUNAM04042011L2L407-04-2011UT_Modificado_II');
n=50;
T = data(:,1);
T = seconds(data(:,1));
T.Format = 'hh:mm';
T3 = data(:,3);
m3 = movmean (T3,n);
T4 = data(:,4);
m4 = movmean (T4,n);
T5 = data(:,5);
m5 = movmean (T5,n);
T6 = data(:,6);
m6 = movmean (T6,n);
T7 = data(:,7);
m7 = movmean (T7,n);
T8 = data(:,8);
m8 = movmean (T8,n);
T9 = data(:,9);
m9 = movmean (T9,n);
T10 = data(:,10);
m10 = movmean (T10,n);
T11 = data(:,11);
m11 = movmean (T11,n);
T12 = data(:,12);
m12 = movmean (T12,n);
T13 = data(:,13);
m13 = movmean (T13,n);
T14 = data(:,14);
m14 = movmean (T14,n);
T15 = data(:,15);
m15 = movmean (T15,n);
T16 = data(:,16);
m16 = movmean (T16,n);
t = tiledlayout(7,2);
t.TileSpacing = 'tight';
t.Padding = 'tight';
ax1 = nexttile;
grid(ax1,'on')
plot (T,m3,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title ('\fontsize{6}Emitted Signal from NAA Station');
ylabel ('Phase');
%xlabel ('Samples');
ax2 = nexttile;
grid(ax2,'on')
plot (T,m4,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NAA Station';
ylabel ('Apmlitude');
% xlabel ('Samples');
nexttile
plot (T,m5,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NLK Station';
ylabel ('Phase');
% xlabel ('Samples');
nexttile
plot (T,m6,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NLK Station';
ylabel ('Amplitude');
% xlabel ('Samples');
nexttile
plot (T,m7,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NDK Station';
ylabel ('Phase');
% xlabel ('Samples');
nexttile
plot (T,m8,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NDK Station';
ylabel ('Amplitude');
% xlabel ('Samples');
nexttile
plot (T,m9,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NWC Station';
ylabel ('Phase');
% xlabel ('Samples');
nexttile
plot (T,m10,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NWC Station';
ylabel ('Amplitude');
% xlabel ('Samples');
nexttile
plot (T,m11,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NPM Station';
ylabel ('Phase');
% xlabel ('Samples');
nexttile
plot (T,m12,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NPM Station';
ylabel ('Amplitude');
% xlabel ('Samples');
nexttile
plot (T,m13,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NAU Station';
ylabel ('Phase');
% xlabel ('Samples');
nexttile
plot (T,m14,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NAU Station';
ylabel ('Amplitude');
% xlabel ('Samples');
nexttile
plot (T,m15,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NAA II Station';
ylabel ('Phase');
% xlabel ('Samples');
nexttile
plot (T,m16,'-');
set(gca,'YTick',[]);
set(gca,'XTick',[]);
title '\fontsize{6}Emitted Signal from NAA II Station';
ylabel ('Amplitude');
% xlabel ('Samples');
Thanks in advance for your cooperation.
0 个评论
回答(2 个)
Star Strider
2023-8-11
If you want the tick labels just above the x-axis, that is certainly possible.
Try this —
data = readmatrix('GeoUNAM0404201...ficado_II.txt')
n=50;
stn = repelem({'NAA','NLK','NDK','NWC','NPM','NAU','NAA II'}, 1, 2);
T = data(:,1);
T = seconds(data(:,1));
T.Format = 'hh:mm';
max(data(:,1))
figure
t = tiledlayout(7,2);
t.TileSpacing = 'tight';
t.Padding = 'tight';
for k = 3:16
nexttile
plot(T, movmean(data(:,k),n))
yl = ylim;
xt = xticks;
[h,m] = hms(xt);
text(xt, zeros(size(xt))+min(yl),compose('%02d:%02d',[h m]), 'VerticalAlignment','bottom', 'HorizontalAlignment','center', 'FontSize',8)
xticks([])
yticks([])
if rem(k,2) == 0;
ylabel('Amplitude')
else
ylabel('Phase')
end
title(sprintf('\\fontsize{6}Emitted Signal from %s Station',stn{k-2}));
end
Make appropriate changes to get the result you want.
There are only 7 listed stations, so there can be only 14 plots.
The x-tick data are in seconds, and the time vector goes only up to 50.9, so all the hour and minute fields will be blank. You may want to change ‘T.Format’ to reflect that.
.
0 个评论
dpb
2023-8-11
编辑:dpb
2023-8-11
The two lines
set(gca,'YTick',[]);
set(gca,'XTick',[]);
remove the tick marks and thereby the ticklabels.
Remove those and they'll show up. You've got too much data on a plot this way, however, probably in order to be able to add for every plot and still see. But, give it a try.
As general MATLAB programming practice; don't create a zillion sequentially named variables that are just copies of the array; use indexing into the array itself and loops and a lookup table for the metadata such as the titles if it isn't available from an auxiliary data source...
Something more like
n=50;
data = readmatrix('GeoUNAM0404201...ficado_II.txt');
T=seconds(data(:,1)); T.Format='mm:ss'; % ok, I'll allow converting to duration
%[data(1,1) data(end,1)]
V1=3; NV=7; % first column variable to plot, number signals
data(:,V1:end)=movmean(data(:,V1:end),n); % do the moving mean on 'em all first
hTL = tiledlayout(NV,2); % make variable so can change mind easily
hTL.TileSpacing = 'tight';
hTL.Padding = 'tight';
YLABELS={'Phase','Ampl'}; % y labels, shorten "amplitude"
TITLES={'Emitted Signal from NAA Station'; % titles for each pair
'Emitted Signal from NLK Station';
'Emitted Signal from NDK Station';
'Emitted Signal from NWC Station';
'Emitted Signal from NPM Station';
'Emitted Signal from NAU Station';
'Emitted Signal from NAA II Station'};
% v=V1-1; % first column of pairs to plot less one
% for i=1:NV*2
% for j=1:2
% hAx=nexttile; % get a handle for current axes
% v=v+1; % increment column
% plot(T,data(:,v),'-');
% ylabel(YLABELS(j));
% hAx.FontSize=6;
% title(TITLES(i),'fontsize',6);
% end
% end
% let's try legend instead
v=V1-1; % first column of pairs to plot less one
for i=1:NV
for j=1:2
hAx=nexttile; % get a handle for current axes
v=v+1; % increment column
plot(T,data(:,v),'-');
ylabel(YLABELS(j));
hAx.FontSize=6;
station=extractBetween(TITLES(i),'from ',' Station');
%title(TITLES(i),'fontsize',6);
legend(station,'fontsize',6);
end
end
Trying to put into too little space; you'll have to see what you can do to cut things down some. You might want to consider converting to a table and looking at stackedplot, It does only one set of axes vertically, not two but you can use panels so theoretically, at least, you could put two side-by-side in a figure. It has the one advantage over either tiled layout or subplots that it uses a common x axis. Of course, one could only display the time axis here for the bottom plot and gain some space that way as one fix/help.
The basic problem is just too much; possiby dispense with the title and just use legend with only the station name without all the other verbiage. Well, forgot about the line...it takes up too much space so isn't great. I guess just an anotation/text or use it in the ylabel string for left. A global title by column for "Phase/Amplitude" would be good option, but don't think that exists except manually.
0 个评论
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Data Exploration 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!