Plot histogram for multiple cases in 3 dimensions

115 views (last 30 days)
Hi, I'm Alex and I would like to ask a question regarding the possibilities of a histogram plot.
I like to plot a "2D" histogram from a 30 by N matrix. In this 30 by N matrix data from a Monte Carlo simulation is stored for 30 different cases. The data stored in de N colums can be used for the "2D" histogram plot. However, there comes the problem. I like to plot a "2D" histogram of every case into one single '3D' plot.
If you take axis x,y,z. The plot would be something like: Horizontal x-axis is for the data stored in the N columns (Data from Monte Carlo simulation). The Vertical y-axis is for the frequency of those values to create a histogram (I call this 2D). The Horizontal z-axis is for the 30 different cases for which I like to create a similar histogram in the x- and y-axis.
The confusion is the way "2D" or "3D" histograms work. I'm not interested in a comparison histogram between the values of every case. I simply want to show every histogram into one single figure so I can show the trent between the different cases.
Thanks in advance!
Is this even possible with histograms or should we use a different plot style?

Answers (2)

Satyajeet Sasmal
Satyajeet Sasmal on 15 Dec 2015
Hi Andy,
Have you tried using the "hist3" and bar3 functions? Please see the sample code below:
vec_x = [1 2 4 5 7 8 9 3 8 7 2]';
vec_y = [1 3 9 5 7 8 1 3 2 9 2]';
vec_bin_edges = 0:9;
hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
mat_joint = hist3([vec_x vec_y], 'Edges', {vec_bin_edges, vec_bin_edges});
bar3(mat_joint, 1);
axis tight
Here are the documentation links for bar3 and hist3 respectively:

A Lotgering
A Lotgering on 16 Dec 2015
Edited: Torsten on 13 Aug 2022
I found the answer:
The following worked for me. 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):
% 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);
% 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')

Sign in to comment.

Community Treasure Hunt

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

Start Hunting!

Translated by