Plotting monthly precipitation and time

7 views (last 30 days)
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 Comment
Von Duesenberg
Von Duesenberg on 9 Jan 2017
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).

Sign in to comment.

Accepted Answer

Peter Perkins
Peter Perkins on 12 Jan 2017
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 Comment
Natasha Sekhon
Natasha Sekhon on 13 Jan 2017
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?

Sign in to comment.

More Answers (1)

Ed Marquez
Ed Marquez on 11 Jan 2017
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 Comment
Peter Perkins
Peter Perkins on 12 Jan 2017
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.

Sign in to comment.

Tags

Community Treasure Hunt

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

Start Hunting!