Plot histograms of two samples as plots
現在この質問をフォロー中です
- フォローしているコンテンツ フィードに更新が表示されます。
- コミュニケーション基本設定に応じて電子メールを受け取ることができます。
エラーが発生しました
ページに変更が加えられたため、アクションを完了できません。ページを再度読み込み、更新された状態を確認してください。
古いコメントを表示
I would like to plot the histograms of two samples as a line plot.
I've been using the histograms functions but this is not what I want. I would like the histogram to be a plot rather than histogram with boxes.
I would also like to plot the line plot of each sample in the same plot but using different colour. I would also like to fix the x- and y-axis.
Can someone help with this?
採用された回答
"I would like the histogram to be a plot rather than histogram with boxes."
Option 1: Draw line at the center, top of each bar
I interpretted this as a line that traces the height of each bar of the histogram. Or, would you rather plot the probability density function? Here's a demo that achieves the prior.
% Create some fake data
a = randn(1, 10000).*100;
b = randn(1, 10000).*50;
% Create histogram bins
nBins = 50; %number of bins
aEdges= linspace(min(a),max(a), nBins+1);
bEdges= linspace(min(b),max(b), nBins+1);
figure
subplot(2,1,2)
histogram(a,aEdges);
hold on
histogram(b,bEdges);
subplot(2,1,1)
aX = aEdges(2:end)-((aEdges(2)-aEdges(1))/2); %bin centers
aCounts = histcounts(a,aEdges);
plot(aX, aCounts, 'b-s')
hold on
bX = bEdges(2:end)-((bEdges(2)-bEdges(1))/2); %bin centers
bCounts = histcounts(b,bEdges);
plot(bX, bCounts, 'r-s')

" I would also like to fix the x- and y-axis"
Option 2: Fit the histrogram with a smooth line
See the examples in the documentation
8 件のコメント
Gina Carts
2019 年 4 月 30 日
編集済み: Gina Carts
2019 年 4 月 30 日
The first plot it's what I need. Can I plot only the line though? I mean without the squares on top of the line plot.
I included the histogram too for visual comparison.
To plot without the square markers,
plot(aX, aCounts, 'b-s') %Change this...
plot(aX, aCounts, 'b-') %...to this
Gina Carts
2019 年 5 月 2 日
Why the number of bins has an effect in the y-axis (i.e. frequency)?
How do I know the correct number of bins? The information in y-axis isn't it important?
I attach a figure of my results. I used same data but in one case I used nBins=50 in the other case nBins=100.
Adam Danz
2019 年 5 月 2 日
"Why the number of bins has an effect in the y-axis (i.e. frequency)? "
In a histogram plot, the y-axis is frequency. For a bar that spans from x=0.01 to x=0.02 and has a height of y=112, that means there are 112 data points between the values of x=0.01 and x=0.02.
If you increase the width of the bar for the same dataset, the number of data points that fall within those bounds will increase.
So the wider the bars, the larger the counts.
"How do I know the correct number of bins? "
There is no correct answer but there are often wrong answers. Some bad science results from chosing bin widths that make your data appear to have different distributions or densities. Sometimes thinking about your data will help with that decision if there are natrual intervals within your data. Sometimes thinking about the question you're asking will help with that decision.
"The information in y-axis isn't it important?"
Yes. It tells you how often (frequency) your data fall into each bin. I can't say whether that's what you're looking for or not. You could plot a normalized histogram where all the bars hights add to 1.0. You could normalize the histrogram so the tallest bar is always equal to 1. You could plot the underlying density function. There's lots of options but it all comes down to what you're trying to do.
Lastly, I'll update my answer with an alternative method that adds a smooth, fitted line.
Gina Carts
2019 年 5 月 28 日
I wanted to modify the y-axis and put my limits but I cannot do it.
I set my own edges and y-axis limit. Below it's my code
a = randn(1, 10000).*100;
b = randn(1, 10000).*50;
aEdges=-1:0.025:1;
bEdges=-1:0.025:1;
figure
subplot(2,1,2)
histogram(a,aEdges);
hold on
histogram(b,bEdges);
subplot(2,1,1)
aX = aEdges(2:end)-((aEdges(2)-aEdges(1))/2);
aCounts = histcounts(a,aEdges);
plot(aX, aCounts, 'b-s')
axis([0,1,0,0.15])
hold on
bX = bEdges(2:end)-((bEdges(2)-bEdges(1))/2);
bCounts = histcounts(b,bEdges);
plot(bX, bCounts, 'r-s')
axis([0,1,0,0.15])
When I use your code I get the correct shape of my distributions but I want to set my own limits.
Adam Danz
2019 年 5 月 28 日
Have you tried ylim() ?
Gina Carts
2019 年 5 月 28 日
Yea the ylim() works. Is there any way to make the lines on the plot thicker?
Adam Danz
2019 年 5 月 28 日
plot(x,y,'LineWidth', 3)
% or
h = plot(x,y);
h.LineWidth = 3;
% or
set(h, 'LineWidth', 3)
その他の回答 (0 件)
カテゴリ
ヘルプ センター および File Exchange で Data Distribution Plots についてさらに検索
製品
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!Web サイトの選択
Web サイトを選択すると、翻訳されたコンテンツにアクセスし、地域のイベントやサービスを確認できます。現在の位置情報に基づき、次のサイトの選択を推奨します:
また、以下のリストから Web サイトを選択することもできます。
最適なサイトパフォーマンスの取得方法
中国のサイト (中国語または英語) を選択することで、最適なサイトパフォーマンスが得られます。その他の国の 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)
