簡體   English   中英

在不使用 Hist 的情況下在 Matlab 中生成直方圖

[英]Producing a histogram in Matlab with out using Hist

我在 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 的平均值,這將是我想要的 plot(直方圖)的 Z 值,並且我有 X,Y(所有測試都相同)來自:

x = 0:tbinstep:tbinend;
y = 0:varbinstep:varbinend;

但是我如何將這些組合在一起以制作顯示條形平均高度的圖形? 我不能再次使用 hist3 ,因為它只會計算 avedata 的分布。

有人建議使用 bar3啟動 XY 問題的風險,但這會問一個問題“我如何從 2 個向量和一個矩陣到 1 個矩陣 bar3 的 go 可以處理?即我如何處理 plot x(1), y( ), avedata(1,1) 等是否適用於 avedata 中的所有數據點?”

TIA

通過查看 matlab r2014b 中的hist3源代碼,它在內部實現了自己的繪圖,准備數據和 plot 它使用surf方法。 這是一個 function,它再現了相同的 output,靈感來自hist3 function 以及您的選項( '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 function 輸出 plot 時,您可以調用此 function。 請注意,這可以處理箱的非統一定位:

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')

這是兩個輸出的比較:

使用 hist3 函數 使用 hist3plot 函數

因此,如果我正確理解了您的問題和代碼,您將多個實驗數據的分布繪制為直方圖,那么您想要計算所有先前直方圖的平均形狀。

我通常避免給出提問者沒有明確要求的方法,但對於這個我必須評論說這是一件非常奇怪的事情。 我以前從未聽說過計算多個直方圖的平均形狀。 因此,以防萬一,您可以簡單地將 append 所有實驗數據放入一個變量中,並 plot 使用 histogram2 的歸一化histogram2 此代碼輸出相對頻率直方圖 (其他歸一化方法

% Append all data in a single matrix
x = []
for i = 1:length(files6)
    x = [x; csvread(files6(i).name)];
end

% Plot normalized bivariate histogram, normalized
xEdges = 0:tbinstep:tbinend;
yEdges = 0:varbinstep:varbinend;
histogram2(x(:,1), x(:,3), xEdges, yEdges, 'Normalize', 'Probability')

現在,如果您真的想繪制多個直方圖的平均形狀,那么可以,使用bar3 由於bar3不接受 (x,y) 值參數,因此您可以遵循其他答案,或修改XTickLabelYTickLabel屬性以匹配您的 bin 范圍。

... % data = yourAverageData;

% Save axis handle to `h`
h = bar3(data);

% Set property of axis
h.XTickLabels = 0:tbinstep:tbinend;
h.YTickLabels = 0:varbinstep:varbinend;

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM