Hi,
Is it possible to edit the colorbar of a heatmap such that it looks like the following image (rapidly created with paint ;) )
a.PNG
So the values aren't numeric but strings.
Thanks for help!

1 件のコメント

Adam Danz
Adam Danz 2019 年 6 月 26 日
編集済み: Adam Danz 2019 年 6 月 28 日
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.

サインインしてコメントする。

 採用された回答

Adam Danz
Adam Danz 2019 年 6 月 26 日
編集済み: Adam Danz 2019 年 6 月 27 日

2 投票

This can't be done with heatmap() (see comments under the question).
If you're using a different function that allows access to the colobar, it's fairly straightforward. Here's a demo.
% Create demo
C = rand(3,4)/10;
imagesc(C)
cbh = colorbar();
% set color range
caxis([0,.1])
% set ticks
set(cbh, 'YTick', [0.001, 0.01, 0.05, .1], ...
'YTickLabel', {'p=0.001', 'p=0.01', 'p=0.05', 'p=.1'})

その他の回答 (2 件)

Christian Karcher
Christian Karcher 2020 年 4 月 28 日
編集済み: Christian Karcher 2020 年 4 月 28 日

10 投票

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
Adam Danz 2020 年 4 月 28 日
You can't assign the tick labels without first assigning the ticks.
Christian Karcher
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
giannit 2021 年 5 月 12 日
many thanks! this should be the accepted answer
Alexander Krauss
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
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:
  1. multiply your data by 100
  2. 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
Alexander Krauss
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
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
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 についてさらに検索

製品

リリース

R2019a

質問済み:

2019 年 6 月 26 日

回答済み:

2023 年 12 月 19 日

Community Treasure Hunt

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

Start Hunting!

Translated by