Preserve plot box size while updating data aspect ratio

8 views (last 30 days)
Matlab documentation states that disabling either of the data or plot box aspect ratios turns off stretch-to-fill and causes the axes to immediately resize. However, when changing the data aspect ratio of an axes, I would like the plot box to stay as it is, and instead the XLim and YLim properties to be automatically updated. I have tried several different iterations of the code below, but none have succeeded.
Notably, if the figure window is not docked, then the desired behavior is achieved. And if run line-by-line, then the desired behavior is achieved. But, I'm running some costly figure generation code, so I would rather avoid these less efficient "hacks". I'm using version 2020a, in case it matters.
figure
set(gcf,'windowstyle','docked')
cax=axes;
hold(cax,'on')
cax.YLim=[0,cax.PlotBoxAspectRatio(2)];
cax.PlotBoxAspectRatioMode='manual';
cax.DataAspectRatioMode='manual';
cax.XLimMode='auto';
cax.YLimMode='auto';
cax.DataAspectRatio=[1, 0.5, 1];
  2 Comments
J. Alex Lee
J. Alex Lee on 10 Dec 2022
I think you will need to calculate the axes dimensions and manually set them.
Varun
Varun on 20 Mar 2023
Edited: Varun on 20 Mar 2023
Hello!
Can you please tell me exactly what's going wrong? I ran the code snippet you posted and I got a docked plot of the axes. When I zoomed in on the plot, the plot box does not get resized, and instead the values on the axes changed i.e., the xlim and ylim values got changed. I tried this on both R2022b and R2020a and got the same results. I even tried plotting multiple arrays on the same axes and still, the size does not get affected. So, please let me know exactly how to help!

Sign in to comment.

Answers (0)

Categories

Find more on 2-D and 3-D Plots in Help Center and File Exchange

Tags

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!