bar plot problem

조회 수: 11 (최근 30일)
raymond
raymond 2011년 11월 23일
hello i"m trying to graph a bar plot from form .1 to .9 based on this index p=.1:.1:.1 but its graphing from it seems to be plotting from about -.3 to 1.3 can someone help me please fix this
win=0 step=0 for p=.1:.1:.9
for i=1:1:100
a=3
b=3
c=3
while a>0 && b>0 && c>0
step=step+1
d=rand(1,3)
if d(1,1)<p && d(1,2)<p && d(1,3) <p
a=a+0
b=b+0
c=c+0
elseif d(1,1)>p && d(1,2) >p &&d(1,3) >p
a=a+0
b=b+0
c=c+0
elseif d(1,1)<p && d(1,3) < p
a= a -1
b= b+2
c= c-1
elseif d(1,1)>p &&d(1,3) >p
a= a -1
b= b+2
c= c-1
elseif d(1,3)<p &&d(1,2) < p
a= a +2
b= b-1
c= c-1
elseif d(1,2)>p && d(1,3) >p
a= a +2
b= b-1
c= c-1
elseif d(1,1)<p &&d(1,2) < p
a= a -1
b= b-1
c= c+2
elseif d(1,1)>p && d(1,2) >p
a= a -1
b= b-1
c= c+2
end
end
win=win+1
end
%axis([0 1 0 75])
avg=step/100
bar(p,avg)
hold on
end

채택된 답변

the cyclist
the cyclist 2011년 11월 23일
A quick fix is to put the following line after your bar() command:
>> set(get(gca,'Children'),'BarWidth',0.1)
The problem is that your bars are being plotted with width 0.8, by default. Then, each one overlaps the previous ones. If you put a breakpoint just after that bar() command, and see each bar being put down, you'll see what I mean.
A better solution, but which I am too lazy to implement for you, would be for you to get all the values of p and avg stored into vectors, then do the bar plot with the vectors, outside the loop. Then MATLAB would do the work of adjusting the widths appropriately.
  댓글 수: 1
raymond
raymond 2011년 11월 23일
thank you works great

댓글을 달려면 로그인하십시오.

추가 답변 (0개)

카테고리

Help CenterFile Exchange에서 Bar Plots에 대해 자세히 알아보기

태그

Community Treasure Hunt

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

Start Hunting!

Translated by