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 commentaire

Shambhavi Adhikari
Shambhavi Adhikari le 4 Fév 2019
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.

Connectez-vous pour commenter.

 Réponse acceptée

Star Strider
Star Strider le 4 Fév 2019
Modifié(e) : Star Strider le 4 Fév 2019
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')
See the documentation on Histogram Properties (link) for a discussion of all the options.

38 commentaires

Shambhavi Adhikari
Shambhavi Adhikari le 4 Fév 2019
Hi, Can i convert y axis to percentage instead of probability?
Star Strider
Star Strider le 4 Fév 2019
My impression is that ‘percentage’ is ‘probability’. For a full list of the options, see the documentation section on 'Normalization' (link).
Star Strider
Star Strider le 4 Fév 2019
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
Star Strider le 4 Fév 2019
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?
Shambhavi Adhikari
Shambhavi Adhikari le 4 Fév 2019
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
Star Strider le 5 Fév 2019
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
Star Strider le 5 Fév 2019
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.
Shambhavi Adhikari
Shambhavi Adhikari le 5 Fév 2019
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.
Shambhavi Adhikari
Shambhavi Adhikari le 5 Fév 2019
Thank you for everything. It worked.
Star Strider
Star Strider le 5 Fév 2019
My pleasure.
If my Answer helped you solve your problem, please Accept it!
Shambhavi Adhikari
Shambhavi Adhikari le 5 Fév 2019
One last thing: Is there any way, we can calculate time(seconds) from percentage?
Star Strider
Star Strider le 5 Fév 2019
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.
Shambhavi Adhikari
Shambhavi Adhikari le 6 Fév 2019
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
Star Strider le 6 Fév 2019
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.
Shambhavi Adhikari
Shambhavi Adhikari le 6 Fév 2019
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
Shambhavi Adhikari le 6 Fév 2019
Modifié(e) : Shambhavi Adhikari le 6 Fév 2019
I mean, how to get xaxis range where 90% of the data lies?
Shambhavi Adhikari
Shambhavi Adhikari le 6 Fév 2019
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
Star Strider le 6 Fév 2019
  1. I do not have your data.
  2. You still need to Accept my Answer.
Shambhavi Adhikari
Shambhavi Adhikari le 6 Fév 2019
I have accepted your answer
Star Strider
Star Strider le 6 Fév 2019
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.
the setdiff (link) function is likely the best option, with ismembertol also likely applicable.
Star Strider
Star Strider le 6 Fév 2019
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.
Shambhavi Adhikari
Shambhavi Adhikari le 6 Fév 2019
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
Star Strider le 6 Fév 2019
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?
Shambhavi Adhikari
Shambhavi Adhikari le 6 Fév 2019
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'}
Shambhavi Adhikari
Shambhavi Adhikari le 6 Fév 2019
Thank you
Star Strider
Star Strider le 6 Fév 2019
As always, my pleasure.
Shambhavi Adhikari
Shambhavi Adhikari le 6 Fév 2019
I was able to get 90% data, but 80% doesn't work. I mean x80 = BinCenters(xfcn(80)); doesn't work.
Star Strider
Star Strider le 6 Fév 2019
It worked in my code, and with my simulated data. It gave the correct result with ‘Check80’ as well.
Yair Altman
Yair Altman le 6 Fév 2019
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
Star Strider le 6 Fév 2019
@Yair — Thank you!
Shambhavi Adhikari
Shambhavi Adhikari le 7 Fév 2019
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?
example.PNG
Star Strider
Star Strider le 7 Fév 2019
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.

Connectez-vous pour commenter.

Plus de réponses (0)

Catégories

Community Treasure Hunt

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

Start Hunting!

Translated by