matlab - 在不使用 Hist 的情况下在 Matlab 中生成直方图
问题描述
我在 Matlab 中使用直方图来查看我的实验中一些数据的分布。我想从一组测试中找到平均分布(条形的平均高度),然后生成一个平均直方图。
通过使用此代码:
data = zeros(26,31);
for i = 1:length(files6)
x = csvread(files6(i).name);
x = x(1:end,:);
time = x(:,1);
variable = x(:,3);
thing(:,1) = x(:,1);
thing(:,2) = x(:,3);
figure()
binCenter = {0:tbinstep:tbinend 0:varbinstep:varbinend};
hist3(thing, 'Ctrs', binCenter, 'CDataMode','auto','FaceColor','interp');
colorbar
[N,C] = hist3(thing, 'Ctrs', binCenter);
data = data + N;
clearvars x time variable
end
avedata = data / i;
我可以找到 N 的平均值,这将是我想要的图(直方图)的 Z 值,并且我有 X,Y(所有测试都相同)来自:
x = 0:tbinstep:tbinend;
y = 0:varbinstep:varbinend;
但是我如何将这些组合在一起以制作显示条形平均高度的图形?我不能再次使用 hist3 ,因为它只会计算 avedata 的分布。
有人建议使用 bar3 来引发XY 问题,但这会提出一个问题“我如何从 2 个向量和一个矩阵变为 bar3 可以处理的 1 个矩阵?即我如何绘制 x(1), y(1 ), avedata(1,1) 等是否适用于 avedata 中的所有数据点?”
TIA
解决方案
通过查看hist3
matlab r2014b中的源代码,它内部实现了他自己的绘图,准备数据并使用surf
方法绘制它。hist3
这是一个使用您的选项 ( )重现相同输出的函数'CDataMode','auto','FaceColor','interp'
。您可以将其放在一个名为的新文件中hist3plot.m
:
function [ h ] = hist3plot( N, C )
%HIST3PLOT Summary of this function goes here
% Detailed explanation goes here
xBins = C{1};
yBins = C{2};
% Computing edges and width
nbins = [length(xBins), length(yBins)];
xEdges = [0.5*(3*xBins(1)-xBins(2)), 0.5*(xBins(2:end)+xBins(1:end-1)), 0.5*(3*xBins(end)-xBins(end-1))];
yEdges = [0.5*(3*yBins(1)-yBins(2)), 0.5*(yBins(2:end)+yBins(1:end-1)), 0.5*(3*yBins(end)-yBins(end-1))];
xWidth = xEdges(2:end)-xEdges(1:end-1);
yWidth = yEdges(2:end)-yEdges(1:end-1);
del = .001; % space between bars, relative to bar size
% Build x-coords for the eight corners of each bar.
xx = xEdges;
xx = [xx(1:nbins(1))+del*xWidth; xx(2:nbins(1)+1)-del*xWidth];
xx = [reshape(repmat(xx(:)',2,1),4,nbins(1)); NaN(1,nbins(1))];
xx = [repmat(xx(:),1,4) NaN(5*nbins(1),1)];
xx = repmat(xx,1,nbins(2));
% Build y-coords for the eight corners of each bar.
yy = yEdges;
yy = [yy(1:nbins(2))+del*yWidth; yy(2:nbins(2)+1)-del*yWidth];
yy = [reshape(repmat(yy(:)',2,1),4,nbins(2)); NaN(1,nbins(2))];
yy = [repmat(yy(:),1,4) NaN(5*nbins(2),1)];
yy = repmat(yy',nbins(1),1);
% Build z-coords for the eight corners of each bar.
zz = zeros(5*nbins(1), 5*nbins(2));
zz(5*(1:nbins(1))-3, 5*(1:nbins(2))-3) = N;
zz(5*(1:nbins(1))-3, 5*(1:nbins(2))-2) = N;
zz(5*(1:nbins(1))-2, 5*(1:nbins(2))-3) = N;
zz(5*(1:nbins(1))-2, 5*(1:nbins(2))-2) = N;
% Plot the bars in a light steel blue.
cc = repmat(cat(3,.75,.85,.95), [size(zz) 1]);
% Plot the surface
h = surf(xx, yy, zz, cc, 'CDataMode','auto','FaceColor','interp');
% Setting x-axis and y-axis limits
xlim([yBins(1)-yWidth(1) yBins(end)+yWidth(end)]) % x-axis limit
ylim([xBins(1)-xWidth(1) xBins(end)+xWidth(end)]) % y-axis limit
end
然后,当您想绘制 Matlab 函数的输出时,可以调用此hist3
函数。请注意,这可以处理箱的非统一定位:
close all; clear all;
data = rand(10000,2);
xBins = [0,0.1,0.3,0.5,0.6,0.8,1];
yBins = [0,0.1,0.3,0.5,0.6,0.8,1];
figure()
hist3(data, {xBins yBins}, 'CDataMode','auto','FaceColor','interp')
title('Using hist3')
figure()
[N,C] = hist3(data, {xBins yBins});
hist3plot(N, C); % The function is called here
title('Using hist3plot')
这是两个输出的比较:
推荐阅读
- reporting-services - 不同报告服务器上的不同数字格式
- java - 当字符都具有相似的重复时,使用霍夫曼编码压缩文件?
- c# - 查询 DBLink 时 OracleDataAdapter 未填充 DataTable
- python-3.x - 重新循环直到找到所有匹配项,逻辑?
- javascript - 无法在 NetCore 2 上添加 ReactJS.NET
- netsuite - NetSuite - 保存的搜索 - 动态日期范围公式
- azure-resource-manager - 部署 ARM 模板时出现内部服务器错误
- cytoscape.js - 带有 Dagre 布局的 Cytoscape.js 可以绘制垂直树吗?
- java - Eclipselink 尝试在表中插入空值
- scala - 将 List[Any] 中的每个对象转换为特定类型?