how to put different colors for different bars for barh function.

5 ビュー (過去 30 日間)
Seyhan Emre Gorucu
Seyhan Emre Gorucu 2012 年 7 月 17 日
Hello,
I have a bar plot that shows the value for each fluid type but I want the bar to be green if the value of the bar is larger than 0.7. The simplified code is as follows:
neworder = {
'Hot Water' [0.2700]
'Steamflood' [0.4500]
'N2' [0.6800]
'CO2' [1.0100]
'HC' [1.0100]};
for i=1:length(neworder)
if cell2mat(neworder(i,2))<0.7
neworder1(i,:)=neworder(i,:);
else
neworder2(i,:)=neworder(i,:);
end
end
figure
barh([neworder1{:,2}],0.5,'b');
set(gca,'YtickL',neworder1(:,1),...
'XLim',[0 1],...
'Color','white');
hold on
barh([neworder2{:,2}],0.5,'g');
set(gca,'YtickL',neworder2(:,1),...
'XLim',[0 1],...
'Color','white');
This code puts two values on the same plots as I wanted but some of the values disappear. What I want is I want to see 5 bars 3 of which are blue and the rest two are green.
Thank you very much in advance.

採用された回答

per isakson
per isakson 2012 年 7 月 18 日
編集済み: per isakson 2012 年 7 月 18 日
Study this and note especially the length and values of neworder, neworder1 and neworder2 in your and my revised code.
function cssm()
neworder = {
'Hot Water' [0.2700]
'Steamflood' [0.4500]
'N2' [0.6800]
'CO2' [1.0100]
'HC' [1.0100]};
y_labels = neworder( :, 1 );
neworder1 = cat( 2, y_labels, num2cell( nan(5,1) ) );
neworder2 = cat( 2, y_labels, num2cell( nan(5,1) ) );
for i=1:length(neworder)
if cell2mat(neworder(i,2))<0.7
neworder1(i,:)=neworder(i,:);
else
neworder2(i,:)=neworder(i,:);
end
end
figure
barh([neworder1{:,2}],0.5,'b');
set(gca,'YtickL',neworder1(:,1),...
'XLim',[0 1],...
'Color','white');
hold on
barh([neworder2{:,2}],0.5,'g');
set(gca,'YtickL',neworder2(:,1),...
'XLim',[0 1],...
'Color','white');
end
I've included "%%" for a purpose. They make it possible to run one cell at a time and inspect the result. There are buttons, "Evaluate Cell ..."

その他の回答 (0 件)

カテゴリ

Help Center および File ExchangeGraphics Object Properties についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by