Current axis tick vector differ between having figure visible and invisible

32 views (last 30 days)
I am creating a plot that can be summarized as
fig = figure('visible', 'off');hold on;
plot(y_data);
colororder({'k'}); % Force right y-axis to be black
yyaxis('right'); hold on;
plot(yy_data);
When I get current axes and check the YTicks after plotting in the right axis, the ax.YTick vector matches what is on the right axis. However, if I switch back to the left axis, I get a shorter vector of YTicks than is on the left axis (only first, middle and last). This is also the case when I query the current axes right after plotting on the left axis. But if I make the figure visible and then query the YTicks on the left axis, the values in the array matches exactly what is in the left axis of the plot.
What am I missing? There must be another explanation than whether the figure is visible, I'm sure.
  5 Comments
Britt
Britt 36 minutes ago
Drawnow inserted before calling gca does not seem to make any difference.
My version is the same as yours, the only difference is that I am running it in Linux
>> ver
----------------------------------------------------------------------------------------------------------
MATLAB Version: 24.2.0.2923080 (R2024b) Update 6
MATLAB License Number:
Operating System: Linux 6.14.0-1017-oem #17-Ubuntu SMP PREEMPT_DYNAMIC Mon Nov 24 08:52:02 UTC 2025 x86_64
Java Version: Java 1.8.0_202-b08 with Oracle Corporation Java HotSpot(TM) 64-Bit Server VM mixed mode
----------------------------------------------------------------------------------------------------------
MATLAB Version 24.2 (R2024b)
Curve Fitting Toolbox Version 24.2 (R2024b)
dpb
dpb about 1 hour ago
Edited: dpb about 1 hour ago
As noted, seems peculiar, but at least so far I've not been able to reproduce it. Mayhaps is buried in the bowels of what is different in the systems rendering code and/or firmware/hardware.
On your comment "colororder({'k'}) because I do not want my axis to have the same color as the first line I draw in this axis", you can set the YAxis.Color property to whatever you wish it to be. It's OK to reset the color order if you want; I was just curious as to why it was in the code in the particular instance.
%hAx=axes;
yyaxis('left')
yyaxis('right')
figure
%axes
yyaxis('left')
yyaxis('right')
hAx=gca;
hAx.YAxis(2).Color='k';
The second has the RH y axes color as black without changing the default color order; you can, then plot into either left/right with whatever color assignments you want for each line or continue on with the default color order. You'll note the above two figures both have the default blue LH color.
If you set 'box','on' it will be whichever is the axes in focus.
Regarding hold on it resets the auto-ranging to 'on'; wasn't sure but what your app might have formerly reset autoscaling off and that was what was turning it back on and thus changing ticks/marks, not having the entire session.

Sign in to comment.

Answers (1)

Walter Roberson
Walter Roberson about 14 hours ago
Automatic ticks are not reliably computed until you make the figure visible again.
As long as the figure is invisible, the plot is treated as-if the size of the plotting area is unknown -- and the size of the plotting area is one factor in determining the automatic tick marks.
  1 Comment
Britt
Britt about 2 hours ago
Edited: Britt 29 minutes ago
Ok this confirms what I see. I am puzzled because I have not had this problem before (I recently updated from 2023b to 2024b). At one point I had to introduce a pause(0.5) to make sure there was time for the axes to update, which previously resolved my axes issues.
Can I do anything to get "actual" ticks (which will print alright when the figure is printed to file without ever being visible as a matlab figure) without opening the figure?

Sign in to comment.

Products


Release

R2024b

Community Treasure Hunt

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

Start Hunting!