% 數(shù)據(jù) x = randn(1000,1); % 直方圖 histogram(x);
這段代碼生成了一個(gè)大小為1000的隨機(jī)值的正態(tài)分布直方圖。使用histogram函數(shù)可以對數(shù)據(jù)進(jìn)行分組,并且自動(dòng)計(jì)算組距。同時(shí),可以使用histogram函數(shù)中的一些其他參數(shù)來設(shè)置圖形的一些屬性,如修改顏色、邊界和曲線等。
% 數(shù)據(jù) x = randn(1000,1); % 直方圖 histogram(x,'Normalization','probability');
% 數(shù)據(jù) x = randn(1000,1); % 直方圖 h = histogram(x); % 修改屬性 h.FaceColor = [0, 0.5, 0.5]; h.EdgeColor = 'none'; h.BinWidth = 0.5; h.Normalization = 'probability'; h.FaceAlpha = 0.75; % 顯示直方圖 xlabel('x'); ylabel('Frequency'); title('Normal distribution histogram');
% 數(shù)據(jù) x = randn(1000,1); % 直方圖 histogram(x,'BinLimits',[-5,5]);
% 數(shù)據(jù) x = randn(1000,1); % 直方圖 [counts,edges] = histcounts(x); % 顯示計(jì)數(shù)和邊界 counts edges
bar函數(shù):與histogram函數(shù)相比,bar函數(shù)在繪制直方圖時(shí)較為常見,它可以用于將一維離散數(shù)據(jù)轉(zhuǎn)換為直方圖。
% 數(shù)據(jù)
x = randn(1000,1);
% 統(tǒng)計(jì)每個(gè)bin中元素個(gè)數(shù)
[counts,edges] = histcounts(x);
binWidth = edges(2)-edges(1);
% 顯示直方圖
bar(edges(1:end-1),counts/(binWidth*length(x)),1);
% 數(shù)據(jù) x = randn(1000,1); % 直方圖 histogram(x); % 調(diào)整圖形大小 fig = gcf; fig.Position = [100, 100, 800, 600];
% 讀取圖像 I = imread('peppers.png'); % 繪制灰度直方圖 imhist(rgb2gray(I)); % 顯示圖像 imshow(I);
% 數(shù)據(jù) x = randn(1000,1); y = 0.5*x + randn(1000,1)/3; % 直方圖 histogram2(x,y,'FaceColor','flat','DisplayStyle','tile'); % 顯示標(biāo)簽 xlabel('X'); ylabel('Y');
% 數(shù)據(jù) x = randn(1000,1); % 計(jì)算頻率直方圖 [counts,edges] = histcounts(x,10,'Normalization','probability'); binWidth = edges(2)-edges(1); % 顯示直方圖 bar(edges(1:end-1),counts,1); % 顯示標(biāo)簽 xlabel('X'); ylabel('Frequency');