Having multiple different bins in a histogram that don't overlap
12 Ansichten (letzte 30 Tage)
Ältere Kommentare anzeigen
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 Kommentare
Antworten (2)
Walter Roberson
am 8 Dez. 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 Kommentare
Voss
am 8 Dez. 2023
Bearbeitet: Voss
am 8 Dez. 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 Kommentare
Siehe auch
Kategorien
Mehr zu Histograms finden Sie in Help Center und File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!