Colorbar's colormap not updating when also adding a label to the bar

382 views Asked by At

Below's some code that generates a figure that has a colorbar with a custom label:

function q41269479
% Create an axes:
figure();
% Display an image:
hIm = imagesc(peaks);
% Adjust colormap and colorbar:
h = colorbar; colormap(gray);
ylabel(h, '\Delta', 'Rotation', 0, 'Units', 'Normalized', 'Position', [0.48 1.05]);

What I expect to get: Expected result

What I actually get: enter image description here

Notice that the colorbar is still parula (the default) and not gray as I asked it politely to be.

The natural way to solve some graphical glitch like this is by "invalidating" the figure... Unfortunately, the popular commands drawnow and refresh, executed after the figure is at the shown state, do not remedy this situation.

It should be noted that exporting the figure results in the correct colors.

Question: Does anybody have an idea why this happens and how to fix it (preferably without workarounds / hacks)?

I'm running R2016b, on Win 7. As far as I know, this does not happen on Octave 4.0.3.

1

There are 1 answers

0
Dev-iL On BEST ANSWER

Several workarounds seem to solve this issue:

% Solution 1: adding a pause/drawnow AFTER setting the colormap.
h = colorbar; colormap(gray); pause(eps);
% OR:
h = colorbar; colormap(gray); drawnow;

% Solution 2: change the figure size (e.g. maximize then minimize)
drawnow % Required to avoid Java errors
jFig = get(handle(gcf), 'JavaFrame'); 
jFig.setMaximized(true); pause(0.0001); jFig.setMaximized(false);

% Solution 3: add/remove datatip
hDataCursorMgr = datacursormode(gcf);
hDatatip = createDatatip(hDataCursorMgr,hIm); delete(hDatatip);

I have no idea why this happens though. My best guess is that JIT tries to execute these commands on multiple threads. Then, when colormap tries to change the colors of the colorbar, it encounters an object "locked" by ylabel, and as a result cannot perform its function. However the update "directive" does make its way into some event queue that execute when certain things happen to the figure.