Bar graph using hold on
Show older comments
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

1 Comment
Renato SL
on 23 Oct 2019
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.
Answers (0)
Categories
Find more on Bar Plots in Help Center and File Exchange
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!