how to make intervals in a bar graph
36 次查看(过去 30 天)
显示 更早的评论
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?
回答(2 个)
Adam Danz
2020-10-21
编辑:Adam Danz
2020-10-21
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 个评论
the cyclist
2020-10-21
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')
Adam Danz
2020-10-21
Oh, that's interesting!
I just tested it with NaNs as spacers too but histogram throws an error that N must be finite.
the cyclist
2020-10-21
编辑:the cyclist
2020-10-21
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 个评论
Sadiq Akbar
2020-10-22
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
2020-10-22
编辑:the cyclist
2020-10-22
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
2020-10-22
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
2020-10-22
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
2020-10-22
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
2020-10-22
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
2020-10-22
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
2020-10-22
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
2020-10-22
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
2020-10-22
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?
Adam Danz
2020-10-22
Could you show us your histogram and how you created it?
What do you mean by " The horizontal and vertical numbers in mine are in points"? If you want integer bins, you can specify the bin edges.
Sadiq Akbar
2020-10-22
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
2020-10-22
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
2020-10-22
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
2020-10-22
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.
Adam Danz
2020-10-22
". Now I want to see that how many values of those fitness are there within an interval."
That's what the y-axis shows.
Sadiq Akbar
2020-10-22
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.
Adam Danz
2020-10-22
Yes, those are the values that define the range of your x-axis in the histogram plots and they are all between 0 and 1 - some files have a smaller portion of that range.
Sadiq Akbar
2020-10-22
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
2020-10-22
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
2020-10-22
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
2020-10-23
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.
另请参阅
类别
在 Help Center 和 File Exchange 中查找有关 Discrete Data Plots 的更多信息
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!发生错误
由于页面发生更改,无法完成操作。请重新加载页面以查看其更新后的状态。
您也可以从以下列表中选择网站:
如何获得最佳网站性能
选择中国网站(中文或英文)以获得最佳网站性能。其他 MathWorks 国家/地区网站并未针对您所在位置的访问进行优化。
美洲
- América Latina (Español)
- Canada (English)
- United States (English)
欧洲
- 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)
亚太
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)