Having multiple different bins in a histogram that don't overlap
조회 수: 12 (최근 30일)
이전 댓글 표시
This is some test code as I try to figure out histograms.
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/1563799/image.png)
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
댓글 수: 0
답변 (2개)
Walter Roberson
2023년 12월 8일
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.
댓글 수: 0
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
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';
댓글 수: 0
참고 항목
카테고리
Help Center 및 File Exchange에서 Histograms에 대해 자세히 알아보기
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!