MATLAB Answers

0

Colormap utility - two axes in colorbar

Asked by Jocelino Rodrigues on 11 Aug 2019
Latest activity Edited by dpb
on 14 Aug 2019
Accepted Answer by dpb
Hi everyone,
I have a query regarding the colormap utility and plotting two variables using one colormap and one colorbar, the latter of which would have two axes for each variable. To accompany the more detailed query description below, please see the image attached - it was edited using paint to demonstrate what I would like to plot using MATLAB.
I have two variables that are related to eachother (let's say A and B) where A = xB where 'x' is a unitless constant value (let's say it's a 'difficult' number like 2.98 for every case). Therefore, A and B have the same units.
I would like to use a single colormap for both variables since 'x' is the variable that couples between one and the other. Now, where the right tick axis says B = 6, the left tick axis would have A equal to 17.88 which doesn't look good as a tick value. Instead I would like it to have ticks at location 18, which would have to be slightly higher up in the colorbar than 6 (see image attached). Same for the other values.
The image attached shows what I would like to code. Is this possible? Please let me know as it's something that sounds feasible, but I haven't figured out how to do it.
Thank-you.

  0 Comments

Sign in to comment.

1 Answer

Answer by dpb
on 12 Aug 2019
Edited by dpb
on 12 Aug 2019
 Accepted Answer

Yeah, it's doable...refinements needed, but general idea works.
Z = peaks(20);
[~,hCF]=contourf(Z,10);
hAx=gca; % save axes handle main axes
hCB=colorbar; % add colorbar, save its handle
hNuCBAx=axes('Position',hCB.Position,'color','none'); % add mew axes at same posn
hNuCBAx.XAxis.Visible='off'; % hide the x axis of new
posn=hAx.Position; % get main axes location
posn(3)=0.8*posn(3); % cut down width
hAx.Position=posn; % resize mmain axes to make room for labels colorbar
hCB.Position=hNuCBAx.Position; % put the colorbar back to overlay second axeix
hNuCBAx.YLim=[-22 22]; % alternate scale limits new axis
ylabel(hCB, 'A','Rotation',0,'FontWeight','bold','VerticalAlignment','middle')
ylabel(hNuCBAx,'B','Rotation',0,'FontWeight','bold','VerticalAlignment','middle')
hCB.Position=hNuCBAx.Position; % put the colorbar back to overlay second axeix
results in
I overshot the width reduction a little and it's kinda' kludgy to have to move the original colorbar back to its original position when change the other axis size but there's an internal callback function that ties them together...

  9 Comments

dpb
on 12 Aug 2019
Well, rereading the documentation enough times I realize that I've had a misconception--an axes CAN have more than one colorbar object...just that if the second is tried to be created on top of the first the first is silently deleted. Testing shows this is so even if try to trick HG2 by using the 'Position' property instead of one of the named locations--it checks internally and still wipes out the first.
Thus, you can create one on the east and another on the west and then move the west one to the position read from the east but the axes size callbacks and internal logic moves the main axes width and the two don't end up overlaying each other exactly...so, while it's easy way to create two objects of the same kind on the figure, it appears to be at least as much if not more trouble to actually create the desired effect as compared to the first try above...as with yyaxis there's just too much going on behind the scenes don't have control over.
Thanks for this - I really appreciate your help and in depth description. I will give these a go and get back to you in a few days to confirm the answer works!
dpb
on 14 Aug 2019
Good luck!
Just thought of another potential "gotcha'!" The new axis does not have any link to the colorbar so if you make modifications to the plot that trigger the colorbar callbacks on position, the overlap is probably going to break and the position would need to be reset for hNuCBAx. I've not tried to think through what it would take to write the suitable callback for it...unfortunately, since one can't find the handle to an axis for the colorbar even with Yair's undocumented function to find hidden properties, there's no way to use linkaxes.
The ability to have the colorbar property .AxisLocation be 'both' as well as either 'in' or 'out' could be a worthwhile enhancement request. With that (and the subsequent changing of the properties to be able to address both instead of just one), this would be a trivial exercise. And, if that were done when the object was created via something like yyaxis() on the target area before being encapsulated, then the effort to do it at that level wouldn't seem to be that much of a stretch...
Whether TMW would think the usage of high enough frequency to make the list is debatable, but if one never suggests... :)

Sign in to comment.