필터 지우기
필터 지우기

Having multiple different bins in a histogram that don't overlap

조회 수: 12 (최근 30일)
Zachary
Zachary 2023년 12월 8일
편집: Voss 2023년 12월 8일
This is some test code as I try to figure out histograms.
Is there anyway to make it so all the colors are separate? Code is below
clear
clc
sensorVoltage = 5*rand(12,100)%test array - normally i would have voltage data
figure;
subplot(1,2,1)
histogram(sensorVoltage(1,:),'BinWidth', 0.1,'FaceColor','c','DisplayName','Lights On, No LED','DisplayStyle', 'bar')
hold on
histogram(sensorVoltage(2,:),'BinWidth', 0.1,'FaceColor','m','DisplayName','Lights On, IR LED','DisplayStyle', 'bar')
histogram(sensorVoltage(3,:),'BinWidth', 0.1,'FaceColor','r','DisplayName','Lights On, Red LED','DisplayStyle', 'bar')
histogram(sensorVoltage(4,:),'BinWidth', 0.1,'FaceColor','g','DisplayName','Lights On, Green LED','DisplayStyle', 'bar')
histogram(sensorVoltage(5,:),'BinWidth', 0.1,'FaceColor','b','DisplayName','Lights On, Blue LED','DisplayStyle', 'bar')
histogram(sensorVoltage(6,:),'BinWidth', 0.1,'FaceColor','y','DisplayName','Lights On, Yellow LED','DisplayStyle', 'bar')
title('IR Voltage with Lights ON')%Lights on Histogram
grid on
legend('show')
ylabel('Number of Readings')
xlabel('Voltage (V)')
hold on
subplot(1,2,2)
histogram(sensorVoltage(1,:),'BinWidth', 0.1,'FaceColor','c','DisplayName','Lights Off, No LED','DisplayStyle', 'bar')
hold on
histogram(sensorVoltage(2,:),'BinWidth', 0.1,'FaceColor','m','DisplayName','Lights Off, IR LED','DisplayStyle', 'bar')
histogram(sensorVoltage(3,:),'BinWidth', 0.1,'FaceColor','r','DisplayName','Lights Off, Red LED','DisplayStyle', 'bar')
histogram(sensorVoltage(4,:),'BinWidth', 0.1,'FaceColor','g','DisplayName','Lights Off, Green LED','DisplayStyle', 'bar')
histogram(sensorVoltage(5,:),'BinWidth', 0.1,'FaceColor','b','DisplayName','Lights Off, Blue LED','DisplayStyle', 'bar')
histogram(sensorVoltage(6,:),'BinWidth', 0.1,'FaceColor','y','DisplayName','Lights Off, Yellow LED','DisplayStyle', 'bar')
title('IR Voltage with Lights Off')%Lights off Histogram
grid on
legend('show')
ylabel('Number of Readings')
xlabel('Voltage (V)')
hold off

답변 (2개)

Walter Roberson
Walter Roberson 2023년 12월 8일
Are you looking for bar with the 'stacked' option?
Or are you looking for bar() with groups?
Generally speaking, there are two phases to creating histogram plots: first do the binning, and then draw the bars. histogram() combines both of those together but is not as flexible in plotting.
Perhaps you should use histcounts and then bar() to get the visual results you want.

