Setting labels and titles of axes extremely slow
16 visualizaciones (últimos 30 días)
Mostrar comentarios más antiguos
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(...
'Units','Pixels',...
'Position',[50 70 1300 80],...
'Name','abc',...
'DockControls','off',...
'Resize','off',...
'NumberTitle','off');
and added about 6~8 custom sized axes to it using:
axes(...
'Parent',mainfig,
'Units','pixels',
'Position',ex [1 2 3 4]);
Creating this window, these axes and the plots that use these axes using:
plot(ax,...)
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: https://pastebin.com/wBjxydju
5 comentarios
Massimo Ciacci
el 22 de Jul. de 2018
Editada: Stephen23
el 22 de Jul. de 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...
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192539/image.png)
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/192540/image.png)
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
el 24 de Sept. de 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.
Respuestas (1)
Massimo Ciacci
el 8 de Oct. de 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');
set(allCurves,'visible','off');
% (2) fix/change labels
% (3) restore visible
set(allCurves,'visible','on');
as an example you can try the following plot code which plots 20M points on two curves
close all
clearvars
tic
profile off
profile on
SPEED_UP = 1;
NN = 20e6;
figure(100); grid on; hold on; axh=gca;
plot(1:NN,1:NN);
plot(1:NN,-(1:NN));
if SPEED_UP
axChild = get(gca,'Children');
allCurves = findobj(axChild,'type','line');
set(allCurves,'visible','off');
end
xlabel('x'); ylabel('y');
set(axh,'xscale','log');
xlh = get(axh,'xlabel');
if SPEED_UP
%Restore visibility
set(allCurves,'visible','on');
end
profile viewer
toc
Elapsed time is 2.147400 seconds. SPEED_UP = 0
Elapsed time is 0.690461 seconds. SPEED_UP = 1
![profiler_before_after_NEW.png](https://www.mathworks.com/matlabcentral/answers/uploaded_files/241723/profiler_before_after_NEW.png)
2 comentarios
Massimo Ciacci
el 8 de Oct. de 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
el 13 de Feb. de 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.
Ver también
Categorías
Más información sobre Graphics Performance en Help Center y File Exchange.
Productos
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!