![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/181330/image.png)
I need to plot a histogram
3 ビュー (過去 30 日間)
古いコメントを表示
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 件のコメント
採用された回答
Star Strider
2018 年 1 月 24 日
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; cumsum(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 —
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/181330/image.png)
3 件のコメント
Star Strider
2018 年 7 月 13 日
Thank you.
It might be possible with arrayfun (that contains implied loops). The for loop is easier, and likely more efficient.
Peyman Mostafaei
2021 年 3 月 9 日
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
その他の回答 (4 件)
Steven Lord
2018 年 7 月 13 日
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 件のコメント
Walter Roberson
2018 年 1 月 24 日
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 件のコメント
FURKAN CEVAHIR
2018 年 1 月 24 日
編集済み: FURKAN CEVAHIR
2018 年 1 月 24 日
1 件のコメント
Star Strider
2018 年 1 月 24 日
As always, my pleasure!
Add 'XScale' to the set call:
set(gca, 'XTick',V1L, 'FontSize',6, 'XScale','log')
参考
カテゴリ
Help Center および File Exchange で Data Distribution Plots についてさらに検索
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!