Plotting histogram with increments
이 질문을 팔로우합니다.
- 팔로우하는 게시물 피드에서 업데이트를 확인할 수 있습니다.
- 정보 수신 기본 설정에 따라 이메일을 받을 수 있습니다.
오류 발생
페이지가 변경되었기 때문에 동작을 완료할 수 없습니다. 업데이트된 상태를 보려면 페이지를 다시 불러오십시오.
이전 댓글 표시
0 개 추천
I want to plot histogram. My data consists of (say 1000 numbers) from -12 to 12 and I want to plot a histogram of 26 bins from -12 to -11.9 then from -11.9 to -11, -11 to -10 and till 10 to 11 and 11 to 11.9 and 11.9 to 12. How do i do this?
Please give me some logic to do this?
댓글 수: 1
My X is an array with 110000 numbers from -12 to 12. And I want to plot histogram like this. Can you help me to put a simple logic for this as the code is so long writing like this.
h1 = histogram(x(x>=-12 & x <=-11.9));
hold on;
h1 = histogram(x(x>=-11.9 &x <=-11));
hold on;
h1 = histogram(x(x>=-11 & x <=-10));
hold on;
h1 = histogram(x(x>=-10 & xx <=-9));
hold on;
h1 = histogram(x(x>=-9 & x <=-8));
hold on;
h1 = histogram(x(x>=-8 & x <=-7));
hold on;
h1 = histogram(x(x>=-7& x <=-6));
hold on;
h1 = histogram(x(x>=-6 & x <=-5));
hold on;
h1 = histogram(x(x>=-5&x <=-4));
hold on;
h1 = histogram(x(x>=-4 & x <=-3));
hold on;
h1 = histogram(x(x>=-3 & x <=-2));
hold on;
h1 = histogram(x(x>=-2 & x <=-1));
hold on;
h1 = histogram(x(x>=-1 & x <=0));
hold on;
h1 = histogram(x(x>=0 & x <=1));
hold on;
h1 = histogram(x(x>=1& x <=2));
hold on;
h1 = histogram(x(x>=2 &x <=3));
hold on;
h1 = histogram(x(x>=3 &x <=4));
hold on;
h1 = histogram(x(x>=4& x <=5));
hold on;
h1 = histogram(gx(gx>=5 & x <=6));
hold on;
h1 = histogram(x(x>=6 & gx <=7));
hold on;
h1 = histogram(x(x>=7& x <=8));
hold on;
h1 = histogram(x(x>=9 & x <=10));
hold on;
h1 = histogram(x(x>=10 & x <=11));
hold on;
h1 = histogram(x(x>=11& x <=11.9));
hold on;
h1 = histogram(x(x>=11.9 & x <=12));
hold off;
axis([xmin xmax]);
legend({'-S','-11','-10,'-9','-8','-7','-6','-5','-4','-3','-2','-1','0','1','2','3','4','5','6','7','8','9','10','11','S'});
And my axis doesn't work. And y axis must be in percentage.
채택된 답변
Star Strider
2019년 2월 4일
편집: Star Strider
2019년 2월 4일
I am not certain what you are asking. You can easily specify the bin edges in the histogram (link) function.
From the documentation:
‘histogram(X,edges) sorts X into bins with the bin edges specified by the vector, edges. Each bin includes the left edge, but does not include the right edge, except for the last bin which includes both edges.’
EDIT —
Responding tto the Comment you posted below your Question, this should do what you want:
X = 24*rand(1,110000)-12; % Create Data
BinEdges = -12 : 12;
h = histogram(X, BinEdges, 'Normalization','probability');
XTL = {'-S','-11','-10','-9','-8','-7','-6','-5','-4','-3','-2','-1','0','1','2','3','4','5','6','7','8','9','10','11','S'};
set(gca, 'XTick',BinEdges', 'XTickLabel', XTL)
xlabel('Bin Ranges')
ylabel('Probability')
댓글 수: 38
Hi, Can i convert y axis to percentage instead of probability?
Star Strider
2019년 2월 4일
My impression is that ‘percentage’ is ‘probability’. For a full list of the options, see the documentation section on 'Normalization' (link).
Star Strider
2019년 2월 4일
Shambhavi Adhikari’s ‘Answer’ moved here:
how to put values from y-axis in legend something like this?
-12 to -11 = 10%
-11 to -9 = 8%
Star Strider
2019년 2월 4일
There is no need for a legend, since you aparently have only one group of data.
There are ways to put the value of each bar on top of the bar.
What do you want to do?
I want to put values on the top of the bar. The probability or percentage of each values, like this:
-12 to -11 = 10%
-11 to -9 = 8%
With that addition, the full code is now:
X = 24*rand(1,110000)-12; % Create Data
BinEdges = -12 : 12;
h = histogram(X, BinEdges, 'Normalization','probability');
XTL = {'-S','-11','-10','-9','-8','-7','-6','-5','-4','-3','-2','-1','0','1','2','3','4','5','6','7','8','9','10','11','S'};
set(gca, 'XTick',BinEdges', 'XTickLabel', XTL)
xlabel('Bin Ranges')
ylabel('Probability')
BinCenters = BinEdges(1:end-1)+0.5;
BinValues = h.Values;
text(BinCenters, BinValues, compose('%2.0f%%',BinValues*100), 'HorizontalAlignment','center', 'VerticalAlignment','bottom', 'FontSize',7) % Choose One
text(BinCenters, BinValues, sprintfc('%2.0f%%',BinValues*100), 'HorizontalAlignment','center', 'VerticalAlignment','bottom', 'FontSize',7) % Choose One
That should do what you want.
Shambhavi Adhikari’s ‘Answer’ moved here:
Hi, Thank you for your help. I want to have decimal point up to 3 digits or 5 digit on the percentage. How do i do that? Do i need to change %2.0f%% something different? And how to multiply the points on the y-axis into 100 and have 45 instead of 0.45?
text(BinCenters, BinValues, sprintfc('%2.0f%%',BinValues*100), 'HorizontalAlignment','center', 'VerticalAlignment','bottom', 'FontSize',7)
My pleasure.
With those changes, my complete code is now:
X = 24*rand(1,110000)-12; % Create Data
BinEdges = -12 : 12;
h = histogram(X, BinEdges, 'Normalization','probability');
XTL = {'-S','-11','-10','-9','-8','-7','-6','-5','-4','-3','-2','-1','0','1','2','3','4','5','6','7','8','9','10','11','S'};
yt = get(gca,'YTick');
set(gca, 'XTick',BinEdges', 'XTickLabel', XTL, 'YTick',yt, 'YTickLabel',yt*100)
xlabel('Bin Ranges')
ylabel('Probability (%)')
BinCenters = BinEdges(1:end-1)+0.5;
BinValues = h.Values;
text(BinCenters, BinValues, sprintfc('%6.3f%%',BinValues*100), 'HorizontalAlignment','center', 'VerticalAlignment','bottom', 'FontSize',4)
You may want to use this for the bar top labels instead:
text(BinCenters, BinValues, sprintfc('%6.3f%%',BinValues*100), 'HorizontalAlignment','left', 'VerticalAlignment','bottom', 'FontSize',6, 'Rotation',60)
Star Strider
2019년 2월 5일
Shambhavi Adhikari’s ‘Answer’ moved here:
Thank you for your help. I am trying to run this: I don't know why but it gives me an empty array.
y= x(x<=-11.9 & x>=11.9); I am trying to find elements less than -11.9 and greater than 11.9
Star Strider
2019년 2월 5일
‘I am trying to find elements less than -11.9 and greater than 11.9 ’
That is exactly the opposite of what you asked for in your original Question, where you stated that you want to calculate the histogram of your data from -12 to +12 in increments of 1.
If your data only has values from -12 to +12, you should expect an empty result if you search for values outside those limits.
I will delete my Answer in a few hours.
No, there are some data points which are less than -11.9 and greater than 11.9. I am trying to use that syntax to find the elements. It gives me empty array.
Think about that logic!
y = x(x<=-11.9 & x>=11.9);
Of course it is going to be an empty array, because no value of ‘x’ can simultaneoously be ≤-11.9 and ≥11.9.
Use a ‘logical or’ instead:
y = x(x<=-11.9 | x>=11.9);
That may get you closer to your goal.
Thank you for everything. It worked.
Star Strider
2019년 2월 5일
My pleasure.
If my Answer helped you solve your problem, please Accept it!
One last thing: Is there any way, we can calculate time(seconds) from percentage?
Star Strider
2019년 2월 5일
That depends on what the original data and units are, and if it is possible to convert them relatively easily (a simple ratio or multiplication, or if a nonlinear relation is necessary).
It might be easier to calculate the time from the original counts, or from the bin (x-axis) values.
Without knowing what your data are, it is not possible to say definitively.
Thank you for the suggestion. I want to export my histogram figure to a powerpoint file. But, i need to import a specific powerpoint template. Can you please help me on this?
Star Strider
2019년 2월 6일
Unfortunately, I cannot. I have no experience with exporting MATLAB graphics to PowerPoint.
See if Yair Altman’s export_fig (link) File Exchange contribution will do what you want. It is highly regarded, and appears to do everything.
I have nevertheless Answered your original Question and all the other follow-ups to it.
Switching back to the histogram plot, I am trying to get the range of x axis where the y value is highest, how do i do it?
Shambhavi Adhikari
2019년 2월 6일
편집: Shambhavi Adhikari
2019년 2월 6일
I mean, how to get xaxis range where 90% of the data lies?
I am trying to get all the elements that doesn't apply this condition,
filter_data= x(x~=data);
where x is the matrix of elements from 10 to 50 and data is another matrix from 25 to 30.
I want the filter data and get values from 10 to 25 and from 30 to 50 in one matrix.
I tried this expression but it doesn't work.
Star Strider
2019년 2월 6일
- I do not have your data.
- You still need to Accept my Answer.
I have accepted your answer
Star Strider
2019년 2월 6일
Thank you.
With regard to:
I mean, how to get xaxis range where 90% of the data lies?
What ‘90%’ do you mean? The central 90%, the cumulative distribution from 0%-90%, or something else? There are many ways to interpret this.
Taking a wild guess with respect to:
I am trying to get all the elements that doesn't apply this condition,
filter_data= x(x~=data);
where x is the matrix of elements from 10 to 50 and data is another matrix from 25 to 30.
I want the filter data and get values from 10 to 25 and from 30 to 50 in one matrix.
Star Strider
2019년 2월 6일
Shambhavi Adhikari’s ‘Answer’ moved here:
What ‘90%’ do you mean? The central 90%, the cumulative distribution from 0%-90%, or something else? There are many ways to interpret this.
It means that, I want to get x axis range, where the most of the data falls, 80-90% of the whole data.
I am still not certain what 90% you want.
This takes approximately the central 90%:
X = 5*randn(1,110000); % Create Data
BinEdges = -12 : 12;
h = histogram(X, BinEdges, 'Normalization','probability');
XTL = {'-S','-11','-10','-9','-8','-7','-6','-5','-4','-3','-2','-1','0','1','2','3','4','5','6','7','8','9','10','11','S'};
yt = get(gca,'YTick');
set(gca, 'XTick',BinEdges', 'XTickLabel', XTL, 'YTick',yt, 'YTickLabel',yt*100)
xlabel('Bin Ranges')
ylabel('Probability (%)')
BinCenters = BinEdges(1:end-1)+0.5;
BinValues = h.Values;
text(BinCenters, BinValues, sprintfc('%6.3f%%',BinValues*100), 'HorizontalAlignment','center', 'VerticalAlignment','bottom', 'FontSize',4)
xfcn = @(pct) cumsum(BinValues)>=(0.5-pct/200) & cumsum(BinValues)<=(0.5+pct/200);
x80 = BinCenters(xfcn(80));
x90 = BinCenters(xfcn(90));
The ‘xfcn’ anonymous function returns a logical vector corresponding to approximately the central ‘pct’ of the histogram data. The ‘x80’ and ‘x90’ variables are the bin centres associated with those values.
I used these two assignments to check that the bin centres chosen were approximately correct:
Check80 = sum(BinValues(xfcn(80)));
Check90 = sum(BinValues(xfcn(90)));
Since the ‘BinValues’ vector is in probabilities (percentages), these values can be read directly as the decimal fractions of the entire histogram.
This one worked. Thank you.
So if you have x tick values up 50 and if it's in this format: 1s,2s,3s,4s till 50s, is there any way we can give the xtick values in loop instead of writing number till 50?
Star Strider
2019년 2월 6일
As always, my pleasure.
I do not understand. I am using the information you previously supplied about how you want the x-tick values of your histogram to appear. What do ‘1s,2s,3s,4s till 50s’ refer to?
I just only want to add a string 's' on the bin values. My bin values were from -12 to 12. I wrote all the values as XTL = {-11','-10','-9','-8','-7','-6','-5','-4','-3','-2','-1','0','1','2','3','4','5','6','7','8','9','10','11'};
Is there any way i can write the bin values as below but using some loop starting from -12 to 12.
XTL = {'-11s','-10s','-9s','-8s','-7s','-6s','-5s','-4s','-3s','-2s','-1s','0s','1s','2s,'3s','4s','5s','6s','7s','8s','9s','10s','11s'};
Try this:
XTL11c = sprintfc('%ds', -11:11);
producing:
XTL11c =
1×23 cell array
Columns 1 through 9
{'-11s'} {'-10s'} {'-9s'} {'-8s'} {'-7s'} {'-6s'} {'-5s'} {'-4s'} {'-3s'}
Columns 10 through 19
{'-2s'} {'-1s'} {'0s'} {'1s'} {'2s'} {'3s'} {'4s'} {'5s'} {'6s'} {'7s'}
Columns 20 through 23
{'8s'} {'9s'} {'10s'} {'11s'}
Thank you
Star Strider
2019년 2월 6일
As always, my pleasure.
I was able to get 90% data, but 80% doesn't work. I mean x80 = BinCenters(xfcn(80)); doesn't work.
Star Strider
2019년 2월 6일
It worked in my code, and with my simulated data. It gave the correct result with ‘Check80’ as well.
Yair Altman
2019년 2월 6일
I want to export my histogram figure to a powerpoint file. But, i need to import a specific powerpoint template. Can you please help me on this?
The best way is to export your figure into some image file, and then import that image file into your Powerpoint file. You can export via the built-in Matlab print() command, or the export_fig utility, or the ScreenCapture utility, or any external (non-Matlab) screen-grab utility that you may have.
Back in 2006 or 2008 I created a utility called OfficeDoc that enabled full programmatic Matlab import/export/formatting of Office files (DOC/XLS/PPT). However, with changes in Matlab and Office I found that maintaining this utility is a pain so I gave up and removed it from the File Exchange. I believe that you can still find other PPT export utilities on File Exchange if you search for them.
Star Strider
2019년 2월 6일
@Yair — Thank you!
Hi,
Looks like the xfcn = @(pct) cumsum(BinValues)>=(0.5-pct/200) & cumsum(BinValues)<=(0.5+pct/200);
x80 = BinCenters(xfcn(80));
x90 = BinCenters(xfcn(90));
is not working. I am trying to get the x values of the data where most of the data frequency lies (90%). I have attached a figure. Most of my data lies in between 200 to 205 but, when I try to use the above command, it gives me value as 195.5 to 200.5. Can you help me on this?

Star Strider
2019년 2월 7일
Can you help me on this?
Unfortunately, I cannot. I do not have your data, so I cannot experiment with it. From the image you posted, it looks as though your data are between 195 and 210, and that you have only 3 bins.
My code is based on your having 24 bins, since that is what you previously wanted, and my code works as well as it can (and is as accurate as it can be given that bin precision) with those results. It calculates the approximate centre of your data (as 50% of the maximum, that may not be the mode of your data distribution), and uses the cumsum function and thresholding to choose the bins with the requested data frequency. The data I synthesised to test my code was from a normal distribution, so it was symmetrical.
I do not know the distribution of your data. I suggest that you use the histfit (link), fitdist (link), or similar function to determine the distribution that best fits your data. You can then determine from the returned parameters and the cdf (link) function for that distribution how best to estimate the probabilities you are interested in.
추가 답변 (0개)
카테고리
도움말 센터 및 File Exchange에서 Standard File Formats에 대해 자세히 알아보기
참고 항목
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)
