3D Histogram of 50x50 data

9 views (last 30 days)
AS
AS on 3 Jun 2023
Answered: Diwakar Diwakar on 4 Jun 2023
Hello, I am looking to plot a 3D histogram (like the one in the attached snippet) in MATLAB. I have x and y axes which are 50 bins each, one is ranging from -4000 and 4000 and the other one from 0 and 8000. I just need to plot the binned values (as z) which is 50x50.
Thanks in advance.

Accepted Answer

the cyclist
the cyclist on 3 Jun 2023
You can make this style of plot using the histogram2 function.
  3 Comments
the cyclist
the cyclist on 3 Jun 2023
% Read the data from file
xyz = readmatrix("Histogram2_sample.xlsx");
% Get the x, y, and z values
x = xyz(2:end,1);
y = xyz(1,2:end)'; % Transpose to get a column vector
z = xyz(2:end,2:end);
% Flip x and z, because histogram algorithm requires increasing values
x = flipud(x);
z = flipud(z);
% Plot
figure
histogram2('XBinEdges',[x; x(end)+160],'YBinEdges',[y; y(end)+160],'BinCounts',z)
% Change the view to make the perspective similar to posted image
set(gca,"View",[145 30])
AS
AS on 3 Jun 2023
awesome, thank you very much!!

Sign in to comment.

More Answers (1)

Diwakar Diwakar
Diwakar Diwakar on 4 Jun 2023
This code may help you:
% Generate some random data
data = randn(1000, 2) .* [4000, 8000] + [0, 4000];
% Define the bin edges
x_edges = linspace(-4000, 4000, 51);
y_edges = linspace(0, 8000, 51);
% Compute the histogram
histogram2D = hist3(data, 'Edges', {x_edges, y_edges});
% Plot the 3D histogram
figure;
bar3(histogram2D);
xlabel('X');
ylabel('Y');
zlabel('Count');
title('3D Histogram');
% Customize the plot appearance
colormap jet; % Change the color map if desired
colorbar; % Add a color bar

Tags

Products


Release

R2022b

Community Treasure Hunt

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

Start Hunting!