why does my colorbar shift my second y axis?
    4 visualizzazioni (ultimi 30 giorni)
  
       Mostra commenti meno recenti
    
hy guys 
i am trying to add a colorbar to my second y axis in the folowinf code, however the colorbar is shifting the axis.
any idea how to fix it plz.
thank you in advance
best regards
the code is the following:
code:
clear all
clc
a=randi(10,10,20);
x=linspace(-1,1,20);
y=linspace(-1,1,10);
imagesc(x,y,a)
ax1=gca;
ax2=axes('Position',ax1.Position,'XAxisLocation','top','xlim',[1 size(a,2)],'fontweight','bold','fontsize',10,...
    'YAxisLocation','right','ylim',[1 size(a,1)],'color','none');
link_axis(ax1,ax2);
colorbar
%% 
function link_axis(ax1,ax2)
ax1.Box = 'off';
% if you uncomment " ax1.Interactions = []; " then you can play only with
% the second axis
% ax1.Interactions = []; 
ax1.Toolbar.Visible = 'off'; 
% Compute scaling factor to convert ax1 scale from ax2 scale
% xyscale is 2x1 factors of [x;y] axes
xyscale = [range(ax1.XLim) / range(ax2.XLim); range(ax1.YLim) / range(ax2.YLim)];
% Store original axis limits for both axes
% axBaseLim is 2x2x2 axis limits of [x;y] axes, [min,max] limits; and
% [ax1,ax2] along the 3rd dimension
axBaseLim = [ax1.XLim; ax1.YLim];           % ax1
axBaseLim(:,:,2) = [ax2.XLim; ax2.YLim];    % ax2
% Assign listener
ax2.UserData.Listener = addlistener(ax2,{'XLim','YLim'}, 'PostSet', ...
    @(~,~)axisLimitListener([], [], [ax1,ax2], xyscale, axBaseLim)); 
% Fix restoreview button
axTB = axtoolbar(ax2,'default'); 
isRestoreButton = strcmpi({axTB.Children.Icon},'restoreview');
if any(isRestoreButton)
    restoreButtonHandle = axTB.Children(isRestoreButton);
    originalRestoreFcn = restoreButtonHandle.ButtonPushedFcn;
    restoreButtonHandle.ButtonPushedFcn = ...
        {@myRestoreButtonCallbackFcn, ax1, originalRestoreFcn, xyscale, axBaseLim};
end
    function axisLimitListener(~,~,ax,scalingFactor,axBaseLim)
        % Listener callback that responds to x/y axis limit changes to ax2 and 
        % updates the axis limits to ax1. 
        % INPUTS
        %   ax: 1x2 array of axis handles to [ax1,ax2]
        %   scalingFactor: (see description of xyscale above)
        %   axBaseLim: (see description of axBaseLim above)
        % Convert the lower axis limits from ax2 to values  normalized 
        % by the original axis range.  Example: for an axis range of [10,20] 
        % that was changed to [12,20], the lower limit of 12 is normalized to 0.2;
        normLowerLimit = ([ax(2).XLim(1);ax(2).YLim(1)] - axBaseLim(:,1,2))./range(axBaseLim(:,:,2),2); 
        % Compute the new lower limits to ax1.
        newLimits = normLowerLimit.*range(axBaseLim(:,:,1),2) + axBaseLim(:,1,1);
        % Compute the new upper limits ax1.
        newLimits(:,2) = newLimits(:,1) + [range(ax(2).XLim);range(ax(2).YLim)].*scalingFactor;
        % Update ax1 limits
        set(ax(1), 'XLim', newLimits(1,:), 'YLim', newLimits(2,:))
    end
    function myRestoreButtonCallbackFcn(hobj, event, ax1, originalCallback, xyscale, axBaseLim)
        % Responds to pressing the restore button in the ax2 toolbar. 
        % originalCallback is a function handle to the original callback 
        % function for this button. 
        % xyscale and axBaseLim are defined elsewhere.
        originalCallback(hobj,event) % reset ax2
        axisLimitListener([],[],[ax1,event.Axes],xyscale,axBaseLim) % update ax1
    end
