How can I edit code so only bottom graph is displayed.

조회 수: 1 (최근 30일)
Aayma Mughal
Aayma Mughal 2022년 8월 6일
답변: Steven Lord 2022년 8월 6일
Hello,
I tried using histcount, but I cannot get it to work. This code prints like the screenshot. I only want the bottom graph to show. How can I accomplish this?
Thank you
figure(1)
subplot(2,1,1);
hist_female_age = histogram(female(:,1));
subplot(2,1,2);
hist_male_age = histogram(male(:,1),hist_female_age.BinEdges);
hist = [hist_male_age.Values;hist_female_age.Values];
bins1 = 45:10:95;
bar(bins1,hist)
xlabel("Age of patient") ;
ylabel("Number of patients") ;
legend("Male Patient","Female patient") ;

답변 (2개)

dpb
dpb 2022년 8월 6일
W/o the data and w/o knowing what didn't work, the simplest way to just get the second plot alone by itself would be to just draw it by itself -- since you're only using histogram as a crutch and don't care about the plot,
hist_female_age = histogram(female(:,1));
hist_male_age = histogram(male(:,1),hist_female_age.BinEdges);
ages = [hist_male_age.Values;hist_female_age.Values];
bins1 = 45:10:95;
bar(bins1,ages)
xlabel("Age of patient")
ylabel("Number of patients")
legend("Male Patient","Female patient")
will leave you with just one full-sized figure...or you could leave as you have it and just create another figure at the end and replot bar into it...
Not sure where you went wrong with histcounts, but something like
edges=[40:10:100];
[nF,e,iF]=histcounts(female(:,1),edges);
[nM,e,iM]=histcounts(male(:,1),edges);
x=mean([e(1:end-1);e(2:end)]); % bin midpoints
hB=bar(x,[nM;nF]);
should create the bar graph more directly. (NB: Aircode, untested...)

Steven Lord
Steven Lord 2022년 8월 6일
The histcounts function does not return an object with properties like the histogram function does, but the outputs from histcounts serve the same purpose as some of the properties of the histogram object. The bin counts and the bin edges (which are the two properties you use from your first histogram call) are the first two outputs (respectively) from histcounts.
x = randn(1, 1e5);
[binValues, binEdges] = histcounts(x);
h = histogram(x, binEdges);
checkValues = isequal(h.Values, binValues)
checkValues = logical
1
checkEdges = isequal(h.BinEdges, binEdges)
checkEdges = logical
1

카테고리

Help CenterFile Exchange에서 Data Distribution Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by