Bar graph using hold on

조회 수: 11 (최근 30일)
Krishnapriya Suresh
Krishnapriya Suresh 2019년 10월 23일
댓글: Renato SL 2019년 10월 23일
I tried plotting bar graphs of cumulative rainfall for the months of june, july and august (13 weeks) of 6 consecutive years in matlab using for loop. I used hold on function to get all the plots in a single graph. But instead of getting a side-by-side bars, I got bar graphs stacked on top. Please help me create a side-by-side graph.
This is the code I used
clear all
a=[1:1:13]
list=dir('*.TXT')
for i=1:13(list)
f=list(i).name
rain_cum(i)=textread(f)*649.2/1420000
end
bar(a,cumsum(rain_cum))
xlabel=('Weeks');
ylabel=('Rainfall');
hold on
list=dir('*.TXT')
for i=14:26(list)
f=list(i).name
rain_cum(i-13)=textread(f)*649.2/1420000
end
bar(a,cumsum(rain_cum))
xlabel=('Weeks');
ylabel=('Rainfall');
hold on
list=dir('*.TXT')
for i=27:39(list)
f=list(i).name
rain_cum(i-26)=textread(f)*649.2/1420000
end
bar(a,cumsum(rain_cum))
xlabel=('Weeks');
ylabel=('Rainfall');
hold on
list=dir('*.TXT')
for i=40:52(list)
f=list(i).name
rain_cum(i-39)=textread(f)*649.2/1420000
end
bar(a,cumsum(rain_cum))
xlabel=('Weeks');
ylabel=('Rainfall');
hold on
list=dir('*.TXT')
for i=53:65(list)
f=list(i).name
rain_cum(i-52)=textread(f)*649.2/1420000
end
bar(a,cumsum(rain_cum))
xlabel=('Weeks');
ylabel=('Rainfall');
hold on
list=dir('*.TXT')
for i=66:78(list)
f=list(i).name
rain_cum(i-65)=textread(f)*649.2/1420000
end
bar(a,cumsum(rain_cum))
xlabel=('Weeks');
ylabel=('Rainfall');
legend({'y=2013','y=2014','y=2015','y=2016','y=2017','y=2018'},'location','northwest')
hold off
This is what I got
cumulative_rain.jpg
  댓글 수: 1
Renato SL
Renato SL 2019년 10월 23일
I think the documentation of bar (here) addresses your issue. Read the Display group of bars section.
Other points:
  • I don't think you need to declare list multiple times
  • I don't think you need to declare hold on multiple times (in case you must use it)
  • Can you combine the loops to read the data into just 1 loop? It would be nicer to see.
  • The xlabel & ylabel line should be xlabel('Weeks') and ylabel('Rainfall') respectively. ylabel documentation here for details.

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

답변 (0개)

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by