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 Center および File ExchangeSubplots についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by