2

In matlab versions prior to 2014 I could alter the underlying image in the colorbar by doing the following:

cmap = ... % something which is MxNx3
colormap(reshape(cmap, [N*M,3]))
cmapIxs2D = reshape((1:(N*M))', [N, M]);
ax = colorbar('peer', gca);
set(get(ax, 'Children'), 'CData', cmapIxs2D);
ylim(ch, [0 255]), xlim(ch, [0 1])

This was useful if you wanted to display a custom colormap which is e.g. 2D (NxMx3) instead of the normal 1D (Nx3). How could this be done in versions after 2014 where the underlying image of the colorbar is no longer accesible, it has no Children according to the documentation.

Example (Color value is interpreted as having a e.g. velocity(y-axis-color) and acceleration(x-axis-color)) :

enter image description here

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
thorsan
  • 1,034
  • 8
  • 19
  • What, how. what? Can you show an image of what it would look like a MxNx2 colobar? My brain fails to see how this could work – Ander Biguri Aug 10 '16 at 11:05
  • Can you post an image of what you mean? – Dan Aug 10 '16 at 11:09
  • Added example @Dan – thorsan Aug 10 '16 at 11:18
  • @thorsan but then you're not applying a regular colormap to an image are you? – Dan Aug 10 '16 at 11:35
  • @Dan I am using indexed images and my colormap is MxN=2^16 long, i.e this only works with opengl. So plotting and colormap setting is standard, its just getting the colorbar to show the colormap as a 2D plot and not 1D. Offcourse, caxis functionality is broken when I do it this way. – thorsan Aug 10 '16 at 11:39
  • So if you're not actually using the caxis, maybe you should rather use a subplot and just draw your own colourbar as a separate (RGB) image? – Dan Aug 10 '16 at 12:02
  • @Dan I could do that, but in order to maintain the relation between the plot and the colorbar after resizing, it is preferable to use the colorbar. And this works in versions, as described above, prior to 2014, but after 2014, the colorbar is a new object with other properties. – thorsan Aug 10 '16 at 12:40
  • I join Dan's question - what kind of relation between the colorbar and the image are you interested in specifically? `CLim`? Did you try [`linkprop`](http://www.mathworks.com/help/matlab/ref/linkprop.html)? – Dev-iL Aug 10 '16 at 12:56
  • @Dev-iL I am not interested in any special relationship between the colorbar and the image other than what is there today, except that I dont need caxis to work. I have an indexed image, were the interpretation of the colors are 2D and not 1D as normal colorbar, thus I want to replace the colors the colorbar is displaying with the 2D colormap. Displaying the image using my colormap works fine, it is just to get the colorbar to show this 1D colormap as a 2D image. – thorsan Aug 10 '16 at 13:00
  • @thorsan the colorbar is effectively an rgb image with limits. it can be simulated with a simple `imagesc` command a lot more easily than even what you were doing previously. – Tasos Papastylianou Aug 10 '16 at 23:25
  • @TasosPapastylianou It could be done with an image, (I have code implemented code for this), but placement and resizeing of the figure is very well handled in the colorbar code, and I would like to avoid having to implement this functionality – thorsan Aug 11 '16 at 06:04
  • fair enough. it just sounds to me you're exchanging an easier task for a harder task, which also happens to rely on undocumented features that could break at any moment (like they have). – Tasos Papastylianou Aug 11 '16 at 07:56

1 Answers1

1

Based on the ideas proposed in the OP comments, I've come up with something:

function q38871518
%% Plot something random:
hF = figure('Color',0.4*[1 1 1],'SizeChangedFcn',@recolorCB); membrane;
hTmp = gca;
% Compute the fake colorbar contents:
cm = bsxfun(@times,permute(colormap,[1,3,2]),0:0.01:1); % figure(); imagesc(cm);
% Create an axes to hold the fake colorbar
hAx = axes(hF); imagesc(hAx,cm); axis(hAx,'off'); 
function recolorCB(varargin)
  drawnow;
  if exist('cb','var')
    cb.Face.Texture.CData(:) = 0;
    % "Link" the 'Position' prop between the colorbar and the fake colorbar:
    hAx.Position = cb.Position;
  end
end
% Create the real colorbar 
cb = colorbar(hTmp,'Color',[1 1 1]);
% Synchronize positions:
hAx.Position = cb.Position;
% Make sure the fake colorbar is at the bottom, so we can see the values clearly
uistack(hAx,'bottom');
% Final touch-ups:
drawnow; cb.Face.Texture.CData(:) = 0; cb.Face.Texture.ColorType = 'truecoloralpha';
end

The result is:

enter image description here

The "fake" colorbar moves to the correct location as the figure size changes. When saving the figure, the old colorbar appears, this also happens after zooming out (and probably following some other actions). Getting rid of this would require some extra hacking...

Tested on MATLAB R2016a.

Dev-iL
  • 23,742
  • 7
  • 57
  • 99
  • Thanks, this is a possible solution for making a custom colorbar, I have a solution like this for Matlab < 2014. You could also set the colorbar to invisible, cb.Visible = 'off' – thorsan Aug 11 '16 at 11:46
  • @thorsan `cb.Visible = 'off';` removes the numbers, no? – Dev-iL Aug 11 '16 at 11:50
  • Yes, but I would set custom numbers on the image axis, but this could off course also be set on the colorbar axis – thorsan Aug 11 '16 at 12:25