How to modify the colorbar font and size when drawing with the heatmap function?

4 views (last 30 days)
When using the heatmap function to draw, the font and size of colorbar cannot be modified.
clear;clc;close all
M = [0.9284,0.4765,0.2391,0.2548,0.2648;
0.2575,0.8214,0.8071,0.2356,0.2339;
0.2331,0.2307,0.9944,0.5106,0.2217;
0.2449,0.2102,0.2005,0.6086,0.2167;
0.2738,0.2263,0.2052,0.2279,0.5993];
xLabels = {'20','50','100','150','200'};
yLabels = {'10','20','30','40','50'};
h = heatmap(M,'XDisplayLabels',xLabels,'YDisplayLabels',yLabels);
title('RMSE Heatmap');
xlabel('True');
ylabel('Model');fw=12;h.FontSize=fw;
colormap(h, 'parula');
set(gca,'FontName','Times New Roman','FontSize',fw);

Answers (1)

Umar
Umar on 29 Jul 2025

Hi @Wei,

To address and interpret your comments correctly, “When using the heatmap function to draw, the font and size of colorbar cannot be modified.” & going through documentation provided below,

https://www.mathworks.com/help/bioinfo/ref/heatmap.html

I implemented code which demonstrates how to set the font size for the heatmap itself and the axes, but the colorbar remains unaffected by these settings.

   % Define the matrix for the heatmap
   M = [0.9284, 0.4765, 0.2391, 0.2548, 0.2648;
     0.2575, 0.8214, 0.8071, 0.2356, 0.2339;
     0.2331, 0.2307, 0.9944, 0.5106, 0.2217;
     0.2449, 0.2102, 0.2005, 0.6086, 0.2167;
     0.2738, 0.2263, 0.2052, 0.2279, 0.5993]; 
   % Define the labels for the axes
   xLabels = {'20', '50', '100', '150', '200'}; 
   yLabels = {'10', '20', '30', '40', '50'}; 
     % Create the heatmap
     h = heatmap(M, 'XDisplayLabels', xLabels, 'YDisplayLabels', yLabels); 
     title('RMSE Heatmap');
     xlabel('True');
     ylabel('Model');
   % Set font size for the heatmap
   fw = 12; 
   h.FontSize = fw;
   % Set the colormap
   colormap(h, 'parula');
   % Customize the axes font properties
   set(gca, 'FontName', 'Times New Roman', 'FontSize', fw);

Please see attached.

Hope this helps.

Categories

Find more on Data Distribution Plots in Help Center and File Exchange

Products


Release

R2020a

Community Treasure Hunt

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

Start Hunting!