代码中注释都很详细,就不另外解释了
1. 画出一个基本的热力图
1.1 详细代码
%% 画出一个基本的热力图
clc;clear all;close all;
% 数据 生成一个10x10的矩阵
mydata=ones(10); %
mydata(2,:)=2;mydata(3,:)=3;mydata(4,:)=4;mydata(5,:)=5;
mydata(6,:)=6;mydata(7,:)=7;mydata(8,:)=8;mydata(9,:)=9;mydata(10,:)=10;
% 画图 并进行插值
imTest=imagesc(mydata);
imTest.Interpolation='bilinear';
colormap parula;
colorbar;
% c=colorbar;
% ylabel(c, 'phase differ') 给颜色图 加标签
ax.XTick = [1,13,25,37,49,61,73,85,97,109,121,133,144];
ax.XTickLabel = {'L1','L2','L3','L4','L5','L6','L7','L8','L9','L10','L11','L12','L13'};
% xtickangle(90) 控制角度 Y轴设置类似
xlabel('Day');
ylabel('Device');
1.2 效果图
2. 多子图共用一个colorbar/色带
2.1 详细代码
%% 多子图公用一个colorbar
clc, clear, close all;
% 设定4个子图所在的块,离图窗边框的距离
top_margin = 0.03;
btm_margin = 0.1;
left_margin = 0.08;
right_margin = 0.1;
fig_margin = 0.05; % 子图之间的距离
row = 4 ; col = 1 ; % row行 和 col列的数据
% 数据
mydata=zeros(5,40);
for i=1:5
mydata(i,:)=i;
end
clim = [0 6]; % 0到4线性对应着颜色表,数据的值在0到3之间,那么画出表的颜色,就会截取0到3之间,对应的颜色
% 根据 行数和列数,计算子图的高度和宽度
fig_h = (1 - top_margin - btm_margin - (row-1)*fig_margin) /row;
fig_w = (1 - left_margin - right_margin - (col -1) *fig_margin) /col;
for i = 1 : row % 4行
position = [left_margin ,...
1-(top_margin + i*fig_h + (i-1) * fig_margin), fig_w, fig_h];
axes('Position',position);
imTest1=imagesc(mydata,clim); % 画热力图
colormap jet; % 设置颜色库
imTest1.Interpolation='bilinear'; % 插值
%设置x轴坐标的标签
ax=gca;
ax.XTick = [4,8,12,16,20,24,28,32,36,40];
ax.XTickLabel = {'L1','L2','L3','L4','L5','L6','L7','L8','L9','L10'};
% 设置坐标轴框的宽度和字体大小
set(gca,'FontSize',10,'LineWidth',0.1);
% ylabel
ylabel(['Device' num2str(i)],'FontSize',20); %num2str 将数字转换为整形数组
set(gca,'YDir','normal'); % Y轴递增
% 只在第四个子图显示xlabel,用一个if语句,简单设置
if i ==4
xlabel('Day','FontSize',20)
end
end
% 画色带 重新定义一个坐标区,并作为当前的坐标区
axes('Position', [1-right_margin-fig_margin, btm_margin, 0.15,1-(top_margin + btm_margin)]);
axis off;
c=colorbar();
c.Position(3) = 1.2*c.Position(3);
caxis(clim); % 设置颜色图 的范围
% 固定图窗大小
set(gcf,'Position',[100 100 800 600]);
部分参考:Matlab中图形窗口的大小、字体、axis等设置
2.2 效果图
3. 多子图共用一个colorbar/色带 另一种办法
3.1 详细代码
%% 多子图共用一个color另外一种办法
clc; clear; close all;
% data
mydata=zeros(4,40);
for i=1:4
mydata(i,:)=i;
end
tiledlayout(4,1)
clim=[0 1];
% Top plot
mydata=rand(4,40);
for i=1:4
nexttile
imTest1=imagesc(mydata,clim); % 画热力图
colormap jet; % 设置颜色库
imTest1.Interpolation='bilinear'; % 插值
% xtcik, ytick的设置
set(gca,'FontSize',15,'LineWidth',0.1); % 字体大小,框的宽度
set(gca,'xtick',[]) % x轴标签不显示
set(gca,'YTick',[1 2 3 4],'YTickLabel',{'','10','20','30'}); % YTick的数值对应mydata的行
set(gca,'YDir','normal'); % Y轴数值递增
% ylabel
ylabel(['Device' num2str(i)],'FontSize',20); %num2str 将数字转换为整形数组
if i==4
% xtcik
ax=gca;
ax.XTick = [4,8,12,16,20,24,28,32,36,40]; %xtick对应mydata的列
ax.XTickLabel = {'L1','L2','L3','L4','L5','L6','L7','L8','L9','L10'};
% xlabel
xlabel('Day','FontSize',16);
end
end
% 固定图窗大小
set(gcf,'Position',[300 300 1000 500]); %左 下 右 上
% 画颜色表
colorbar
cb = colorbar;
cb.Layout.Tile = 'east';