Colormap utility - two axes in colorbar

15 views (last 30 days)
Jack M
Jack M on 11 Aug 2019
Edited: Jack M on 12 Jun 2021
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.
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 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?
Thank you.

Accepted Answer

dpb
dpb on 12 Aug 2019
Edited: dpb on 12 Aug 2019
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
Jack M
Jack M on 13 Aug 2019
Edited: Jack M on 13 Aug 2019
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
dpb on 14 Aug 2019
Edited: 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.

More Answers (1)

Prasobhkumar P. P.
Prasobhkumar P. P. on 19 Aug 2020
Thank you dpb. The connection between 2 axes are achieved using updated version of your code.
https://in.mathworks.com/matlabcentral/fileexchange/79295-multiple-axes-colorbar

Categories

Find more on Colormaps in Help Center and File Exchange

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!