我有一个带有x和y坐标的矩阵以及每个数据点的温度值.当我在散点图中绘制它时,一些数据点会使其他数据点模糊,因此,该图不能真实地表示我的数据集中温度如何变化.
为了解决这个问题,我想降低图表的分辨率并创建像素,这些像素代表像素区域内所有数据点的平均温度.另一种思考问题的方法是我需要将网格放在当前图上并平均网格的每个网段内的值.
我找到了这个线程 - 使用散点数据集在MatPlotLib中生成热图 - 它显示了如何使用python来实现我想要的最终结果.但是,我目前的代码是在MATLAB中,即使我尝试了不同的建议,如热图,轮廓f和图像c,我也无法得到我想要的结果.
mat*_*fee 10
您可以使用accumarray "降低数据的分辨率",您可以在其中指定每个点应该输入的"bin",并指定您希望对该bin中的所有点取平均值.
一些示例数据:
% make points that overlap a lot
n = 10000
% NOTE: your points do not need to be sorted.
% I only sorted so we can visually see if the code worked,
% see the below plot
Xs = sort(rand(n, 1));
Ys = rand(n, 1);
temps = sort(rand(n, 1));
% plot
colormap("hot")
scatter(Xs, Ys, 8, temps)
Run Code Online (Sandbox Code Playgroud)

(我只排序Xs并temps为了获得上面的条纹图案,以便我们可以直观地验证"降低的分辨率"是否有效)
现在,假设我想通过获得只差一分每减少我的数据的分辨率0.05单位在X和Y方向,它是所有点的平均值在该广场(所以自从我X和Y从0到1,我会得到总共20*20分).
% group into bins of 0.05
binsize = 0.05;
% create the bins
xbins = 0:binsize:1;
ybins = 0:binsize:1;
Run Code Online (Sandbox Code Playgroud)
我用来计算histc每个X和Y所在的箱子(注意 - 在这种情况下,因为箱子是常规的,我也可以这样做idxx = floor((Xs - xbins(1))/binsize) + 1)
% work out which bin each X and Y is in (idxx, idxy)
[nx, idxx] = histc(Xs, xbins);
[ny, idxy] = histc(Ys, ybins);
Run Code Online (Sandbox Code Playgroud)
然后我accumarray用来做temps每个箱子里面的意思:
% calculate mean in each direction
out = accumarray([idxy idxx], temps', [], @mean);
Run Code Online (Sandbox Code Playgroud)
(注-这意味着在点temps(i)在行属于"像素"(我们的产出矩阵)的idxy(1)列idxx(1).我做了[idxy idxx],而不是[idxx idxy]使所产生的基质具有Ÿ==行和X ==列))
你可以这样画:
% PLOT
imagesc(xbins, ybins, out)
set(gca, 'YDir', 'normal') % flip Y axis back to normal
Run Code Online (Sandbox Code Playgroud)

或者像这样的散点图(我在'像素'的中点绘制每个点,并绘制原始数据点以进行比较):
xx = xbins(1:(end - 1)) + binsize/2;
yy = ybins(1:(end - 1)) + binsize/2;
[xx, yy] = meshgrid(xx, yy);
scatter(Xs, Ys, 2, temps);
hold on;
scatter(xx(:), yy(:), 20, out(:));
Run Code Online (Sandbox Code Playgroud)
