Scaling issues when using two x axes for a single subplot

2 views (last 30 days)
I have a subplot in which I want to use two x axes one above the another. The second x axis(ax2) has values that are inverse of the first x axis(ax1) and hence there is a one to one correspondence between the two axes. Also, ax2 is just for reference and does not have any curve associated with it inside the plot area of the subplot. Now, I am using axis square before adding ax2 due to which ax2 is not exactly vertical below the first axis, in fact ax2 is longer than ax1 and it extends both sides of ax1. I even tried assigning the same 'left' and 'width' in the 'position' vector for ax2 using ax1 properties. I also checked the 'position' vectors of the two axes and their 'left' and 'bottom' properties were the same. Yet, the two axes are not of the same width and are not vertically aligned. My code is as below.
plot(var1,var2,'Color',col(kk,:));
set(gca, 'XTick', xax, 'XTickLabel', xax)
xlabel('var1')
ylabel('var2')
axis square
ax1 = gca;
shr = 0.1;
set(ax2,'Position', get(ax1, 'Position') .* [1 1 1 0.001] - [0 shr 0 0],'Color','none')
xlim([st_xax,end_xax]);
set(ax2, 'XTick', var3, 'XTickLabel', var3)
xlabel('var3')
Basically, I want to know if I am doing it right above or if there is any other property that should be tweaked to make the second axis to be of the same width with the first axis vertically above the second.
I attribute the issue to axis square as if I do not use axis square then the two axes have the same width and ax2 is vertically above ax1.
Any help would be appreciated.

Answers (1)

Star Strider
Star Strider on 26 Sep 2015

Community Treasure Hunt

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

Start Hunting!