You are now following this question
- You will see updates in your followed content feed.
- You may receive emails, depending on your communication preferences.
how to make intervals in a bar graph
37 views (last 30 days)
Show older comments
Suppose I have some people and they are of different weight, e.g.
weight in Kq: 40-45 45-50 50-55 55-60 60-65
No. of Persons: 4 12 13 6 5
I want to represent the weight on x-axis and No. of persons on y-axis. This will tell us that there are 4 persons whose weight lies within 40-45 and 12 persons whose weight lies in the interval 45-50 kilo and so on. How can we do that?
Answers (2)
Adam Danz
on 21 Oct 2020
Edited: Adam Danz
on 21 Oct 2020
For continuous intervals
Since your intervals are continuous, you should be using histogram() instead of bar().
kg = 40:5:65;
n = [4 12 13 6 5];
histogram('BinEdges',kg,'BinCounts',n)
xlabel('Weight (kg)')
ylabel('Number of participants')
For discountinuous or categorical intervals
For any future visitors who are not using continuous x-bins and want to label bin edges, follow this demo.
% Create data
bins = [10 20;
50 60;
75 85];
y = [20;10;30];
% Create bar plot
% The .5 specifies bin width, making room for labels
h = bar(y, .5);
% Get bar centers and bar widths
xCnt = h.XData + h.XOffset; % XOffset is undocumented!
width = h.BarWidth;
% Get x-val of bar-edges
barEdgesX = xCnt + width.*[-.5;.5];
% Set new xtick and xticklabels, rotate by 90deg.
ax = h.Parent; % axis handle, if you don't have it already
ax.XTick = barEdgesX(:);
ax.XTickLabel = string(reshape(bins',[],1));
ax.XTickLabelRotation = 90;
2 Comments
the cyclist
on 21 Oct 2020
For "discontinuous" bins, it might be simpler to use your original syntax, but with a zero bin count for empty bins.
For example,
kg = 40:5:75;
n = [4 0 12 13 0 6 5];
figure
histogram('BinEdges',kg,'BinCounts',n)
xlabel('Weight (kg)')
ylabel('Number of participants')
the cyclist
on 21 Oct 2020
Edited: the cyclist
on 21 Oct 2020
Here is one way:
w = 42.5 : 5.0 : 62.5;
n = [4 12 13 6 5];
bar(w,n,'BarWidth',1)
FYI, if you have the underlying individual weight data, rather than the bin counts, you will definitely want to use histogram as in Adam's solution (although the syntax will be different).
23 Comments
Sadiq Akbar
on 22 Oct 2020
Thank you very much to both of you Adam Danz and the cyclist.
Adam Danz I ran your 1st code. It works very well. But when I try it on the following data, it gives me error
I replaced Kg and n vectors by:
Kg=[2.E-03
1.E-03
4.E-04
3.E-04
3.E-04
2.E-04
2.E-04
2.E-04
2.E-04
2.E-04
2.E-04
1.E-04
1.E-04
9.E-05
8.E-05
8.E-05
8.E-05
8.E-05
8.E-05
6.E-05
5.E-05
4.E-05
3.E-05
2.E-05
2.E-05
2.E-05
2.E-05
1.E-05
1.E-05
1.E-05
1.E-05
9.E-06
9.E-06
7.E-06
7.E-06
6.E-06
5.E-06
5.E-06
4.E-06
4.E-06
2.E-06
2.E-06
1.E-06
1.E-06
1.E-06
4.E-07
3.E-07
3.E-07
2.E-07
2.E-07
2.E-07
2.E-07
2.E-07
2.E-07
2.E-07
1.E-07
1.E-07
1.E-07
7.E-08
6.E-08
4.E-08
1.E-08
1.E-08
9.E-09
4.E-09
2.E-09
2.E-10
3.E-11
7.E-12
8.E-13
7.E-13
6.E-13
5.E-13
3.E-13
9.E-14
1.E-14
5.E-15
5.E-17
2.E-22
5.E-23
2.E-23
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
];
n=1:100;
histogram('BinEdges',kg,'BinCounts',n)
xlabel('Weight (kg)')
ylabel('Number of participants')
But it gives me the following error:
>> histogramByAdamDanz
Error using histogram
Expected input number 2, BinEdges, to be non-decreasing valued.
Error in histogram>parseinput (line 267)
validateattributes(value,{'numeric','logical'},{'vector', ...
Error in histogram (line 124)
[opts,passthrough,dispatch] = parseinput(args,firstaxesinput);
Error in histogramByAdamDanz (line 16)
histogram('BinEdges',fitness2sn0,'BinCounts',Runs)
>>
the cyclist
on 22 Oct 2020
Edited: the cyclist
on 22 Oct 2020
As I mentioned, because you have the individual data, not the bin counts, you need a different syntax.
You can just do
figure
histogram(Kg,'BinEdges',0:1e-5:1e-3)
xlabel('Weight (kg)')
ylabel('Number of participants')
Adam Danz
on 22 Oct 2020
Yeah, in my example, N are the counts and KG are the bin edges. The KG data in your comment above do not appear to be bin edges, as the cyclist mentioned. They appear to be the raw data in which case, what are N?
Perhaps you're just looking for,
histogram(kg)
Sadiq Akbar
on 22 Oct 2020
Thank you very much the cyclist. It works now. But I have certain points to ask you:
(1) If I want to change the data, i.e. instead of one Kg, I have three Kg's like Kg1, Kg2 and Kg3, then how will I do this histogram. i.e.
Kg1=[2.E-03
1.E-03
4.E-04
3.E-04
3.E-04
2.E-04
2.E-04
2.E-04
2.E-04
2.E-04
2.E-04
1.E-04
1.E-04
9.E-05
8.E-05
8.E-05
8.E-05
8.E-05
8.E-05
6.E-05
5.E-05
4.E-05
3.E-05
2.E-05
2.E-05
2.E-05
2.E-05
1.E-05
1.E-05
1.E-05
1.E-05
9.E-06
9.E-06
7.E-06
7.E-06
6.E-06
5.E-06
5.E-06
4.E-06
4.E-06
2.E-06
2.E-06
1.E-06
1.E-06
1.E-06
4.E-07
3.E-07
3.E-07
2.E-07
2.E-07
2.E-07
2.E-07
2.E-07
2.E-07
2.E-07
1.E-07
1.E-07
1.E-07
7.E-08
6.E-08
4.E-08
1.E-08
1.E-08
9.E-09
4.E-09
2.E-09
2.E-10
3.E-11
7.E-12
8.E-13
7.E-13
6.E-13
5.E-13
3.E-13
9.E-14
1.E-14
5.E-15
5.E-17
2.E-22
5.E-23
2.E-23
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
0.E+00
];
Kg2=[2.E-23
9.E-05
3.E-10
9.E-05
2.E-03
4.E-03
1.E-02
8.E-20
2.E-02
2.E-03
2.E-03
2.E-01
2.E-09
1.E-19
3.E-14
1.E+00
3.E-03
4.E-03
5.E-07
6.E-25
3.E-25
8.E-05
1.E-21
5.E-05
2.E-16
3.E-03
2.E-05
2.E-21
1.E-03
7.E-19
2.E-04
6.E-05
0.E+00
6.E-03
2.E-04
1.E-04
2.E-03
2.E-04
2.E-02
9.E-04
7.E-23
1.E+00
1.E-10
2.E-03
2.E-17
4.E-03
4.E-21
7.E-04
0.E+00
1.E-08
2.E-03
5.E-17
2.E-05
4.E-09
2.E-02
9.E-04
1.E-03
2.E-06
6.E-02
3.E-07
1.E-05
3.E-08
2.E-03
3.E-07
2.E-05
1.E-26
2.E-08
1.E-09
1.E-01
0.E+00
3.E-03
2.E-19
5.E-03
1.E-20
4.E-04
9.E-04
2.E-22
0.E+00
7.E-23
2.E-23
2.E-17
2.E-02
9.E-03
4.E-04
7.E-04
1.E-04
6.E-05
3.E-02
2.E-02
1.E-02
2.E-19
5.E-04
6.E-03
3.E-04
7.E-02
2.E-28
3.E-04
3.E-04
2.E-04
];
Kg3=[4.E-02
8.E-02
1.E-01
2.E-02
2.E-05
8.E-03
8.E-02
2.E-01
2.E-01
4.E-02
6.E-05
2.E-06
2.E-02
7.E-02
7.E-03
1.E-02
1.E-03
1.E-01
2.E-02
9.E-01
1.E-02
1.E-07
6.E-02
1.E-01
8.E-02
3.E-02
6.E-02
2.E-01
2.E-01
5.E-02
7.E-04
2.E-03
5.E-02
5.E-02
3.E-02
9.E-03
3.E-03
9.E-01
1.E-03
4.E-02
1.E-03
1.E-01
4.E-03
4.E-01
2.E-01
7.E-02
4.E-04
9.E-02
2.E-01
5.E-02
4.E-02
1.E-02
2.E-01
1.E-02
1.E-05
1.E-02
4.E-02
2.E-01
3.E-02
8.E-02
9.E-01
7.E-03
1.E-05
2.E-03
5.E-02
2.E-03
9.E-02
3.E-01
1.E-02
3.E-02
6.E-02
1.E-03
6.E-02
9.E-02
1.E-02
1.E-01
6.E-02
2.E-02
1.E-01
2.E-01
2.E-01
3.E-01
9.E-02
3.E-03
5.E-05
2.E-02
1.E-01
2.E-04
1.E-01
3.E-02
2.E-01
1.E-01
1.E-02
4.E-02
3.E-02
6.E-03
3.E-02
8.E-02
8.E-02
]
(2) 2ndly if I want to give colours to each bar, how to do that?
(3) If I want to plot the three Kgs data in one graph, then (a) can we make all three Kgs as one Kg and plot it like above? If yes then how will we decide the range as you did inside histogeram command which is 0:1e-5:1e-3
(b)If I plot one histogram for Kg1, then I want to plot Kg2 and Kg3 histogram over the 1st, then how will we do that?
(c) I want to show x-axis in log scale
Thanks once again that you listened to me.
Sadiq Akbar
on 22 Oct 2020
Thanks Adam Danz for your response. You mean to say that when data is in raw form, then we don't need N?
Adam Danz
on 22 Oct 2020
The histogram function does two main things.
- counts the number of data points within each bin
- plots the results
The first method in my answer uses,
histogram('BinEdges',kg,'BinCounts',n)
which bypasses the first step and the user supplies the bin-counts (and bin edges). In that case, the histogram function is only plotting the results.
So yes, when your supplying raw data, the histogram function does the bin-counting for you.
Sadiq Akbar
on 22 Oct 2020
Thank you very much dear Adam Danz. What do you say about the points that I have asked the cyclist in the above comments as the cyclist seems busy.
the cyclist
on 22 Oct 2020
Sadiq, could I suggest that you make a careful reading of the documentation for the histogram function? It explains how to do nearly everything you have asked about.
Sadiq Akbar
on 22 Oct 2020
Thank you very much dear the Cyclist. I will study it but as I am not too much technical, so will ask you when I feel some difficulty in that doc. Thanks to both of you again.
Sadiq Akbar
on 22 Oct 2020
Dear the cyclist , I read the doc. you provided. I saw the option of "Plot Multiple Histograms". When I ran their given program, it is well displayed. But when I put my data there, its very strange. The horizontal and vertical numbers in mine are in points but in their graph, these are integer values. Further, I don't understan it what to do now?
Sadiq Akbar
on 22 Oct 2020
Thank you very much dear Adam Danz. Yes, I ran it again and here is the plot in the attachment, but this time only the horizontal axis is in floating point number.
Adam Danz
on 22 Oct 2020
That's partially helpful. What we're still missing is,
- The data used to generate the histogram
- The syntax you used (the line that contains the histogram() function)
- And what the x-axis should represent (I believe it's weight in kg but I don't know why the xlim is 0-1).
Sadiq Akbar
on 22 Oct 2020
sorry for that. Ok I I am attaching the three mat files and write my code here.
% Multiple Histogram of my data
%1st keep the attached mat files 2sn0_sorted, 3sn0_sorted and 4sn0_sorted in the
%same directory as this m file, then run it and observe my plots. If you
%see, the markings on horizontal axis of the plots, it is in floating point
%numbers while in theirs, its integers.
load 2sn0_sorted
fitness2sn0=one;
% Runs=tt;
load 3sn0_sorted
fitness3sn0=one;
load 4sn0_sorted
fitness4sn0=one;
h1 = histogram(fitness2sn0);
hold on
h2 = histogram(fitness3sn0);
h3 = histogram(fitness4sn0);
Sadiq Akbar
on 22 Oct 2020
Thank you very much for your devoted help. I have run a meta-heuristic algorithm that sends random vectors to my fitness function and my fitness function compares those vectors one by one with my desired vector. When the vector within those vectors is nearly same to my desired vector, then my fitness function returns these values. If the vector is not matching, then the fitness returns large values. So these small values mean that the estimated vectors are nearly matching my desried vector. Now I want to see that how many values of those fitness are there within an interval. I am attaching the graph that I want to get because this is the histogram that has been obtained for similar data.
Sadiq Akbar
on 22 Oct 2020
Thank you very much for your prompt response. If you load the mat file one in one time and click on the variable "one" in workspace, you can see all the fitness values in that data set. From there you can see it very easily.
Sadiq Akbar
on 22 Oct 2020
If you see the graph that I have given in the attachment(histogram.fig), that graph has been obtained for similar values. Then why mine is not like that? or how can I get like that?
Adam Danz
on 22 Oct 2020
If you plot the data from fitness2sn0.mat (variable 'one'), you'll see that is spans from x=0 to x=0.0022349. That's closer to the range of data in your histogram.jpg image (though it has a different distribution).
"Then why mine is not like that? or how can I get like that?"
I'm not sure if that's the right question to be asking.
The way I see it, the question isn't how to make your data look like that plot. The questions could be,
- (not really a question) Perhaps I am plotting it corretly and my data have a different result.
- Am I using at the right data in the first place? Should I be binning and counting this variable?
- I am using the correct data but the outcome is unexpected. Is that because of an error in the analysis, an error when importing the data, or is it a true reflection of whatever I was measuring?
We're at a crossroads here where no amount of technical help is going to answer these questions (without more background informat). You as the owner of the data and the person analyzing it needs to take a step back think about it from big-picture perspective.
I'll point out that the values in your original question (kilograms etc) don't seem to be relevant to the jpg image or the mat data you shared so I sense some disorientation here.
Sadiq Akbar
on 22 Oct 2020
Thank you very much dear Adam Danz.Yes you are right that my 1st data of Kg was different than this one. Its becuase I posted this data with the question that what will be the histogram/bar plot for this, but no one did an attempt on that. Then I thought may be I am not able to make some one understand my problem. So I changed my question in the type which is understandable to all and therefore, I posted here as KG and n. And you and the cyclist attempted it.
Sadiq Akbar
on 23 Oct 2020
I found one such code on this site today. The code is as follows:
[~,edges] = histcounts(log10(x));
histogram(x,10.^edges)
set(gca, 'xscale','log')
In this code, when I replace x by my data, then it plots well. Now I want to insert a preview in this graph. How can I insert the same graph as a preview pane in the same figure window as is in the attachment figure.
See Also
Categories
Find more on Discrete Data Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!An Error Occurred
Unable to complete the action because of changes made to the page. Reload the page to see its updated state.
Select a Web Site
Choose a web site to get translated content where available and see local events and offers. Based on your location, we recommend that you select: .
You can also select a web site from the following list
How to Get Best Site Performance
Select the China site (in Chinese or English) for best site performance. Other MathWorks country sites are not optimized for visits from your location.
Americas
- América Latina (Español)
- Canada (English)
- United States (English)
Europe
- Belgium (English)
- Denmark (English)
- Deutschland (Deutsch)
- España (Español)
- Finland (English)
- France (Français)
- Ireland (English)
- Italia (Italiano)
- Luxembourg (English)
- Netherlands (English)
- Norway (English)
- Österreich (Deutsch)
- Portugal (English)
- Sweden (English)
- Switzerland
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)