Edit colorbar of heatmap (string)
이전 댓글 표시
Hi,
Is it possible to edit the colorbar of a heatmap such that it looks like the following image (rapidly created with paint ;) )

So the values aren't numeric but strings.
Thanks for help!
댓글 수: 1
The handle to the colorbar in heatmap is inaccessible. Without the handle, you can't change properties to the colorbar; you can't even delete it. Even when you try to add a new colorbar, you cannot output the handle with a heatmap plot.
heatmap() in general is highly restrictive in the user's abilty to manipulate many of its properties. I used to use heatmap more frequently prior to these changes and have moved onto using imagesc() instead. Bummer.
채택된 답변
추가 답변 (2개)
Christian Karcher
2020년 4월 28일
편집: Christian Karcher
2020년 4월 28일
The handle to the colorbar is accessible, and with it all modifications:
figure;
heatmap(rand(5))
axs = struct(gca); %ignore warning that this should be avoided
cb = axs.Colorbar;
cb.TickLabels = {'p<0.1','p>0.1','...'};
댓글 수: 7
Adam Danz
2020년 4월 28일
You can't assign the tick labels without first assigning the ticks.
Christian Karcher
2020년 4월 28일
well... you can?
It just replaces the automatically generated ticks from the heatmap. It's a working minimal example, feel free to copy & paste it into matlab and see for yourself.
If you want to have specific tick replacement, of course you should re-define the ticks first, but it should really just be a minimal example, not a full customizable colorbar wondertool ;)
giannit
2021년 5월 12일
many thanks! this should be the accepted answer
Alexander Krauss
2022년 9월 28일
Dear Christian,
is it also possible to set the colorbar to a percent scale?
I know it works for standard colormaps, but I don't know whether you can change it in a heatmap.
Would be very glad if you could help me out. :)
Best regards,
Alex
Christian Karcher
2022년 9월 29일
편집: Christian Karcher
2022년 9월 29일
I am not aware of an option to directly format a value to percentage.
But the ticks can be directly reformated. So it would be a two-stage process:
- multiply your data by 100
- add a percent sign to the labels
figure;
heatmap(rand(5)*100); % convert data to percent
axs = struct(gca); %ignore warning that this should be avoided
cb = axs.Colorbar;
cb.Ruler.TickLabelFormat = '%g%%'; % add percent sign to labels
See https://de.mathworks.com/help/matlab/matlab_prog/formatting-strings.html for all your formatting needs.
Alexander Krauss
2022년 9월 29일
편집: Alexander Krauss
2022년 9월 29일
Great solution, thanks a lot (also for the rapid reply) ! :)
Best regards,
Alex
Muhanned Alsaif
2023년 8월 21일
편집: Muhanned Alsaif
2023년 8월 21일
another way to access the colorbar can be found in the following post:
Tony Castillo
2023년 12월 19일
0 개 추천
Is it possible to add a label to that colour bar which is directly created in a heatmap? I mean we can tag a SURF plot (the colourbar because it is separated from the surfplot), but I have not found the manner of doing the same in the colorbar of the heatmap.
카테고리
도움말 센터 및 File Exchange에서 Data Distribution Plots에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!
