MATLAB Answers

How to move heatmap's Ylabel to colorbar's rightside

6 views (last 30 days)
mingmin zhang
mingmin zhang on 26 Aug 2021
Commented: mingmin zhang on 31 Aug 2021
A=[0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
0 0 0 0 0 0 0 0 1 1 1 1 1 1 1 1;
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0;
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0;
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0;
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0;
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0;
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0;
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0;
1 1 1 1 1 1 1 1 0 0 0 0 0 0 0 0;]; % here is the matrix
h1=heatmap(A)
h1.Title='This is Title'
h1.YLabel='This is YLabel'
h1.CellLabelColor = 'none';
h1.FontName='Time New Roman'
But I'd like to move the YLabel to the right side of colorbar.
Unfortunately, I've already tried many methods. but it doesn't work.

Accepted Answer

dpb
dpb on 27 Aug 2021
Edited: dpb on 27 Aug 2021
OK, one can take the last idea above and make things a little easier -- you don't actually have to have the two panels, but just one that covers the whole figure --
figure
hUIP=uipanel;
hHM=heatmap(hUIP,A);
hHM.ColorbarVisible='off';
CBPosn=[0.8500 0.1100 0.0650 0.8150];
hAx=axes(hUIP,'Position',CBPosn);
hAx.Visible='off';
hCB=colorbar(hAx,"Position",CBPosn);
colormap(hAx,hHM.Colormap)
hCB.AxisLocation='in';
hCB.Label.String='This is Colorbar Label';
produces something like--
which is reasonable approximation to the desired result I think. One might still tweak on position, size, etc., ..., but the basics seem to be there. The key to recognize is to pull the colormap from the heatmap object to apply to the new colorbar to synch them up.
NOTA BENE: The axis/colorbar position vector is the end result of a whole series of trial and error settings...for the above code I simply output the end position vector and pasted it in above.
Salt to suit, as always...

More Answers (1)

dpb
dpb on 26 Aug 2021
Edited: dpb on 27 Aug 2021
Unfortunately, you're a victim of the recent penchant of TMW to lock down these higher-level graphics objects so that the underlying axes handles (or the colorbar handle here in particular) are not only hidden but made private so not even the magic from Yair Altman's getundoc function can find the handle.
Nor can you do anything exciting like not show the builtin colorbar but then add your own so that you could use its properties--
>> h1.ColorbarVisible='off';
>> hold on
Error using hold (line 59)
Using hold with heatmap is not supported.
>>
Well, what happens if go ahead anyways...
>> hCB=colorbar(h1);
Error using matlab.graphics.chart.Chart/colorbar
Output arguments are not supported when using colorbar with heatmap.
>>
It simply traps the call and resets the 'ColorbarVisible' property back to 'on'
Well, one last thing to try...
>> hT=text(20,8,'Text Label','Rotation',90,'HorizontalAlignment','center');
Error using text
Text cannot be a child of HeatmapChart.
>>
Well, one more try...
>> hA=subplot(1,6,[1:5]);
>> hHM=heatmap(hA,A);
Error using heatmap (line 78)
HeatmapChart cannot be a child of Axes.
>> which heatmap
So, can't even put the heatmap into a subplot...
I've railed about this sort of thing over many of the new features; all I can suggest is to add your voice to the chorus of discontent with this misguided direction of taking full control away from the user--"Mathworks Knows Best!" seems to be the new mantra and users are children to be protected instead of being given the toolset.
ADDENDUM:
I still think it a very rude thing TMW has begun doing along these lines in not letting users have access to properties they might want to tweak even if the developers believe they've done the best possible design. It's still possible to have the simple-to-use case with default parameters but still let the user adjust to suit; there's no reason for making all these oddball figures that don't match the general pattern of a handle graphics axes object.
Anyways, still yet one more try shows a way on can make it work -- use two panels in the figure instead. The following still needs work to fix up the color range and adjust the sizes, and it may not be worth the effort in the long run, but is only way I can see to get the desired effect and still use the builtin heatmap.
hUIP=uipanel('Position'=[0 0 0.8 1]); % create panel to hold heatmap
hHM=heatmap(hUIP,A); % add heatmap to it
hHM.ColorbarVisible='off'; % turn off colorbar for it
hUIP(2)=uipanel('Position',[0.8 0 0.2 1]); % a second panel for the custom colorbar
hAx=axes(hUIP(2)); % have to have an axes to have CB
hAx.Visible='off'; % make it invisible
hCB=colorbar(hAx,'Position',[0.15 0.11 0.15 0.815]); % add the colorbar
hCB.Label.String='This is Colorbar Label'; % and a label
hCB.Label.Position=[2 0.5 0]; % position it to RHS
The above yields
As noted, it still needs work to adjust panel sizes, position of colorbar and the colorbar range to match, but the basic objective is doable -- albeit with much more effort than what should be needed.

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!