[HEATMAP] Display single colorbar for a multiplot

조회 수: 34 (최근 30일)
Giovanni Bambini
Giovanni Bambini 2023년 8월 28일
댓글: Adam Danz 2023년 8월 29일
As in the title, I need help displaying a single colorbar for several subplots of HEATMAPS.
I tried both with subplots, and tiled design, but none works.
Please, I need help with heatmaps: before people start saying "duplicate", I would like to point out that the methods for countours() and surf() DO NOT WORK with heatmap().
Thanks for the help
E.
  댓글 수: 3
Giovanni Bambini
Giovanni Bambini 2023년 8월 28일
yes. That refers to conturf. With heatmap I get a Matlab error
Dyuman Joshi
Dyuman Joshi 2023년 8월 28일
It is notoriously hard to work with heatmap and their colorbars.
The only way I could find to make it work is if the layout for subplot/tiledlayout is horizontal.

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

채택된 답변

Adam Danz
Adam Danz 2023년 8월 28일
편집: Adam Danz 2023년 8월 28일
Here's a demo showing the following steps needed to create a figure with multiple heatmaps that share the same global colorbar. Since a colorbar cannot be assigned to a heatmap, an invisible axes is created to host the colorbar.
  1. Create the heatmaps within a tiledlayout. Turn off colorbar visibility.
  2. Compute the global color limits which is the [min,max] range of color limits between all heatmaps.
  3. Set the color limits for each heatmap to the global limits since the heatmaps will all share the same colorbar
  4. Create an invisible axes that uses the same colormap as the first heatmap and the same color limits as the global color limits.
Note that this approach does not use the same type of colorbar as heatmap. The missing values indicator in heatmap's colorbar will not appear using this approach.
rng('default')
fig = figure();
tcl = tiledlayout(fig,2,2);
n = 4; % number of heatmaps
h = gobjects(n,1);
for i = 1:n
ax = nexttile(tcl);
h(i) = heatmap(rand(5)*randi(5),'ColorbarVisible','off');
end
% Equate color limits in all heatmaps
colorLims = vertcat(h.ColorLimits);
globalColorLim = [min(colorLims(:,1)), max(colorLims(:,2))];
set(h, 'ColorLimits', globalColorLim)
% Create global colorbar that uses the global color limits
ax = axes(tcl,'visible','off','Colormap',h(1).Colormap,'CLim',globalColorLim);
cb = colorbar(ax);
cb.Layout.Tile = 'East';
  댓글 수: 2
Giovanni Bambini
Giovanni Bambini 2023년 8월 29일
I did not understand the note you wrote.
In my case everything is working, thanks for the help, you were super useful and clear.
Adam Danz
Adam Danz 2023년 8월 29일
Thanks @Giovanni Bambini. Heatmap's colorbar has a special feature when there are missing values in the heatmap data. It indicates missing values in a small box under the colorbar. My solution above creates a regular colorbar which will not have this feature.
Here's a demo
data = magic(5);
data([1 5 20]) = NaN;
heatmap(data)

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

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by