Voss
Voss 2023년 12월 8일
편집: Voss 2023년 12월 8일
Maybe something like this?
sensorVoltage = 5*rand(12,100)%test array - normally i would have voltage data
sensorVoltage = 12×100
0.4177 4.0336 0.7738 2.2657 0.6297 3.4554 1.4350 0.8823 1.6507 4.4461 4.0671 1.5809 0.3994 0.1572 1.5080 4.7796 0.0623 3.5689 1.5176 3.0059 0.6806 0.0297 1.8081 2.4819 3.4668 0.9644 4.5350 0.1630 2.1472 4.8051 4.5674 4.7495 1.9615 1.7357 4.9564 0.6896 0.2801 4.5186 3.3028 4.2793 2.9565 0.3077 3.9920 3.6406 0.1744 4.9976 0.9623 0.0547 4.9393 1.0070 4.8242 0.5600 3.2329 1.0574 4.4414 0.8714 4.6885 3.2152 1.9553 0.8355 0.7146 1.6227 1.7269 0.4718 4.9787 1.3865 0.6758 2.9102 2.6503 3.1987 3.5453 1.2965 4.1426 4.5959 1.6312 0.1613 4.3101 3.8613 0.9238 2.4112 4.2390 2.6644 1.6766 3.2454 3.1984 1.7240 3.8949 2.8660 3.5085 1.5469 4.4271 3.5501 1.9681 0.4199 0.7312 2.5570 0.8185 1.4522 1.7374 1.6708 2.4930 1.3464 3.3989 0.9725 4.0918 0.4835 0.3801 4.8589 4.2190 0.7245 1.9211 3.2753 1.5029 2.7217 1.8622 1.2038 4.8075 1.8973 3.7876 0.2182 3.1214 2.9809 0.3764 0.0739 0.6571 1.9739 2.7454 2.2941 4.9073 2.1906 1.6624 0.6427 1.6555 1.1709 3.6305 0.6217 1.2719 0.5093 1.7512 2.0057 2.8033 4.9083 3.7687 0.7544 1.5473 4.6915 0.8330 1.2667 3.8344 3.8640 0.3585 1.2317 0.6557 2.4890 4.6950 3.5630 0.0037 3.8812 0.5289 0.9364 3.8769 1.6904 3.5744 2.5837 2.4207 2.1838 2.4696 3.5698 0.4420 1.5855 1.5856 4.5014 0.6619 3.4445 4.1809 0.9987 4.8214 0.5330 0.2829 2.1901 1.3552 3.3292 4.6375 2.0638 1.4775 4.4832 0.6217 3.7274 3.4742 4.6944 0.1645 0.8138 0.4521 4.1572 3.5765 4.7408 3.5759 2.9129 3.4656 3.5928 3.3173 3.8935 4.6737 1.1281 2.3501 4.6219 3.7462 4.0470 0.6207 3.0156 2.7085 3.7970 3.2473 4.8349 2.6039 2.6848 2.5552 1.6330 4.0485 3.1603 2.5413 2.2547 1.4336 4.4223 0.1992 1.7133 2.7647 0.4247 0.2607 1.4393 3.6734 1.0923 2.0151 2.8071 3.7071 2.9161 1.9900 1.0419 0.5632 3.7412 2.1800 2.4522 4.8973 0.1430 4.8757 2.3771 4.6977 1.3330 2.8093 3.3505 2.9989 2.0797 2.2876 4.2934 2.9602 2.7798 1.6620 4.5379 1.9217 3.0018 2.7762 4.2434 1.9779 4.8598 2.3703 2.5142 0.7387 0.0752 0.4309 1.7884 0.3769 0.8533 2.5493 4.1992 2.8035 0.9763 4.7557 1.0965 3.9335 1.5346 3.1690 4.6237 3.4061 1.2939 3.9410 0.2454 2.2594 1.6766 1.6549 4.1183 0.5947 1.6874 3.8147 0.5080 3.5882 0.2900 3.1513 1.1278 4.3796 0.1745
figure('Position',[10 10 600 1000]);
colors = repmat('cmrgby',1,2);
names = "Lights " + ["On","Off"] + ", " + ["No";"IR";"Red";"Green";"Blue";"Yellow"] + " LED";
tiledlayout(6,2,'TileIndexing','columnmajor')
h = gobjects(6,2);
for ii = 1:12
nexttile()
h(ii) = histogram(sensorVoltage(ii,:),'BinWidth', 0.1,'FaceColor',colors(ii),'DisplayName',names(ii),'DisplayStyle', 'bar');
grid on
if ii <= 6
ylabel('Num. Readings')
end
m = mod(ii,6);
if m == 1
if ii == 1
state_str = 'ON';
else
state_str = 'Off';
end
title(sprintf('IR Voltage with Lights %s',state_str))
elseif m == 0
xlabel('Voltage (V)')
end
end
leg = legend(h(:),'NumColumns',2);
leg.Layout.Tile = 'north';

카테고리

Help CenterFile Exchange에서 Histograms에 대해 자세히 알아보기

제품


릴리스

R2023b

Community Treasure Hunt

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

Start Hunting!

Translated by