数学建模社区-数学中国

标题: 美赛O奖中看似简单的热力图,好多人都不会画! [打印本页]

作者: 嘎嘎12138    时间: 2024-1-24 15:16
标题: 美赛O奖中看似简单的热力图,好多人都不会画!
热力图简介
热力图是一种有效的可视化手段,美赛论文中频繁会用到的,利用颜色的变化、深浅,配合不同的底图(网页、地图等),可以将离散的数据,通过密度云图的方式形象的呈现出来。
下面主要以几种方式构建数学建模竞赛中的热力图。
案例一:
cdata = [1 2 3 4 5; 5 4 3 2 1; 1 2 3 4 5; 5 4 3 2 1; 1 2 3 4 5];
xvalues = {'1x', '2x', '3x', '4x', '5x'};
yvalues = {'1y', '2y', '3y', '4y', '5y'};
h = heatmap(xvalues, yvalues, cdata);



clc;clear;close all;
% 定义点(x,y,z)
x = randn(50,1);
xmax = max(x);
xmin = min(x);
y = randn(50,1);
ymax = max(y);
ymin = min(y);
z = exp(sin(x.^2)) + exp(cos(y.^2));
N = 500; % 每个维度的数据点数
% 网格化x,y二维空间
[X,Y] = meshgrid(linspace(xmin,xmax,N),linspace(ymin,ymax,N));
% 采用插值法扩展数据,可用方法有'linear'(default)|'nearest'|'natural'|'cubic'|'v4'|
Z = griddata(x,y,z,X,Y,'v4');
%% 等高线法
figure('NumberTitle','off','Name','等高线法','Color','w','MenuBar','none','ToolBar','none');
contourf(X,Y,Z,N, 'LineColor','none');
colormap('jet');
colorbar;
axis off;
%% 投影图法
figure('NumberTitle','off','Name','投影图法','Color','w','MenuBar','none','ToolBar','none');
surf(X,Y,Z,'LineStyle','none');
xlim([min(X(:)) max(X(:))]);
ylim([min(Y(:)) max(Y(:))]);
axis off;
colormap('jet');
colorbar;
shading interp;
view(0,90);
%% imagesc法
figure('NumberTitle','off','Name','imagesc法','Color','w','MenuBar','none','ToolBar','none');
% 因为图像坐标和笛卡尔坐标起始位置不一样,需要上下翻转
imagesc(flipud(Z));
colormap('jet');
colorbar;
axis off;
%% pcolor法
figure('NumberTitle','off','Name','pcolor法','Color','w','MenuBar','none','ToolBar','none');
pcolor(X,Y,Z);
colormap('jet');
colorbar;
shading interp;
axis off;














欢迎光临 数学建模社区-数学中国 (http://www.madio.net/) Powered by Discuz! X2.5