spectrogram x-axis match with data
Show older comments
Hello, I am plotting a spectrogram from an earthquake signal, but the time length of the signal (~250 seconds) does not match the time length displayed in the spectrogram (~100 seconds). Could you help me understand why this discrepancy occurs and how to fix it? Thank you!

2 Comments
Mathieu NOE
on 22 Jan 2025
that is quite strange , but I cannot tell the reason as you don't show the code
to be sure to have same x axis display you can use xlim (with same values) on both subplots
Jorge Luis
on 22 Jan 2025
Accepted Answer
More Answers (1)
this can also helps you :
%%Method 1: equate axis sizes for all axes after adding colorbar
% Size of axes without a colorbar will be set to the size of axes with a colorbar.
% Create Demo
ax = gobjects(2,3);
for i = 1:5
ax(i) = subplot(3,2,i);
imagesc(ax(i),rand(100,1000))
colorbar(ax(i))
end
ax(end) = subplot(3,2,6);
plot(ax(end), 1:1000,(1:1000).*rand(1,1000)*2)
% Set the width and height of all axes to the min width & height
allAxPos = vertcat(ax.Position);
allAxPos(:,3:4) = min(allAxPos(:,3:4)).*ones(numel(ax),1);
set(ax,{'position'},mat2cell(allAxPos,ones(numel(ax),1),4))
%%Method 2: Restore axis position and set colobar positions
% The axes maintain their original sizes prior to adding colorbar.
% The colorbar position and width is adjusted.
function addOutsideColorbar(ax)
pos = ax.Position;
cb = colorbar(ax,'EastOutside');
ax.Position = pos;
cb.Position(1) = sum(ax.Position([1,3]))+.01;
cb.Position(3) = cb.Position(3).*.5;
end
ax = gobjects(2,3);
for i = 1:5
ax(i) = subplot(3,2,i);
imagesc(ax(i),rand(100,1000))
addOutsideColorbar(ax(i))
end
ax(end) = subplot(3,2,6);
plot(ax(end), 1:1000,(1:1000).*rand(1,1000)*2)
%%Method 3: Use tiledlayout
ax = gobjects(2,3);
tlo = tiledlayout(3,2);
for i = 1:5
ax(i) = nexttile(tlo);
imagesc(ax(i),rand(100,1000))
colorbar(ax(i))
end
ax(end) = nexttile(tlo);
plot(ax(end), 1:1000,(1:1000).*rand(1,1000)*2)
Categories
Find more on Data Distribution Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


