Colormap utility - two axes in colorbar

조회 수: 30 (최근 30일)
Jack M
Jack M 2019년 8월 11일
편집: Jack M 2021년 6월 12일
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.

채택된 답변

dpb
dpb 2019년 8월 12일
편집: dpb 2019년 8월 12일
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
Jack M
Jack M 2019년 8월 13일
편집: Jack M 2019년 8월 13일
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 2019년 8월 14일
편집: dpb 2019년 8월 14일
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... :)

댓글을 달려면 로그인하십시오.

추가 답변 (1개)

Prasobhkumar P. P.
Prasobhkumar P. P. 2020년 8월 19일
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

카테고리

Help CenterFile Exchange에서 Axis Labels에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by