end
0 Commenti
Risposta accettata
  Voss
      
      
 il 3 Nov 2022
        clear all
clc
 a=randi(10,10,20);
 x=linspace(-1,1,20);
 y=linspace(-1,1,10);
 imagesc(x,y,a)
 ax1=gca;
ax2=axes('Position',ax1.Position,'XAxisLocation','top','xlim',[1 size(a,2)],'fontweight','bold','fontsize',10,...
        'YAxisLocation','right','ylim',[1 size(a,1)],'color','none');
link_axis(ax1,ax2);
colorbar
drawnow()
set(ax1,'Position',get(ax2,'Position'))
%% 
function link_axis(ax1,ax2)
ax1.Box = 'off';
% if you uncomment " ax1.Interactions = []; " then you can play only with
% the second axis
% ax1.Interactions = []; 
ax1.Toolbar.Visible = 'off'; 
% Compute scaling factor to convert ax1 scale from ax2 scale
% xyscale is 2x1 factors of [x;y] axes
xyscale = [range(ax1.XLim) / range(ax2.XLim); range(ax1.YLim) / range(ax2.YLim)];
% Store original axis limits for both axes
% axBaseLim is 2x2x2 axis limits of [x;y] axes, [min,max] limits; and
% [ax1,ax2] along the 3rd dimension
axBaseLim = [ax1.XLim; ax1.YLim];           % ax1
axBaseLim(:,:,2) = [ax2.XLim; ax2.YLim];    % ax2
% Assign listener
ax2.UserData.Listener = addlistener(ax2,{'XLim','YLim'}, 'PostSet', ...
    @(~,~)axisLimitListener([], [], [ax1,ax2], xyscale, axBaseLim)); 
% Fix restoreview button
axTB = axtoolbar(ax2,'default'); 
isRestoreButton = strcmpi({axTB.Children.Icon},'restoreview');
if any(isRestoreButton)
    restoreButtonHandle = axTB.Children(isRestoreButton);
    originalRestoreFcn = restoreButtonHandle.ButtonPushedFcn;
    restoreButtonHandle.ButtonPushedFcn = ...
        {@myRestoreButtonCallbackFcn, ax1, originalRestoreFcn, xyscale, axBaseLim};
end
function axisLimitListener(~,~,ax,scalingFactor,axBaseLim)
% Listener callback that responds to x/y axis limit changes to ax2 and 
% updates the axis limits to ax1. 
% INPUTS
%   ax: 1x2 array of axis handles to [ax1,ax2]
%   scalingFactor: (see description of xyscale above)
%   axBaseLim: (see description of axBaseLim above)
% Convert the lower axis limits from ax2 to values  normalized 
% by the original axis range.  Example: for an axis range of [10,20] 
% that was changed to [12,20], the lower limit of 12 is normalized to 0.2;
normLowerLimit = ([ax(2).XLim(1);ax(2).YLim(1)] - axBaseLim(:,1,2))./range(axBaseLim(:,:,2),2); 
% Compute the new lower limits to ax1.
newLimits = normLowerLimit.*range(axBaseLim(:,:,1),2) + axBaseLim(:,1,1);
% Compute the new upper limits ax1.
newLimits(:,2) = newLimits(:,1) + [range(ax(2).XLim);range(ax(2).YLim)].*scalingFactor;
% Update ax1 limits
set(ax(1), 'XLim', newLimits(1,:), 'YLim', newLimits(2,:))
end
function myRestoreButtonCallbackFcn(hobj, event, ax1, originalCallback, xyscale, axBaseLim)
% Responds to pressing the restore button in the ax2 toolbar. 
% originalCallback is a function handle to the original callback 
% function for this button. 
% xyscale and axBaseLim are defined elsewhere.
originalCallback(hobj,event) % reset ax2
axisLimitListener([],[],[ax1,event.Axes],xyscale,axBaseLim) % update ax1
end
end
Più risposte (0)
Vedere anche
Categorie
				Scopri di più su Data Distribution Plots in Help Center e File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!


