Zoom in on a logarithmic axes

3 views (last 30 days)
Eric Kappel
Eric Kappel on 5 Nov 2022
Commented: Eric Kappel on 5 Nov 2022
Good day everyone,
I aim to have the standard axes response (in linear values) when zooming in on
a logarithmic distributed frequency axes, but, ... with linkaxes functionality
coupling 2 adjacent figures in the y- (Frequency) direction. How can I acheive this?
My tries are shown in the code below.
close all; clear clc;
%% Mathworks question
n_timepnts = 500 ; % Number of time points
n_freqs = 100 ; % Number of frequency points
Freq = logspace(0,3,n_freqs) ; % Logarithmic distribution
DataArray = rand(n_freqs,n_timepnts) ; % Some arbitrary data array
Spectrum = rand(n_freqs,1).*max(DataArray,[],2) ; % Arbitrary spectrum
time = linspace(0,365,n_timepnts); % Arbitrary time array in days
%% imagesc approach
figure(1);clf
set(1,'Position',[ 28 250 1187 420],'Name','');
ax(1) = subplot(1,9,1:2);
plot(Spectrum,log10(Freq))
xlabel('Spectral max.')
ylabel(' Frequency [Hz]')
title('Arbitrary Spectrum')
grid on;
%---
ax(2) = subplot(1,9,4:9);
imagesc(time,log10(Freq),DataArray)
set(gca,'YDir','normal');
xlabel('Day of Year')
ylabel(' Frequency [Hz]')
title('Spectral data over time')
%---
linkaxes(ax,'y')
Using the contourf approach (not preferred due to slow response)
%% contourf approach
figure(2);clf
set(2,'Position',[ 48 250 1187 420],'Name','');
ax(1) = subplot(1,9,1:2);
semilogy(Spectrum,Freq)
xlabel('Spectral max.')
ylabel(' Frequency [Hz]')
title('Arbitrary Spectrum')
grid on;
%---
[X,Y] = meshgrid(time,log10(Freq));
ax(2) = subplot(1,9,4:9);
contourf(X,Y,DataArray,'LineColor','none')
% contourf(time,Freq,DataArray,'LineColor','none')
set(gca,'YDir','normal');
xlabel('Day of Year')
ylabel(' Frequency [Hz]')
title('Spectral data over time')
%---
% linkaxes(ax,'y')
yields.
Help please!
Kind regards,
Eric

Accepted Answer

Kevin Holly
Kevin Holly on 5 Nov 2022
You can add Listeners as shown below.
%% Parameters
n_timepnts = 500 ; % Number of time points
n_freqs = 100 ; % Number of frequency points
Freq = logspace(0,3,n_freqs) ; % Logarithmic distribution
DataArray = rand(n_freqs,n_timepnts) ; % Some arbitrary data array
Spectrum = rand(n_freqs,1).*max(DataArray,[],2) ; % Arbitrary spectrum
time = linspace(0,365,n_timepnts); % Arbitrary time array in days
%% contourf approach
figure(2);clf
set(2,'Position',[ 48 250 1187 420],'Name','');
ax(1) = subplot(1,9,1:2);
semilogy(Spectrum,Freq)
xlabel('Spectral max.')
ylabel(' Frequency [Hz]')
title('Arbitrary Spectrum')
grid on;
%---
[X,Y] = meshgrid(time,log10(Freq));
ax(2) = subplot(1,9,4:9);
contourf(X,Y,DataArray,'LineColor','none')
% contourf(time,Freq,DataArray,'LineColor','none')
set(gca,'YDir','normal');
xlabel('Day of Year')
ylabel(' Frequency [Hz]')
title('Spectral data over time')
%---
% Add Listeners
addlistener(ax(1),'YLim', 'PostSet',@(~,events)set(ax(2),'YLim',log10(ax(1).YLim)))
addlistener(ax(2),'YLim', 'PostSet',@(~,events)set(ax(1),'YLim',10.^(ax(2).YLim)))
  1 Comment
Eric Kappel
Eric Kappel on 5 Nov 2022
I certainly would not have come up with such an elegant solution!!! I think I can exploit this feature a lot more!
Thank you!!!
Kind regards,
Eric

Sign in to comment.

More Answers (0)

Categories

Find more on Visual Exploration in Help Center and File Exchange

Products


Release

R2021b

Community Treasure Hunt

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

Start Hunting!