Current axis tick vector differ between having figure visible and invisible

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

You forgot to attach the data and enough code for somebody to actually try to reproduce...
Why
colororder({'k'});
instead of
fig = figure('visible', 'off');hold on;
plot(y_data);
yyaxis('right'); hold on;
plot(yy_data,'k');
?
Also, why hold on here? It messes with the auto-scaling.
ADDENDUM
Got to a breakpoint and since happen to be using R2024b at the moment, I tried to duplicate your description but could not reproduce the symptoms described.
Can you reproduce this progrommatically that can illustrate with MATLAB returned data rather than just verbal description? Having something that can be reproduced is about the only way such could be diagnosed if there is an operation causing it.
Either a script that generates the observed result or an explicit set of steps that reliably will reproduce the symptoms.
@dpb colororder({'k'}) because I do not want my axis to have the same color as the first line I draw in this axis, and this is the workaround I have found so far. I am happy to be directed to a better solution.
I left in the hold on by accident, in my code I plot several things before shifting axes to the right one. I am not sure I have observed any problems with autoscaling from using hold on, what exactly do you mean?
Here is a minimal code for reproducing:
fig=figure('visible','off');
vec=[0 0.04 0.04 0 0 1.09 1.09 0 0 2.18 2.18 0 0 3.21 3.31 0 0 4.35 4.35];
plot([vec -vec]);
ax = gca;
Now when I query YTick I get
ax.YTick
>> -5 0 5
If I then make the figure visible and query YTick again I get
figure(fig)
ax = gca;
ax.YTick
>> -5 -4 -3 -2 -1 0 1 2 3 4 5
Running the above locally I observe
K>> fig=figure('visible','off');
vec=[0 0.04 0.04 0 0 1.09 1.09 0 0 2.18 2.18 0 0 3.21 3.31 0 0 4.35 4.35];
plot([vec -vec]);
ax = gca;
ax.YTick
ans =
-5.00 -4.00 -3.00 -2.00 -1.00 0 1.00 2.00 3.00 4.00 5.00
K>>
K>> ver
-----------------------------------------------------------------------------------------------------
MATLAB Version: 24.2.0.2923080 (R2024b) Update 6
MATLAB License Number: 871405
Operating System: Microsoft Windows 10 Pro Version 10.0 (Build 19045)
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)
MATLAB Compiler Version 24.2 (R2024b)
MATLAB Report Generator Version 24.2 (R2024b)
Optimization Toolbox Version 24.2 (R2024b)
Signal Processing Toolbox Version 24.2 (R2024b)
Statistics and Machine Learning Toolbox Version 24.2 (R2024b)
K>>
I can't reproduce it here...I fiddled around quite a lot and never saw it do something different unless I explicitly changed the figure size so it didn't have room for that many or somesuch..
I can't explain your sysmptoms; would a drawnow while the figure is still not visible change your symtpoms?
@Walter Roberson, can you reproduce it on your Mac?
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)
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.

 Accepted Answer

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.

2 Comments

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.

More Answers (0)

Categories

Find more on Graphics Object Programming in Help Center and File Exchange

Products

Release

R2024b

Community Treasure Hunt

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

Start Hunting!