Linkprop not working as I'd expect
7 views (last 30 days)
Show older comments
I have an image with two axes (because I want one scale on the left and one scale on the right).
I read on this page that you can link two axes so when you resize the figure, both axes go along for the ride. But when I try it, it does nothing.
To see what I mean, try to run the code below. Then resize the figure window manually and you'll see that the axes do not track each other. By "track each other" I mean that the left and right axes are 'stuck' to the side of the image always, and if you resize the figure window then the image resizes, but both y axes resize appropriately also.
How can I have two axes that exactly track each other even when the figure window is resized? In other words, the black axis and the red axis in the code below should behave identically when the window is resized.
clear;
close all;
figure;
imshow('coins.png');
ax1 = gca;
set(ax1,...
'XColor','r',...
'YColor','r');
axis on;
ax2 = axes(...
'Position',ax1.Position,...
'YAxisLocation','Right',...
'Color','none',...
'XTick',[],...
'YLim',[0,10]);
all_axes = findall(gcf,'type','axes');
linkprop(all_axes,'position');
Thanks!
0 Comments
Answers (1)
Arnab Sen
on 28 Dec 2015
From the description I understand that when you resize the figure the right Y axes is getting resized while the left one is not and you would like to have both of them behave identically. The reason the issue you are facing is you are showing the image first then set the 'gca' to the variable 'ax1'. At that point the 'gca' is the axes of image itself. On the other hand the axes 'ax2' is independent from the image. Tht is why one is resized while other is not. As a workaround you can make both the axes independent from the image in which case both of them behave identically when the figure gets resized. The following code snippet illustrates the above:
>> clear;
>> close all;
>> figure;
>> imshow('coins.png');
>> ax1 = axes(...
>> 'YAxisLocation','left',...
>> 'Color','none',...
>> 'XTick',[],...
>> 'YLim',[0,20]);
>> ax2 = axes(...
'Position',ax1.Position,...
'YAxisLocation','Right',...
'Color','none',...
'XTick',[],...
'YLim',[0,10]);
See Also
Categories
Find more on Interactive Control and Callbacks 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!