MATLAB Answers

# 3D Histgram plot for [N,M] Matrix, visualize result monte carlo simulation

14 views (last 30 days)
Niels van Dijk on 10 Dec 2015
Commented: Niels van Dijk on 13 Dec 2015
Dear all,
I have a question about the visualization of a matrix created by the use of a monte carlo analysis.
With the use of a monte carlo analysis for different cases ( different angle of the cone of a structure) a Matrix of 31*1000 has been created. For this example 1000 times the calculation result of the monte carlo simulation. And 31 times a different angle of the cone, 30 degrees till 60 degrees.
Now i would like to create a 3D histogram to visualize the results compared to eachother. For now i have: A=[31,1000] hist(A(1,:),30)
Which result in the histogram i like, the results of the monte carlo nicely stored in 30 bars. but only for angle 1 (30 deg).
I would like a z-axis (3D) to visualize the other angles of the histogram. Kind of a surf(A) commmand but then histograms. The hist3(A) command doesnt give the pleased result (only 2 columns possible) but gives an indication of how i would like my histgram 3D visualization.
I hope i'm clear enough. Can anyone help me with this one?
##### 0 CommentsShowHide -1 older comments

Sign in to comment.

### Accepted Answer

arich82 on 11 Dec 2015
It seems like you just want a histogram for each of the 30 cases. If so, this is done very simply using histc and bar3 (essentially just two lines of code; three if you don't use the default edges): rng(0);
% make dummy data
ncases = 30;
N = 1000;
data = NaN(ncases, N);
for k = 1:ncases
% generate a dummy random-normal distribution
% with random mean and random standard deviation
data(k, :) = rand(1) + rand(1)*randn(N, 1);
end
% generate data for histograms: 1 histogram per column
edges = [-5:0.5:5]; % bin edges
counts = histc(data, edges, 2); % specify dim 2 to act column-wise
% plot results
hf = figure;
ha = axes;
hb = bar3(edges, counts.'); % note the transpose to get the colors right
xlabel('case number')
ylabel('bins');
zlabel('count');
Please accept this answer if it helps, or let me know in the comments if I've missed something.
##### 1 CommentShowHide None
Niels van Dijk on 13 Dec 2015
Dear Arich82,
The visualization of this example is exactly what i'm looking for!
Unfortunately i can't manage to implement your guidelines correctly.
What am i doing wrong in my code? The input value is Fh_u, this is a matrix of 31*100000 Where 31 are the cone angles. And 100000 the number of simulations.
The code for the plot i have now is as follows:
figure (4)
bx1 = subplot(2,2,1);
edges = [1500000:2000000:2500000]; I dont know if correctly stated now, values of the simulation vary from 1.5 - 2.5 *10^6 Newton._
counts = histc(Fh_u,edges);
bar3(edges,counts);
grid on;
grid minor;
h = findobj(gca,'Type','patch');
h.FaceColor = [0 0.5 0.5];
h.EdgeColor = 'w';
title('Upward bending - F_h')
zlabel('Count')
ylabel('Newton')
xlabel('Angle of Cone')

Sign in to comment.

### More Answers (1)

Mike Garrity on 10 Dec 2015
Is histogram2 what you're looking for? ##### 3 CommentsShowHide 2 older comments
Mike Garrity on 11 Dec 2015
I'm not quite sure I'm following. Do you mean something like this?
% Generate some fake data
npts = 1000;
data = zeros(30,npts);
for i=1:30
data(i,:) = rand(1)*(randn(1,npts)-1) + rand(1)*(randn(1,npts)+1);
end If so, the bad news is that I ran into a number of bugs trying to create this. The histogram object wasn't really happy with what I was trying to do to it. Here's what I came up with, but it's kind of lame.
% Create 30 histograms and rotate them into place
max_val = 0;
for i=1:30
g = hgtransform('Matrix',makehgtform('translate',[0 i 0], ...
'xrotate',pi/2));
if i==1
hold on
end
h = histogram(data(i,:),'BinWidth',.2,'FaceAlpha',1);
h.Parent = g;
max_val = max(max_val,max(h.Values));
end
% Setup axes correctly
set(gca,'SortMethod','depth')
xlim([-pi pi])
ylim([1 30])
zlim([0 max_val])
view(3)
xlabel('Value')
ylabel('Series')
zlabel('Probability')
box on
It might make more sense to use histcounts with bar3 or waterfall if this is the sort of thing you're after.

Sign in to comment.

### Community Treasure Hunt

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

Start Hunting!