Setting labels and titles of axes extremely slow

16 views (last 30 days)
JM on 14 Dec 2017
Commented: Justin on 13 Feb 2021
In my use case the setting of labels and the title of axes takes up to a second or more each and I would like to know why and how I can avoid this.
My use case is as follows: I have programmatically created a 1300x800 figure using:
mainfig = figure(...
'Position',[50 70 1300 80],...
and added about 6~8 custom sized axes to it using:
'Position',ex [1 2 3 4]);
Creating this window, these axes and the plots that use these axes using:
is fine and takes up next to no time in the profiler.
However when I add labels and a title to the axes I am seeing up to 1.5 seconds of runtime for each item set. Here is an example from the MATLAB R2015a profiler (problem occurs in R2015a, R2016b, R2017b):
Line Code Calls Total Time %Time
48 ax.XLabel.String = 'blablabla... 1 1.412 s 33.3%
50 ax.Title.String = 'blablabla... 1 1.379 s 32.5%
49 ax.YLabel.String = 'blablabla... 1 1.375 s 32.4%
The profiler shows these as 100% self time and does not show the innards of these lines.
Also, in this case using xlabel() or ax.XLabel.String makes no difference in time cost.
So, to repeat, I would like to know why and how I can avoid this. It should not be normal that putting a single piece of text somewhere in a GUI costs an eternity of CPU time.
A test case is provided here:
Massimo Ciacci
Massimo Ciacci on 22 Jul 2018
Edited: Stephen23 on 22 Jul 2018
For me the delay depends on the data size. I guess profiler assigns the time to the lines following plot instead on to the plot itself...
Version: '4.6.0 NVIDIA 391.35'
Vendor: 'NVIDIA Corporation'
Renderer: 'GeForce GTX 1070/PCIe/SSE2'
MaxTextureSize: 32768
Visual: 'Visual 0x09, (RGBA 32 bits (8 8 8 8), Z depth 24 bits, Hardware acceleration, Doub...'
Software: 'false'
SupportsGraphicsSmoothing: 1
SupportsDepthPeelTransparency: 1
SupportsAlignVertexCenters: 1
Extensions: {375x1 cell}
MaxFrameBufferSize: 32768
Massimo Ciacci
Massimo Ciacci on 24 Sep 2019
I confirm that quering properties of axis is slower when there is a lot of stuff plotted on them. A good workaround which I found myself again to use today is to fix all axis properties BEFORE plotting anything on them.

Sign in to comment.

Answers (1)

Massimo Ciacci
Massimo Ciacci on 8 Oct 2019
I think I found a nice workaround
% (1) set all curves invsibile, for a good speed up for label handles retrieval
axChild = get(gca,'Children');
allCurves = findobj(axChild,'type','line');
% (2) fix/change labels
% (3) restore visible
as an example you can try the following plot code which plots 20M points on two curves
close all
profile off
profile on
NN = 20e6;
figure(100); grid on; hold on; axh=gca;
axChild = get(gca,'Children');
allCurves = findobj(axChild,'type','line');
xlabel('x'); ylabel('y');
xlh = get(axh,'xlabel');
%Restore visibility
profile viewer
Elapsed time is 2.147400 seconds. SPEED_UP = 0
Elapsed time is 0.690461 seconds. SPEED_UP = 1
Massimo Ciacci
Massimo Ciacci on 8 Oct 2019
I also noticed that this trick can help speed up linkaxes in the same way, by setting allCurves to invisible in both axes before linking ...
Justin on 13 Feb 2021
Strangely neither workaround (setting labels before plotting nor hiding graph objects when setting labels) seems to work when you are plotting histograms rather than lines. At least in 2020b anyway.

Sign in to comment.


Find more on Graphics Performance in Help Center and File Exchange


Community Treasure Hunt

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

Start Hunting!