MATLAB: Bar in different colors for histfit

barcolorchangehistfithistogramMATLABMATLAB and Simulink Student Suiteweibull

Happy New Years Eve everyone!
I hope someone can help me with my problem:
I have some winddata, which I want to display in an histogram with a weibull distribution. I'm using the winddata to evaluate different locations for a windturbine. The windturbine generates electriciy when windspeed is more than 5 m/s. I would like to change the barcolor for windspeed smaller than 5 m/s so that it's easy to see which windspeeds are relevant for the windturbine.
My code so far:
function Weibulldistribution(windspeed)
weibulldata = windspeed;
weibulldata(weibulldata==0) = 0.000001;
figure
h=histfit(weibulldata ,25, 'weibull');
xlim([0 25]);
xticks(0:5:25);
xlabel ('windspeed in m/s');
ylabel ('Distribution in %');
yt = get(gca, 'YTick');
set (gca, 'YTickLabel', [num2str(round((yt'/numel(weibulldata))*100)), repmat(' %', length(yt),1)])
h(1).FaceColor = [0.38 0.6 0];
h(2).Color = [.2 .2 .2];
title('Weibulldistribution of Windspeed', 'FontSize',20);
end
How I'd like to color the bars:
untitled.bmp
I'm looking forward to your ideas!

Best Answer

Hi,
see the section "control individual bar colors" in the documentation:
For example to color the first 3 bars different to the others use this code:
function Weibulldistribution(windspeed)
weibulldata = windspeed;
weibulldata(weibulldata==0) = 0.000001;
figure
h=histfit(weibulldata ,25, 'weibull');
xlim([0 25]);
xticks(0:5:25);
xlabel ('windspeed in m/s');
ylabel ('Distribution in %');
yt = get(gca, 'YTick');
set (gca, 'YTickLabel', [num2str(round((yt'/numel(weibulldata))*100)), repmat(' %', length(yt),1)])
h(1).FaceColor = 'flat';
h(1).CData(1:3,:) = repmat([0.75 0 0.25],3,1);
h(1).CData(4:25,:) = repmat([0.38 0.6 0],22,1);
h(2).Color = [.2 .2 .2];
title('Weibulldistribution of Windspeed', 'FontSize',20);
Substitute your code by this and adapt it for your needs.
Best regards
Stephan