Clear Filters
Clear Filters

Displaying Histograms with decreasing bar heigths

2 views (last 30 days)
Hello,
My data: is a 1000x1 duration array, which I'd like to display within histograms ... with decreasing bar heights.
What I did so far is following:
  • I defined my categories
  • I created individual histograms
  • And displayed those within one figure
%Defining the time periods I'd like to visualize
a=({'03:00:00'}>data_inm);
b={'06:00:00'}>data_inm;
b1=b-a;
c={'12:00:00'}>data_inm;
c1=c-b;
C=categorical(a,[1],{'0-3'});
C1=categorical(b1,[1],{'3-6'});
C2=categorical(c1,[1],{'6-12'});
%Visualization
figure('Name','My Categories');
hold;
histogram(C,'Normalization','probability','BarWidth',0.5);
histogram(C1,'Normalization','probability','BarWidth',0.5);
histogram(C2,'Normalization','probability','BarWidth',0.5);
Besides having more than 3 categories, I guess the way I tried to solve my task so far is not too effective.
... do you have an idea on how to display my histograms with decreasing bar heights? Or on how I could realize a more effective code? I'd be glad to hear your ideas!
Best regards! And thanks a lot in advance!

Accepted Answer

Steven Lord
Steven Lord on 17 Jul 2018
It's not clear to me what you mean when you say "with decreasing bar heights".
Do you want the total area of the bin to be proportional to the number of elements in the bin, not just the height, so a wider bin is shorter than a narrow bin with the same number of elements? If so, you probably want to specify the 'countdensity' or 'pdf' values for the 'Normalization' parameter when you create your histogram.
x = randn(1, 1e4);
E = [-3 -2 -1 -0.5 -0.25 0 0.25 0.5 1 2 3];
figure
histogram(x, E);
figure
histogram(x, E, 'Normalization', 'countdensity');
figure
histogram(x, E, 'Normalization', 'pdf');
Do you mean that you want the bar corresponding to a bin with 2 elements to be less than one unit taller than a bin with 1 element?
figure
h = histogram(x);
ax = ancestor(h, 'axes');
% Compare the appearance of the histogram before and after this line
ax.YScale = 'log';
If neither of those are what you want, please explain in more detail. If there's a webpage that shows specifically what you're trying to achieve, a link to that page may be useful in helping us understand your goal.
  2 Comments
jrbbrt
jrbbrt on 18 Jul 2018
Edited: jrbbrt on 18 Jul 2018
Hi Steven :)
So how could I explain what my goal is in different words ... basically I got 10 separate "histogram bars" I'd like to visualize in one figure. My problem is, that those bars with different heights (which are matching their number of elements) are totally out of order. Means, though my code says "Show me C, then C1 and afterwards C2, C3, C4, C5, ...", they're totally mixed up in their visualization order.
  • For example, instead of C which I wanted to be representing the first "histogram bar" on the very left in my plot, there suddenly was C2. So the order was: "C2, C5, C, C4, C1, C3" instead of "C, C1, C2, C3, C4, C5" Actually all I want is Matlab to visualize my histogram bars in the order I also wrote down the code.
  • Besides this I'm wondering if it's possible to order my histogram bars the way, that the one with most of the elements in it, is on the very left side, and then decreasing with the number of elements, there appear the others. Like a decreasing graph.
I'm sorry I wasn't clear in my question at the very beginning. I guess I first had to figure out, that I actually had 2 things to ask in my mind ... anyway, do you think, it would be easier to just realize my goal with the help of the bar function?
Steven Lord
Steven Lord on 18 Jul 2018
Ah, okay, you want the bins in descending order of height. That's easy. Set the DisplayOrder property of the categorical histogram to 'descend'. The "Change Display Order" section on this documentation page shows how to do this.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

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

Start Hunting!