11 views (last 30 days)
Show older comments
FURKAN CEVAHIR on 24 Jan 2018
-
-
Link
Direct link to this question
https://webchat.mathworks.com/matlabcentral/answers/378667-i-need-to-plot-a-histogram
Commented: Peyman Mostafaei on 9 Mar 2021
Accepted Answer: Star Strider
- image.png
- image.png
Open in MATLAB Online
I tried to plot it with the excel, but excel is giving size range as the same size columns. Columns width is not supposed to be the same. So, I changed the program and decided to plot this histogram on the Matlab.
Can you help me to plot that histogram?
Size Interval (um)
0-0.2
0.2-0.4
0.4-0.6
0.6-0.8
0.8-1.0
1.0-1.2
1.2-1.4
1.4-1.6
1.6-1.8
1.8-2.1
2.1-2.7
2.7-3.6
3.6-5.1
and
Number of particle in interval per cm-3 of air
10
80
132
142
138
112
75
65
52
65
62
32
35
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Sign in to answer this question.
Accepted Answer
Star Strider on 24 Jan 2018
Open in MATLAB Online
This may do what you want.
The Code —
edges = [0 : 0.2 : 1.8, 2.1, 2.7, 3.6, 5.1];
vals = [10 80 132 142 138 112 75 65 52 65 62 32 35];
V1 = [vals' diff(edges')];
V1L = [0; c*msum(V1(:,2))]; % Cumulative Lengths
figure
AxH = axes('NextPlot','add');
for k1 = 1:size(V1,1)
patch([0 1 1 0]*V1(k1,2)+V1L(k1),[0 0 1 1]*V1(k1,1), rand(1,3), 'LineWidth',0.1)
end
hold off
axis([min(edges) max(edges) 0 max(ylim)])
set(gca, 'XTick',V1L, 'FontSize',8)
I chose random colours. Experiment to get the result you want.
The Plot —
3 Comments Show 1 older commentHide 1 older comment
Show 1 older commentHide 1 older comment
leonidas86 on 13 Jul 2018
Direct link to this comment
https://webchat.mathworks.com/matlabcentral/answers/378667-i-need-to-plot-a-histogram#comment_588990
Hello Star Strider,
very nice solution. Is there a way to do this without the for loop?
Star Strider on 13 Jul 2018
Direct link to this comment
https://webchat.mathworks.com/matlabcentral/answers/378667-i-need-to-plot-a-histogram#comment_589015
Thank you.
It might be possible with arrayfun (that contains implied loops). The for loop is easier, and likely more efficient.
Peyman Mostafaei on 9 Mar 2021
Direct link to this comment
https://webchat.mathworks.com/matlabcentral/answers/378667-i-need-to-plot-a-histogram#comment_1378311
Open in MATLAB Online
In addition to this answer, there is another way which uses the command bar instead of patch.
You have to know that the default width in the bar command is 1. All you have to do is to create your plot for each element of data separately based on this fact through a for loop.
edges = [0 : 0.2 : 1.8, 2.1, 2.7, 3.6, 5.1];
vals = [10 80 132 142 138 112 75 65 52 65 62 32 35];
center = (edges(1:end-1) + edges(2:end))/2;
width = diff(edges);
hold on
for i=1:length(center)
bar(center(i),vals(i),width(i),'b')
end
hold off
Sign in to comment.
More Answers (4)
Steven Lord on 13 Jul 2018
Open in MATLAB Online
E = [0, 0.2, 0.4, 0.6, 0.8, 1.0, 1.2, 1.4, 1.6, 1.8, 2.1, 2.7, 3.6, 5.1];
C = [10, 80, 132, 142, 138, 112, 75, 65, 52, 65, 62, 32, 35];
histogram('BinCounts', C, 'BinEdges', E)
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
Walter Roberson on 24 Jan 2018
Open in MATLAB Online
edges = [0 : 0.2 : 1.8, 2.1, 2.7, 3.6, 5.1];
vals = [10
80
132
142
138
112
75
65
52
65
62
32
35];
centers = (edges(1:end-1) + edges(2:end));
bar(centers, vals)
set(gca, 'xtick', centers)
If you need the bars to be variable width (the full width of their bin) then more work is required, as bar() does not support that.
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
FURKAN CEVAHIR on 24 Jan 2018
Edited: FURKAN CEVAHIR on 24 Jan 2018
Thank you. But I need the bars which show variable widths according to the size intervals. How can I do that?
0 Comments Show -2 older commentsHide -2 older comments
Show -2 older commentsHide -2 older comments
Sign in to comment.
FURKAN CEVAHIR on 24 Jan 2018
Edited: FURKAN CEVAHIR on 24 Jan 2018
Great! Thank you so much. What if I want to plot X axis on logarithmic scale like that, Size Intervals (um) ln 0.0 - ln 0.2 ln 0.2 - ln 0.4 ln 0.4 - ln 0.6 ln 0.6 - ln 0.8 ln 0.8 - ln 1.0 ln 1.0 - ln 1.2 ln 1.2 - ln 1.4 ln 1.4 - ln 1.6 ln 1.6 - ln 1.8 ln 1.8 - ln 2.1 ln 2.1 - ln 2.7 ln 2.7 - ln 3.6 ln 3.6 - ln 5.1
1 Comment Show -1 older commentsHide -1 older comments
Show -1 older commentsHide -1 older comments
Star Strider on 24 Jan 2018
Direct link to this comment
https://webchat.mathworks.com/matlabcentral/answers/378667-i-need-to-plot-a-histogram#comment_528128
Open in MATLAB Online
As always, my pleasure!
Add 'XScale' to the set call:
set(gca, 'XTick',V1L, 'FontSize',6, 'XScale','log')
Sign in to comment.
Sign in to answer this question.
See Also
Categories
MATLABGraphics2-D and 3-D PlotsData Distribution PlotsHistograms
Find more on Histograms in Help Center and File Exchange
Tags
- histogram
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
Americas
- 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
- Deutsch
- English
- Français
- United Kingdom(English)
Asia Pacific
- Australia (English)
- India (English)
- New Zealand (English)
- 中国
- 日本Japanese (日本語)
- 한국Korean (한국어)
Contact your local office