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

조회 수: 2 (최근 30일)
Wei
Wei 2025년 7월 29일
답변: Umar 2025년 7월 29일
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);

답변 (1개)

Umar
Umar 2025년 7월 29일

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.

카테고리

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

제품


릴리스

R2020a

Community Treasure Hunt

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

Start Hunting!

Translated by