Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Colorbar slows down plot

I am plotting many images in a loop on the same axes to make a video. Unfortunately the presence of a colorbar makes the loop a whole lot more slow. This happens even though I've "frozen" the colorbar using caxis('manual').

Why? I suppose there still may be listeners slowing the whole thing down, but it's really bad. Having frozen the colorbar there are no should be no calculations involved with it.

Here's a demo that illustrates some of the workings of the colorbar, mainly with the aim of freezing it. There's no loop in the following.

close all
figure(1);
C = gallery('randcorr',10);
ih = imagesc(1*C);
ch = colorbar;
% The colorbar disappears...
ih = imagesc(2*C);
% Must hold plot in order for it not to disappear
hold on
ch = colorbar;
% Now, even though it doesn't disappear, it still changes!
ih = imagesc(3*C);
% Even if we use a lower lever function
set(ih,'CData',4*C);
% We must do this to freeze the colorbar
caxis('manual')
set(ih,'CData',5*C);
ih = imagesc(6*C);
% That worked!
like image 797
Patrick Avatar asked Sep 15 '26 15:09

Patrick


2 Answers

This is more interesting than I expected. I have no answer, but a series of examples demonstrating some non-answers. Maybe someone smarter than me will have better luck.

Example, with a colorbar:

figure;
C = gallery('randcorr',10);
ih = imagesc(1*C);
ch = colorbar;
caxis([-2 2])
tic; 
for ix = 1:100
    set(ih,'CData',gallery('randcorr',10));
    drawnow
end
toc;  %2.7 seconds

And without a colorbar

figure;
C = gallery('randcorr',10);
ih = imagesc(1*C);

caxis([-2 2])    
tic; 
for ix = 1:100
    set(ih,'CData',gallery('randcorr',10));
    drawnow
end
toc;  %0.67 seconds

What caused the change from 2.7 to 0.67 seconds?

A colorbar is really just a special sort of axis, so maybe the problem is having more than 1 interesting axis in the figure

figure;
subplot(2,1,1)
C = gallery('randcorr',10);
ih = imagesc(1*C);
subplot(2,1,2)
C = gallery('randcorr',10);
ih2 = imagesc(1*C);

caxis([-2 2])    
tic; 
for ix = 1:100
    set(ih,'CData',gallery('randcorr',10));
    drawnow
end
toc;  %0.87 seconds (consistently slower,  but not enough)

Maybe the property linking is causing the slow down

figure;
subplot(2,1,1)
C = gallery('randcorr',10);
ih = imagesc(1*C);
subplot(2,1,2)
C = gallery('randcorr',10);
ih2 = imagesc(1*C);
link = linkprop(get(gcf,'children'), 'CLim');

caxis([-2 2])    
tic; 
for ix = 1:100
    set(ih,'CData',gallery('randcorr',10));
    drawnow
end
toc;  %0.88 seconds (pretty much the same as above)

Looking at the default colorbar, it has a lot of color detail, maybe the problem is simply the number of colors which need to be rendered.

figure;
subplot(2,1,1)
C = gallery('randcorr',10);
ih = imagesc(1*C);
subplot(2,1,2)
ih2 = imagesc(repmat(linspace(-2,2,200), 10,1));

caxis([-2 2])    
tic; 
for ix = 1:100
    set(ih,'CData',gallery('randcorr',10));
    drawnow
end
toc;  %0.96 seconds (slower, but still not the 2.7 second colorbar case)
like image 72
Pursuit Avatar answered Sep 17 '26 19:09

Pursuit


Thanks to Pursuit for investigation the issue further, which seems too deep to resolve properly.

Having not found a good standard solution, I resorted to making my own colorbars in separate subplots. These are then left untouched during the loop. To make your own colorbars I recommend using subplot(1,5,1:4) for the main plot, and subplot(1,5,5) for the colorbar. Then you just plot a linspace of the caxis using imagesc again to make the colorbar. Supply y ticks, remove x ticks.

like image 35
Patrick Avatar answered Sep 17 '26 19:09

Patrick



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!