Generate 3D histogram while capping the height of bars

9 vues (au cours des 30 derniers jours)
Ken
Ken le 1 Avr 2024
Commenté : Adam Danz le 10 Avr 2024
I'm generating 3D histograms. It often happens that, given the nature of our data, one bar towers above all the rest. Since the histogram automatically scales to the height of the tallest bar, this makes it hard to see the trends that we would really like to see in the shorter bars. So I would like to generate a histogram (color-coded by height, that's really nice) with the height of individual bars capped. In other words, if I set the cap at 1000 and the computed height of a bar is 4000, it would display with a height of 1000.
It doesn't look like hist3 has a zlimit option. However, this answer contains the following fascinating line of code, a feature I don't see documented in the Matlab documentation, that calculates the matrix of bin values rather than plotting the actual histogram. I have tested, it actually does.
cnt = hist3(X,{bin_x,bin_y});
It would be very easy to apply a floor function to the matrix thus calculated.
If this is documented, could someone please point me to the place? And regardless, can I call hist3 in some other way to plot the contents of this matrix once generated and modified?
  2 commentaires
Dyuman Joshi
Dyuman Joshi le 1 Avr 2024
Modifié(e) : Dyuman Joshi le 1 Avr 2024
That feature is documented in the documentation of hist3 - check the second last syntax.
You could use max() to compare and cap the number accordingly.
Yes, you can call hist3() after manipulating the data, if that is what you meant.
Ken
Ken le 1 Avr 2024
Modifié(e) : Ken le 1 Avr 2024
It looks like I cannot call hist3 on the modified output, because it is not prepared to accept a matrix of bin values, but only a two column matrix of X, Y pairs. I can call bar3, but unfortunately it seems to handle color very differently than hist3 does. 'FaceColor', 'interp' is not accepted, alas!

Connectez-vous pour commenter.

Réponses (2)

Steven Lord
Steven Lord le 1 Avr 2024
Rather than trying to make hist3 or histogram2 truncate the bins, consider using zlim to control the limits of the Z axis.
x = randn(1, 1e5);
y = randn(1, 1e5);
histogram2(x, y, FaceColor = 'flat'); % Note the center bars have height > 400
figure
histogram2(x, y, FaceColor = 'flat')
zlim([0 400]) % Limit the axes to showing only up to 400

Adam Danz
Adam Danz le 1 Avr 2024
Modifié(e) : Adam Danz le 1 Avr 2024
How to cap the height of a 2D histogram without affecting the max color for each bar
This is tricky because hist3 does not return the handle to the surface object it creates. Follow these steps to clip the height of the 3D bars while maintaining the bar color indicating the original heights (if I understood the question correctly).
Note that this produces a misleading plot where bar height and bar color no longer agree which may cause confusion or misinterpretation of the results. Assuming there is good reason to do this, it should be clearly indicated in text that bar height is capped.
% Produce demo data
rng default
x = randn(1000,1);
y = randn(1000,1);
% Plot the 3D historam
figure()
hist3([x,y],'NBins',[5,5],'CDataMode','auto','FaceColor','interp')
colorbar()
Note that the tallest bar has a height of 256. We will cap the height at z=200.
% Define the max height
zClip = 200;
% Get the surface handle - this assumes there is only 1 surface in the axes
ax = gca();
s = findobj(ax,'type','surface');
% Prevent CData from updating when the ZData changes. This will also
% prevent CLim from updating.
s.CDataMode = 'manual';
% Replace ZData larger than zClip
s.ZData = min(s.ZData, zClip);
subtitle(sprintf('Bar height is capped at %.1f',zClip))
Now the 3D histogram is capped at z = 200 while the color data remains at its original values showing a max height of 256.
  2 commentaires
Ken
Ken le 9 Avr 2024
Wow, snagging the surface object out of the axes? Pretty snazzy!
But actually I would like the colors to rescale. I am dealing with a situation in which one tall bar can completely dominate the picture, thereby making almost all other bars almost the same color.
Adam Danz
Adam Danz le 10 Avr 2024
Have you tried setting clim?

Connectez-vous pour commenter.

Catégories

En savoir plus sur Data Distribution Plots dans Help Center et File Exchange

Produits


Version

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by