How to plot histogram using matrix?

43 views (last 30 days)
Dameon Solestro
Dameon Solestro on 25 Nov 2021
Edited: DGM on 25 Nov 2021
I want to plot a histogram where the a value in the first column is the measurement of the rest of the columns. so example would be, the first column, first row, (Which is 1). It would plot the values 0.3 0.2 and 0.3. so the histogram would be the 0.2 and 0.3 and the 0.3 would be counted twice
T =[1 0.3 0.2 0.3; 2 0.2 0.1 0.2 ; 3 0.7 0.9 0.7 ; 4 0.9 0.2 0.1]
T = 4×4
1.0000 0.3000 0.2000 0.7000 2.0000 0.2000 0.1000 0.2000 3.0000 0.7000 0.9000 0.7000 4.0000 0.9000 0.2000 0.1000

Accepted Answer

DGM
DGM on 25 Nov 2021
If I'm understanding the problem correctly, you're trying to plot a series of histograms, where column 1 is merely ordinal information, and you want a histogram for each row.
% parameters
nbins = 20; % use however many bins you want
% test data
T = [1 0.3 0.2 0.3; 2 0.2 0.1 0.2 ; 3 0.7 0.9 0.7 ; 4 0.9 0.2 0.1];
% build histograms with fixed (common) binning
datarange = [min(T(:,2:end),[],'all') max(T(:,2:end),[],'all')];
edges = linspace(datarange(1),datarange(2),nbins+1);
numframes = size(T,1);
H = zeros(numframes,nbins);
for f = 1:numframes
H(f,:) = histcounts(T(f,2:end),edges);
end
% adjust bin labels to indicate bin center value
xticks = linspace(1,nbins,nbins);
hbw = 0.5*abs(diff(datarange))/(nbins+1);
xlabels = linspace(datarange(1)+hbw,datarange(2)-hbw,nbins);
% plot everything
h = bar3(H);
% color plots based on z instead of x
for k = 1:length(h)
h(k).CData = h(k).ZData;
h(k).FaceColor = 'interp';
h(k).EdgeAlpha = 0.5;
h(k).EdgeColor = [0 0 0];
end
set(gca,'xtick',xticks);
set(gca,'xticklabels',num2str(xlabels',3));
view(40,14)
xlabel('bin')
ylabel('frame')
colormap(jet)
This example uses a set of common, uniformly-distributed bins for all rows. If you want to let each row have its own bin locations, you can, but it'll pretty much make the xticks (bin location) unreadable since the bars won't align.
  4 Comments
DGM
DGM on 25 Nov 2021
If you want to count the frequency of unique values of each row, then a method like above won't work. Even if bar3() would allow explicit xdata, managing the labeling would be a complete mess. There may be a way to set those positions via manipulation of the object vertices directly, but that's going to be another mess to think about.
The frequency information can easily be calculated using tabulate() if you have the stats toolbox, or using unique() and hist()/histc() if you don't have stats toolbox.
DGM
DGM on 25 Nov 2021
Edited: DGM on 25 Nov 2021
For instance:
% parameters
nbins = 20; % use however many bins you want
barwidth = 0.08;
% test data
T = [1 0.3 0.2 0.3;
2 0.2 0.1 0.2;
3 0.7 0.9 0.7;
4 0.9 0.2 0.1;
5 0.8 0.5 0.3;
6 0.6 0.1 0.1;
7 0.2 0.4 0.6;
8 0.5 0.2 0.5;
9 0.2 0.3 0.4;
10 0.1 0.9 0.1];
% build histograms with fixed (common) binning
hbw = barwidth/2;
hold on;
for f = 1:size(T,1)
tt = tabulate(T(f,2:end));
% plot
h = bar3(tt(:,1),tt(:,2));
h.XData = h.XData + (f-1);
% need to enforce fixed bar width
YD = NaN(fliplr(size(h.YData)));
for b = 1:size(tt,1)
idx = [2 3 5:8 18 19] + 24*(b-1);
YD(idx) = tt(b,1)-hbw;
idx = [9:12 14 15] + 24*(b-1);
YD(idx) = tt(b,1)+hbw;
end
h.YData = YD.';
end
grid on
view(-56,52)
xlabel('frame')
I omitted the colormappng in this example for simplicity. As you can tell, this is starting to get messy.

Sign in to comment.

More Answers (0)

Products

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!