Plotting monthly precipitation and time

조회 수: 2 (최근 30일)
Natasha Sekhon
Natasha Sekhon 2017년 1월 9일
댓글: Natasha Sekhon 2017년 1월 13일
Hello There, Essentially I'm trying to plot x (time) and y (precipitation). x = [7/15/05 8/9/05 9/15/05 10/16/05 11/17/05 12/28/05 3/8/06 4/12/06 5/13/06 6/17/06 7/16/06 8/9/06 9/10/06 10/9/06 11/4/06 11/29/06 1/2/07];
y = [2.9 0.51 21.49 25.76 13.16 8.46 11.1 3.38 4.85 14.20 3.05 1.45 19.290 0.20 1.17 0.00 7.67];
The code i'm using is: figure(1) plot(x,y);grid;datetick('x','mm/dd/yyyy')
However my x axis is not plotting on the correct scale (attached figure). I seem to be missing a step. Any suggestions would be helpful! Cheers,
  댓글 수: 1
Von Duesenberg
Von Duesenberg 2017년 1월 9일
Hi. Perhaps you could post your code exactly as you typed it in Matlab (I copied and pasted your example but wasn't able to replicate your figure).

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

채택된 답변

Peter Perkins
Peter Perkins 2017년 1월 12일
In R2014b or later, use datetimes, not datenums:
>> x = {'7/15/05' '8/9/05' '9/15/05' '10/16/05' '11/17/05' '12/28/05' '3/8/06' '4/12/06' '5/13/06' '6/17/06' '7/16/06' '8/9/06' '9/10/06' '10/9/06' '11/4/06' '11/29/06' '1/2/07'}
x =
1×17 cell array
Columns 1 through 9
'7/15/05' '8/9/05' '9/15/05' '10/16/05' '11/17/05' '12/28/05' '3/8/06' '4/12/06' '5/13/06'
Columns 10 through 17
'6/17/06' '7/16/06' '8/9/06' '9/10/06' '10/9/06' '11/4/06' '11/29/06' '1/2/07'
>> t = datetime(x,'InputFormat','MM/dd/yy')
t =
1×17 datetime array
Columns 1 through 9
15-Jul-2005 09-Aug-2005 15-Sep-2005 16-Oct-2005 17-Nov-2005 28-Dec-2005 08-Mar-2006 12-Apr-2006 13-May-2006
Columns 10 through 17
17-Jun-2006 16-Jul-2006 09-Aug-2006 10-Sep-2006 09-Oct-2006 04-Nov-2006 29-Nov-2006 02-Jan-2007
>> y = [2.9 0.51 21.49 25.76 13.16 8.46 11.1 3.38 4.85 14.20 3.05 1.45 19.290 0.20 1.17 0.00 7.67];
>> plot(t,y)
  댓글 수: 1
Natasha Sekhon
Natasha Sekhon 2017년 1월 13일
Thank you for the tip! A quick follow up question: What if i had years for the 20th century, i.e., 1905 1906 and onwards. What should i do then?

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

추가 답변 (1개)

Ed Marquez
Ed Marquez 2017년 1월 11일
Hi, I am assuming you are using MATLAB R2014b or later. My understanding is that you would like to plot the values contained in 'y' (precipitation data) versus the values contained in x, which are dates.
First you will need to redefine your 'x' vector to be a cell array containing characters, as shown below:
x = {'7/15/05';'8/9/05';'9/15/05';'10/16/05';'11/17/05';'12/28/05';'3/8/06';'4/12/06';'5/13/06';...
'6/17/06';'7/16/06';'8/9/06';'9/10/06';'10/9/06';'11/4/06';'11/29/06';'1/2/07'};
The 'y' vector you are using can stay as is.
After that you can try the code below:
a = datenum(x);
b = datestr(x,'mmm-dd-yyyy');
figure(1)
axObj = axes;
plot(a,y);
grid;
datetick('x','mm/dd/yyyy')
axObj.XTick = (a);
axObj.XTickLabels = (b);
axObj.XTickLabelRotation = 45;
  댓글 수: 1
Peter Perkins
Peter Perkins 2017년 1월 12일
Just in case it wasn't clear from Ed's post, x in the OP's code is very different than intended:
>> x = [7/15/05 8/9/05]
x =
0.093333 0.17778
Without enclosing quotes, those slashes are divisions.

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by