Bar in different colors for histfit

4 views (last 30 days)
Mo B
Mo B on 1 Jan 2019
Commented: Mo B on 2 Jan 2019
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!

Accepted Answer

Stephan
Stephan on 1 Jan 2019
Edited: Stephan on 1 Jan 2019
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
  1 Comment
Mo B
Mo B on 2 Jan 2019
Great! Thank you very much.
This works perfectly.

Sign in to comment.

More Answers (0)

Community Treasure Hunt

Find the treasures in MATLAB Central and discover how the community can help you!

Start Hunting!