Hello, I`m plotting histograms and would like stack 2 data sets instead of having them overlap. The problem is i didn`t find any options for histograms to do that in Matlab. It seems you need to use bar, but then i get the issue of the xaxis ticks and labels. Does anyone know how to properly plot stacked histograms?
For bar plots, how do we set the xaxis ticks and labels? (i tried set(gca....) and d.Xtick...and it didn`t work.
thanks

3 Kommentare

dpb
dpb am 23 Mai 2018
What, specifically, do you mean by "stacked histogram"? Can you show an image of what you think should look like or point to a link of a sample?
Is the stacked bar graph what you're looking for?
%Example from MathWorks website:
y = [2 2 3; 2 5 6; 2 8 9; 2 11 12];
bar(y,'stacked')
Yasmin Samy
Yasmin Samy am 23 Mai 2018
yes that's what i mean. similar to the stacked bar plots. but i wanted to do it using histogram function where you have the edges on the xaxis. The bar plot just puts a count from 1 to N on the xaxis and i can't seem to replace that.

Melden Sie sich an, um zu kommentieren.

 Akzeptierte Antwort

Benjamin Kraus
Benjamin Kraus am 23 Mai 2018

0 Stimmen

In order to use the bar command to plot a histogram, you need to specify the X-values when you call the bar command. For example:
[N1,edges] = histcounts(randn(1000,1));
N2 = histcounts(randn(1000,1),edges); % Bin using the same edges
ctrs = (edges(1:end-1)+edges(2:end))/2; % Calculate the bin centers
bar(ctrs, [N1' N2'],1,'stacked','FaceAlpha',0.6);
Once that is done, you can either set the ticks to auto, or to the specific bin edges:
xticks auto % Use automatic ticks
xticks(edges) % Use specific edges

6 Kommentare

Yasmin Samy
Yasmin Samy am 23 Mai 2018
Bearbeitet: dpb am 23 Mai 2018
Hello Benjamin! Thanks for your reply. Still not working tho :(
Whats the difference between the syntax of these two lines?
[N1,edges] = histcounts(randn(1000,1));
N2 = histcounts(randn(1000,1),edges); % Bin using the same edges
also xticks isn't working, i get the following error message: Undefined function or variable 'xticks'.
Below is plotting part of my code:
%total mode
edge_tl=[-3:0.05:0.5];
histo_coarse=histcounts(tc_bgd(:,3),edges_c);
hold on
histo_sp=histcounts(Apr_May(:,3),edges_c);
histo_coarse=histo_coarse.';
histo_sp=histo_sp.';
b=bar(edges_c,[histo_coarse histo_sp],'Stacked','FaceAlpha',0.5);
set(b,{'FaceColor'},{[0 0.4470 0.7410];'b'});
Yasmin Samy
Yasmin Samy am 23 Mai 2018
im using Matlab R2016a
dpb
dpb am 23 Mai 2018
Bearbeitet: dpb am 24 Mai 2018
Z=randn(1000,1);
[N1,edges] = histcounts(Z);
N2 = histcounts(Z,edges);
>> all(N1==N2)
ans =
logical
1
>>
None if you use the same input data; of course, the first returns edges vector if you don't already have one so that's a basic difference. Depends on whether you can accept the default binning or want to control it yourself.
ADDENDUM The reason it histcounts was used that way in the Answer is that the Q? was to plot a stacked histogram; the first call creates the first distribution; the second creates the second one to pile on top of the first with different data (new call to randn does that) but so the two will line up for subsequent plot(), make sure the binning is the same between the two.
As for xticks, can't tell, you probably did something inadvertent to alias it or change path or somesuch
Try
clear xticks
which xticks
and report back.
Yasmin Samy
Yasmin Samy am 24 Mai 2018
It seems in matlab R2016a xticks isn't used like this. Usually its axis.XTick= or set(gca,'Xtick') but for some reason im still not getting the results i want.
regarding this line: bar(ctrs, [N1' N2'],1,'stacked','FaceAlpha',0.6); what is the "1" before stacked referring to?
dpb
dpb am 24 Mai 2018
Bearbeitet: dpb am 24 Mai 2018
Indeed xticks was introduced in 16b
>> help bar
bar Bar graph.
...
bar(X,Y,WIDTH) or bar(Y,WIDTH) specifies the width of the bars. Values
of WIDTH > 1, produce overlapped bars. The default value is WIDTH=0.8
...
The doc version uses
...
bar(_____,width)
...
which leaves more to imagination, but the underscore implies the all previous explicit arguments prior to that; iow x,y
Show us what you got; explain specifically what isn't what you want and what that is...
ADDENDUM
Taking a guess, try
hAx=gca; % get handle
xlim([-4.5 4.5]) % symmetrize range
hAx.XAxis.TickLabelFormat='%0.1f'; % consistent format
hAx.XAxis.FontSize=9; % get a little more room
hAx.XTick=edges; % put ticks at bin edges
Probably still won't be sufficient room unless spread figure out wider to clearly read the tick values; you'll have to decide what's more important to show...
Yasmin Samy
Yasmin Samy am 24 Mai 2018
Thanks for your help so far.
So just to recap, i tried the following code templates and they successfully worked this time!
ctrs = (edges(1:end-1)+edges(2:end))/2; % Calculate the bin centers bar(ctrs, [N1' N2'],1,'stacked','FaceAlpha',0.6);
suggested by Benjamin at first, and it worked well so far. I removed the xlim and Xlabel lines where i was trying to adjust my xaxis but they were just messing it up more!
Also i had a log scale, with which using xlim helped solve the issue of an axis that was repeating itself(0.001 0.01 0.1 1 0.001 0.01 0.1 1).
Thanks for your patience and support!!

Melden Sie sich an, um zu kommentieren.

Weitere Antworten (0)

Kategorien

Mehr zu Data Distribution Plots finden Sie in Hilfe-Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by