Having multiple different bins in a histogram that don't overlap
12 views (last 30 days)
Show older comments
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 Comments
Answers (2)
Walter Roberson
on 8 Dec 2023
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 Comments
Voss
on 8 Dec 2023
Edited: Voss
on 8 Dec 2023
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 Comments
See Also
Categories
Find more on Histograms in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!