Bar graph using hold on

11 Ansichten (letzte 30 Tage)
Krishnapriya Suresh
Krishnapriya Suresh am 23 Okt. 2019
Kommentiert: Renato SL am 23 Okt. 2019
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 Kommentar
Renato SL
Renato SL am 23 Okt. 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.

Melden Sie sich an, um zu kommentieren.

Antworten (0)

Kategorien

Mehr zu 2-D and 3-D Plots finden Sie in Help Center und File Exchange

Community Treasure Hunt

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

Start Hunting!

Translated by