【问题标题】:Generate heatmap with coordinates and data stored in vectors使用存储在向量中的坐标和数据生成热图
【发布时间】:2015-04-13 18:48:44
【问题描述】:

令 A 是一个 n×3 矩阵,这样前两列都是 (5*i,5*i) 形式的有序对,i 从 1 到 200。第三列包含从 0 到 1 的值,我将其称为强度。我想制作一个 1000 x 1000 的图,以便 (5*i,5*i) 处的矩形被第三列条目描述的强度着色。

我对热图函数和 imshow 很熟悉,但我看不出有一种方法可以包含这个“按 5 缩放”来制作漂亮的图。当然,通常 x 和 y 坐标的缩放比例可能不同。

在 Matlab 中有没有很好的方法来做到这一点?

【问题讨论】:

  • 如果您只有 1000x2 的数据,那么 1000x1000 的图(或 200x200,如果您考虑步长)应该是什么样子?
  • 是的,你是对的,它没有写正确。我的意思是每一个可能的配对。我将立即编辑。

标签: matlab plot matlab-figure heatmap


【解决方案1】:

使用imagesc 实际上非常简单:

首先是一些示例数据:

%// generate example data
ii = 1:200;
[xx,yy] = meshgrid(ii);
A(:,1) = 5*xx(:);
A(:,2) = 5*yy(:);
A(:,3) = randi([0,1],1,40000);

实际答案

n = 200;

%// reshape data
D = reshape( A(:,3),n,n );

%// heatmap
imagesc(A(:,1),A(:,2),D)
colormap(gray)
caxis([0,1])

给予:

重要提示

如果您的坐标未按照imagesc 的要求排序,您可以使用以下命令对其进行排序:

A = sortrows(A,[2,1]);

小丑例子

%// original image
load clown
I = reshape(1:numel(X),size(X));
[R,C] = ind2sub(size(X),I);
A(:,1) = R(:);
A(:,2) = C(:);
A(:,3) = X(:);
D = reshape( A(:,3),200,320 );
figure(1)
subplot(1,3,1)
imagesc(A(:,1),A(:,2),D)

%// shuffled image -> shuffled data
shuffle = randperm(320*200);
A = A(shuffle,:);
D = reshape( A(:,3),200,320 );
subplot(1,3,2)
imagesc(A(:,1),A(:,2),D)

%// sorted image
A = sortrows(A,[2,1]);
D = reshape( A(:,3),200,320 );
subplot(1,3,3)
imagesc(A(:,1),A(:,2),D)

你看,即使你的坐标排序很乱,你也可以用sortrows重建图像。

【讨论】:

  • 该图看起来是黑白的,因为示例中的所有“温度”值都是 0 或 1。假设实际温度数据没有那么极化,这将得到一个非常漂亮的强度图。
  • stepsize 是如何使用的?
  • @willpower2727 是的,我看错了。但实际上并不重要。
  • @Lepidopterist 你不需要它。
【解决方案2】:

this

function DrawHeatmap(X,Y,Z)
%DRAWHEATMAP Draw a 2D heatmap for (X,Y) coordinates whose values are in Z
%   X, Y , Z must be columns
%   By: Eng. Osama Talaat Abdel-Hafiz - PhD Student
%   Egypt - Sept 2017
    if size(X,2)==1 && size(Y,2)==1 && size(Z,2)==1
        F = scatteredInterpolant(X,Y,Z); % create a function from interpolation
        [X,Y] = meshgrid(min(X):0.1:max(X),min(Y):0.1:max(Y));
        Z = F(X,Y);
        contourf(X, Y, Z, linspace(floor(min(min(Z))),ceil(max(max(Z))),400), 'LineColor','none')
        colorbar;
    else
        error('X, Y , Z must be columns')
    end
end

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-05
    • 2014-05-17
    • 1970-01-01
    • 2017-10-15
    • 2022-01-15
    • 1970-01-01
    相关资源
    最近更新 更多