How do I create a 3D histogram for; x = 1*26, y=1*28, z = 26*28 ?
Mostra commenti meno recenti
I would basically like to bin the the 'z' data into ranges of x and y such as the attached picture:

2 Commenti
Joachim Schlosser
il 21 Mar 2016
Please attach some sample data and the code you have tried so far.
Aaron Rampersad
il 22 Mar 2016
Risposte (3)
Walter Roberson
il 21 Mar 2016
0 voti
There are also a lot of histogram programs in the File Exchange.
2 Commenti
Aaron Rampersad
il 21 Mar 2016
Walter Roberson
il 21 Mar 2016
Do you need zooming and data tips? If not then see http://www.mathworks.com/matlabcentral/fileexchange/35274-matlab-plot-gallery-bar-graph-3d
If you do, then see for example http://www.mathworks.com/matlabcentral/fileexchange/45916-3d-colored-bar-plot
Image Analyst
il 22 Mar 2016
Aaron, give the x and y data as an N by 2 array, then pass in the number of bins in each direction into the poorly named hist3() to create your 2D histogram
counts = hist3([x, y], [26, 28]);
8 Commenti
Aaron Rampersad
il 22 Mar 2016
Image Analyst
il 22 Mar 2016
Z is the histogram itself. In other words, Z is the counts, or the number of times you have a value within a given range of both X and Y. For example if you had 20 values between 2 and 3 for x, and between 6 and 7 for Y, then z (the counts) would be 20 and the bar at that x,y location would be 20 units high.
Aaron Rampersad
il 22 Mar 2016
Image Analyst
il 22 Mar 2016
I don't understand. What is "your" z data? The z data in the histogram is the counts. Is there any other z data? Not that I'm aware of.
Aaron Rampersad
il 22 Mar 2016
Image Analyst
il 22 Mar 2016
Modificato: Image Analyst
il 22 Mar 2016
OK, "Contribution to Hazard" is a count. The count you get from a certain "Source to Site Distance" range, and from a certain "Magnitude" range. So like I said, you can pass "Source to Site Distance" and "Magnitude" into hist3() and get out the counts ("Contribution to Hazard") for those observations that fall into both ranges. The counts will be your "z" values and is represented by the heights of the bars.
I don't know what you mean by clusters. I see no clusters, or many just one big one that is essentially the whole hump. (Did you perhaps mean clutter instead?) If you want fewer bins (vertical columns/bars), then just pass in the number of bins in each direction you want to hist3() and it will make a histogram with that many bars. You can also change the bar width to make it more solid and without empty "air space" between the bars, if you want.
I see from Mike's answer that they now have a histogram2() which seems like it does what hist3() did but with a much more appropriate and descriptive name.
Aaron Rampersad
il 26 Mar 2016
Modificato: Aaron Rampersad
il 26 Mar 2016
Image Analyst
il 26 Mar 2016
Well now you'r totally confusing everybody. We though that the x and y were basically ranges that your data could take. So one data point had both an x coordinate, and a y coordinate. And you wanted 28 bins along the y direction, and 26 bins along the x direction. And then "z" would be the counts of how many data points fell into a certain x,y bins. You don't have any z data yet, before taking the histogram, do you? It's to be computed and is the "count" data. If that's not right, then no one has read your mind correctly, and you'll need to explain better and attach your x, y, and z arrays.
Mike Garrity
il 22 Mar 2016
histogram2(10+5*randn(1e4,1),3*randn(1e4,1),'FaceColor','flat')

13 Commenti
Aaron Rampersad
il 22 Mar 2016
Mike Garrity
il 22 Mar 2016
I'm not following. You've already done the binning and you just want to draw the bars?
Aaron Rampersad
il 26 Mar 2016
Walter Roberson
il 26 Mar 2016
Does z(J,K) correspond to x(J) and y(K) ? If so then you do not need to do anything special. At most you might need to use
[X,Y] = ndgrid(x,y);
and then refer to X and Y in the drawing routines instead of x and y.
CSCh
il 5 Apr 2023
Sorry to open this question again.
I have exactly the same issue. I have 100 Matrices, all of size of M= (20x100), containing messurement values, corresponding to fix spatio coordinates Vektor X (1x20) and vector Y (1x100). I would like to have a 3D Histogram, to see how the Measurements values are distributet with respect to X and Y. From all the answers, I'am not sure how to create such a plot.
Thanks,
Chris
Image Analyst
il 5 Apr 2023
Chris, start your own question and attach your data in a .mat file with the paperclip icon. Essentially you can loop over your matrices, or if, very unfortunately, your matrices all have different names, you'd have to have 100 calls to histcounts.
CSCh
il 6 Apr 2023
Hello Image Analyst, I thought it would be better to finish a thread than to start (again) a new one. There are already some open asking for a 3D Histogram plot, but from my perspective there is no clear answer. Then leave my example aside, it would already help me if Aaron's question could be answered.
So as an example
X=[1:26],
y=[1:28], and
Z = randi([0, 2], [26,28]);
How to make a 3D Histogram of Z in respect to X and Y?
Thank you, Chris
Image Analyst
il 6 Apr 2023
@CSCh Not sure what you mean. What are X and y? Z is a matrix, essentially an image. Not sure what the histogram of an image with respect to X and y actually means. Explain better with an example. Did you try histogram, histcounts, and imhist?
For exampleI, Z is the air pollution measurements which were measured at different points in the space (xy) e.g. over a rectengular area. If you now want to know, where (xy) and how often a value has been measured, you need a 3D histogram, which shows the value distribution (Z) in dependence of the space coordinates (xy). (For my case I have several "copies" of Z,)
I tried histogram and histcounts, but you need always vectors with equal length. In my or Aaron example we have not a squared Matrix thats makes it special and not easily applicable to the standard function.
I hope this explanation helps
Chris
Image Analyst
il 6 Apr 2023
Spostato: Rik
il 14 Apr 2023
You can fill out a rectangular matrix. See my attached demo of scatteredInterpolant.
Image Analyst
il 11 Apr 2023
Spostato: Rik
il 14 Apr 2023
You don't need the toolbox to run scatteredInterpolant. You can run it on just a regular 2-D matrix. Here is the main part:
%================================ MAIN PART RIGHT HERE ==============================================
% Create the scattered interpolant. x, y, and gray levels must be column vectors so use (:) to make them column vectors.
% And grayLevels must be double or else an error gets thrown.
F = scatteredInterpolant(xAll(:), yAll(:), double(grayLevelsAll(:)))
% The above line creates an interpolant that fits a surface of the form v = F(x,y).
% Vectors x and y specify the (x,y) coordinates of the sample points.
% v is a vector that contains the sample values associated with the points (x,y).
% Get a grid of points at every pixel location in the RGB image.
[xGrid, yGrid] = meshgrid(1:columns, 1:rows);
xq = xGrid(:);
yq = yGrid(:);
% Evaluate the interpolant at query locations (xq,yq).
vq = F(xq, yq);
fittedImage = reshape(vq, rows, columns);
%================================ END OF MAIN PART ==============================================
grayLevelsAll is the known values of the few locations that you know the values of.
xAll and yAll are your known coordinate values of the locations.
rows and columns are the size of the matrix you want to fill out.
fittedImage is the 2-D array - you don't have to consider it as an image if you don't want to.
Categorie
Scopri di più su Data Distribution Plots in Centro assistenza e File Exchange
Prodotti
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!

