MATLAB Answers

My histogram color is different everytime I run the program and the legend colors don't match.

10 views (last 30 days)
Chase Viche
Chase Viche on 8 Oct 2019
Commented: Chase Viche on 10 Oct 2019
Every time I run the program my figure 7 histogram comes back with different colors and the Florida legend is correct but the Minnesota color in not. I can't figure out what is wrong with the program. I have also posted a picture of what my graphs come out like.
T = readtable('Numerical Methods EGM 3344 Speeds.xlsx');
F70 = T (1:100,1);
F65 = T (1:100,2);
F55 = T (1:100,3);
M70 = T (1:100,4);
M65 = T (1:100,5);
M55 = T (1:100,6);
figure(1);
histogram(F70.F70);
histogram(F70.F70,65:1:90)
histogram(F70.F70,'BinWidth',.5)
histogram(F70.F70, 'FaceColor', [0 0 0])
set(gca, 'XTick', 65:1:90)
title('Speeds Recorded from Florida Interstate 10 Speed Limit 70 MPH')
xlabel('Speeds Recorded')
ylabel('Amount of Each Speed')
figure(4);
histogram(M70.M70);
histogram(M70.M70,68:1:83)
histogram(M70.M70,'BinWidth',.5)
histogram(M70.M70, 'FaceColor', [1 0 0])
set(gca, 'XTick', 68:1:83)
title('Speeds Recorded from Minnesota Interstate 35 Speed Limit 70 MPH')
xlabel('Speeds Recorded')
ylabel('Amount of Each Speed')
figure(7);
histogram(F70.F70);
histogram(F70.F70, 'FaceColor', [0 0 0])
set(gca, 'XTick', 65:1:90)
hold on
histogram(M70.M70);
histogram(M70.M70, 'FaceColor', [1 0 0])
title('Comparison for the 70 MPH Speed Limit Zones')
xlabel('Speeds Recorded')
ylabel('Amount of Each Speed')
legend('Florida','Minnesota')

  0 Comments

Sign in to comment.

Answers (1)

Chidvi Modala
Chidvi Modala on 10 Oct 2019
Hello Chase Viche,
From my understanding, you are trying to plot two histograms on the same figure. But histogram is coming back with different colors. It is because you are plotting each histogram twice as shown by the commented code below
figure(7);
% histogram(M70.M70);
% histogram(M70.M70, 'FaceColor', [1 0 0])
set(gca, 'XTick', 65:1:90)
hold on
% histogram(M70.M70);
% histogram(M70.M70, 'FaceColor', [1 0 0])
title('Comparison for the 70 MPH Speed Limit Zones')
xlabel('Speeds Recorded')
ylabel('Amount of Each Speed')
legend('Florida','Minnesota')
Try replacing the above code with the below one
figure(7);
histogram(F70.F70, 'FaceColor', [0 0 0])
set(gca, 'XTick', 65:1:90)
hold on
histogram(M70.M70, 'FaceColor', [1 0 0])
title('Comparison for the 70 MPH Speed Limit Zones')
xlabel('Speeds Recorded')
ylabel('Amount of Each Speed')
legend('Florida','Minnesota')

  1 Comment

Chase Viche
Chase Viche on 10 Oct 2019
Thank you that fixed it. In my code it was F70 first then M70. But I think what was happening is that the I was double calling for the histogram with this:
%histogram(M70.M70);
% histogram(M70.M70, 'FaceColor', [1 0 0])
When I only needed this code:
% histogram(M70.M70, 'FaceColor', [1 0 0])
Thank you again.

Sign in to comment.