how to plot winter rainfall data with trend line

回答 (1 件)

dpb
dpb 2015 年 10 月 11 日

0 投票

y=[78:81].';r=[17 29 8 4].';
plot(y,r)
b=polyfit(y,r,1);
rhat=polyval(b,y);
hold on,plot(y,rhat,'r')
set(gca,'xtick',y)
xlim([y(1)-0.5 y(end)+0.5])
set(gca,'xticklabel',num2str([y-1 y]+1900,'%d-%d'))
xlabel('Winter Season of')
ylabel('Season rainfall (in)')
Salt to suit...

4 件のコメント

skyhunt
skyhunt 2015 年 10 月 12 日
thank you for your replying me...u r script is not working..i attached my text file and correct this.
dpb
dpb 2015 年 10 月 12 日
編集済み: dpb 2015 年 10 月 12 日
You'll have to write specific formatting to handle the dash in the first column of data when reading the file
[y1 y2 p]=textread('1.txt','%4d-%2d %f');
will return the full year in y1 and the two-digit second year in y2. You'll have to clean up the script to handle the labeling using full year values since the example didn't roll over the century I just used the 2-digit values. Can't do this with the full data set for obvious reasons.
doc textread % for details
doc textscan % alternative but returns cell arrays
etc., ...
skyhunt
skyhunt 2015 年 10 月 12 日
編集済み: dpb 2015 年 10 月 12 日
[y1, y2, p]=textread('1.txt','%4d-%2d %f');
plot(y1,p)
b=polyfit(y1,p,1);
rhat=polyval(b,y1);
hold on,plot(y1,rhat,'r')
set(gca,'xtick',y1)
xlim([y1(1)-0.5 y1(end)+0.5])
set(gca,'xticklabel',num2str([y1-1 y1]+1900,'%d-%d'))
xlabel('Winter Season')
ylabel('Season rainfall (mm)')
x-axis some problem...please check it sir...
dpb
dpb 2015 年 10 月 12 日
That's the comment I made earlier re: using 2-yr vis a vis 4-yr dates. "Exercise for the student..." :)
Decide what you want the labeling to be and then figure out the formatting to produce that. You could simply start with using y1 alone but it should be obvious that adding 1900 to an already-four-year value isn't going to be what you want.

サインインしてコメントする。

カテゴリ

ヘルプ センター および File ExchangeChristmas / Winter についてさらに検索

質問済み:

2015 年 10 月 11 日

コメント済み:

dpb
2015 年 10 月 12 日

Community Treasure Hunt

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

Start Hunting!

Translated by