[HEATMAP] Display single colorbar for a multiplot
    21 Ansichten (letzte 30 Tage)
  
       Ältere Kommentare anzeigen
    
    Giovanni Bambini
 am 28 Aug. 2023
  
    
    
    
    
    Kommentiert: Adam Danz
    
      
 am 29 Aug. 2023
            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 Kommentare
  Dyuman Joshi
      
      
 am 28 Aug. 2023
				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.
Akzeptierte Antwort
  Adam Danz
    
      
 am 28 Aug. 2023
        
      Bearbeitet: Adam Danz
    
      
 am 28 Aug. 2023
  
      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. 
- Create the heatmaps within a tiledlayout. Turn off colorbar visibility.
- Compute the global color limits which is the [min,max] range of color limits between all heatmaps.
- Set the color limits for each heatmap to the global limits since the heatmaps will all share the same colorbar
- 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 Kommentare
  Adam Danz
    
      
 am 29 Aug. 2023
				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)
Weitere Antworten (0)
Siehe auch
Kategorien
				Mehr zu Data Distribution Plots finden Sie in Help Center und File Exchange
			
	Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!




