Unable to link x-axis in subplot

조회 수: 12 (최근 30일)
newbie9
newbie9 2019년 8월 4일
답변: darova 2019년 8월 17일
I can't seem to get linkaxis to work (code is below, with small datasets attached). I am trying to get the subplots to line up, such that visually the x-axis is has the same range and width for both subplots.
Here is my code:
ax1 = subplot(2,1,1);
scatter(x1, y1)
box on
grid on
axis equal
xlims = get(gca, 'XLim')
ax = gca;
ax.XRuler.Exponent = 0;
ax.YRuler.Exponent = 0;
xlims = get(gca, 'XLim')
ax2 = subplot(2,1,2);
scatter(x1, y1)
xlim(xlims)
box on
grid on
ax = gca;
ax.XRuler.Exponent = 0;
linkaxes([ax1,ax2],'x')
And here is what the figure looks like:
I also tried:
% adding this to the first subplot:
xlims = get(gca, 'XLim')
positioning = get(gca,'position');
% adding this to the second subplot:
xlim(xlims)
set(gca, 'position', [positioning(1) positioning(2)/5 positioning(3) positioning(4)]) %x y width height
  댓글 수: 3
newbie9
newbie9 2019년 8월 4일
편집: newbie9 2019년 8월 4일
Thanks Star Strider. I'm in R2018a. I just restarted my computer and rebooted Matlab, but I am still getting the same plotting issue. Issue is in both the pop-up figure window and the saved jpg.
newbie9
newbie9 2019년 8월 16일
As an update, the axes link when I remove "axis equal" from the first subplot

댓글을 달려면 로그인하십시오.

채택된 답변

darova
darova 2019년 8월 17일
Example
clc,clear
x = linspace(0,10);
y1 = sin(x)./x;
y2 = sin(x)/2;
ax(1) = subplot(2,1,1);
plot(x,y1)
ax(2) = subplot(2,1,2);
plot(x,y2);
set(ax,'Xlim',[2 5],'Box','on')
set(ax,'xgrid','on','ygrid','on')
axis(ax,'equal')

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Subplots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by