Discover MakerZone

MATLAB and Simulink resources for Arduino, LEGO, and Raspberry Pi

Learn more

Discover what MATLAB® can do for your career.

Opportunities for recent engineering grads.

Apply Today

Thread Subject:
Plot date labels

Subject: Plot date labels

From: mklcst mklcst

Date: 9 Aug, 2012 17:47:16

Message: 1 of 16

Hi everybody, I have a 2 column matrix to plot, one with the dates in number and the other column with the values. My problem is the date labels to plot on the x axis, because if I have 100obs I cannot print 100 date labels. In order to face it I tried with some loops but I have an inconsistency on the time range (same space interval different time period of months). I suppose there is an easy way to do it, so if I have a column-vector x with the dates in number and y with the values. What I have to do in order to plot it?

Subject: Plot date labels

From: Justin Ashmall

Date: 9 Aug, 2012 18:06:17

Message: 2 of 16

"mklcst mklcst" wrote in message <k00t34$t35$1@newscl01ah.mathworks.com>...
> Hi everybody, I have a 2 column matrix to plot, one with the dates in number and the other column with the values. My problem is the date labels to plot on the x axis, because if I have 100obs I cannot print 100 date labels. In order to face it I tried with some loops but I have an inconsistency on the time range (same space interval different time period of months). I suppose there is an easy way to do it, so if I have a column-vector x with the dates in number and y with the values. What I have to do in order to plot it?

Providing your number-dates are Matlab datenums, just plot(x,y) as normal and then do:
datetick x % or datetick('x')

Alternatively, open the documentation with "doc" and search for something like "plot dates".

Subject: Plot date labels

From: mklcst mklcst

Date: 10 Aug, 2012 07:06:22

Message: 3 of 16

Thank you Justin.
But I don't see the label in date format as 'mmm-yy' but in numeric one.
If I set "set(gca,'XTick',x)" I see an overlapped text and plot.


"Justin Ashmall" <juestan-matlabnews6h8@yahoo.co.uk> wrote in message <k00u6p$3hq$1@newscl01ah.mathworks.com>...
> "mklcst mklcst" wrote in message <k00t34$t35$1@newscl01ah.mathworks.com>...
> > Hi everybody, I have a 2 column matrix to plot, one with the dates in number and the other column with the values. My problem is the date labels to plot on the x axis, because if I have 100obs I cannot print 100 date labels. In order to face it I tried with some loops but I have an inconsistency on the time range (same space interval different time period of months). I suppose there is an easy way to do it, so if I have a column-vector x with the dates in number and y with the values. What I have to do in order to plot it?
>
> Providing your number-dates are Matlab datenums, just plot(x,y) as normal and then do:
> datetick x % or datetick('x')
>
> Alternatively, open the documentation with "doc" and search for something like "plot dates".

Subject: Plot date labels

From: Justin Ashmall

Date: 10 Aug, 2012 10:48:09

Message: 4 of 16

"mklcst mklcst" wrote in message <k02bte$l1q$1@newscl01ah.mathworks.com>...
> Thank you Justin.
> But I don't see the label in date format as 'mmm-yy' but in numeric one.
> If I set "set(gca,'XTick',x)" I see an overlapped text and plot.

Are your dates Matlab datenums?
> > Providing your number-dates are Matlab datenums

Did you check the documentation?
> > open the documentation with "doc" and search for something like "plot dates".

Looking at the the documentation for datetick it says:
"The axis data values should be generated by or be compatible with the output of the datenum function."

I suspect your "dates in number" are some odd format and that's why it's not working. You don't give any examples so it's impossible to tell.

Subject: Plot date labels

From: mklcst mklcst

Date: 10 Aug, 2012 14:26:10

Message: 5 of 16

My dates vector in in datenum, the problem is that if I set the gca Thr plot is overlapped... I searched on DOC and also I googled it my I didn't find a solution to my problem.
So let's start with an example, suppose you have 1200 of monthly obs (for conveniente se simulate from a standard normal) and a vector of datenums from 1/1/1900 to 31/12/1999.
Can you show me how to proceed?


"Justin Ashmall" <juestan-matlabnews6h8@yahoo.co.uk> wrote in message <k02ot9$riq$1@newscl01ah.mathworks.com>...
> "mklcst mklcst" wrote in message <k02bte$l1q$1@newscl01ah.mathworks.com>...
> > Thank you Justin.
> > But I don't see the label in date format as 'mmm-yy' but in numeric one.
> > If I set "set(gca,'XTick',x)" I see an overlapped text and plot.
>
> Are your dates Matlab datenums?
> > > Providing your number-dates are Matlab datenums
>
> Did you check the documentation?
> > > open the documentation with "doc" and search for something like "plot dates".
>
> Looking at the the documentation for datetick it says:
> "The axis data values should be generated by or be compatible with the output of the datenum function."
>
> I suspect your "dates in number" are some odd format and that's why it's not working. You don't give any examples so it's impossible to tell.

Subject: Plot date labels

From: dpb

Date: 10 Aug, 2012 14:41:56

Message: 6 of 16

On 8/10/2012 9:26 AM, mklcst mklcst wrote:

[Do NOT toppost...hard conversation follow makes]

> My dates vector in in datenum, the problem is that if I set the gca Thr
> plot is overlapped......

You don't set gca, you use datetick()

doc datetick % and friends...

--

Subject: Plot date labels

From: mklcst mklcst

Date: 10 Aug, 2012 15:11:17

Message: 7 of 16

Yes I use: datetick(xData,'mmm-yy','keeplimit');
where xData is a datenum vector.
The problem is that I see in the plot the x-axis label in number

dpb <none@non.net> wrote in message <k036js$e2g$1@speranza.aioe.org>...
> On 8/10/2012 9:26 AM, mklcst mklcst wrote:
>
> [Do NOT toppost...hard conversation follow makes]
>
> > My dates vector in in datenum, the problem is that if I set the gca Thr
> > plot is overlapped......
>
> You don't set gca, you use datetick()
>
> doc datetick % and friends...
>
> --

Subject: Plot date labels

From: dpb

Date: 10 Aug, 2012 15:19:06

Message: 8 of 16

On 8/10/2012 9:26 AM, mklcst mklcst wrote:
...

> So let's start with an example, suppose you have 1200 of monthly obs
> ... and a vector of datenums from 1/1/1900 to 31/12/1999.
> Can you show me how to proceed?
...

 >> t=linspace(datenum(1900,1,1),datenum(1999,12,31),1200)';
 >> d=rand(size(t));
 >> plot(t,d)
 >> datetick('x','yyyy')

--

Subject: Plot date labels

From: dpb

Date: 10 Aug, 2012 15:46:56

Message: 9 of 16

On 8/10/2012 10:11 AM, mklcst mklcst wrote:

{TOP POSTING REPAIRED (ONCE) NO MORE RESPONSES IF YOU PERSIST)

> dpb <none@non.net> wrote in message <k036js$e2g$1@speranza.aioe.org>...
>> On 8/10/2012 9:26 AM, mklcst mklcst wrote:
...

>> You don't set gca, you use datetick()
...
 > Yes I use: datetick(xData,'mmm-yy','keeplimit');
 > where xData is a datenum vector.
...

doc datetick

NB the first argument definition per documentation.

> datetick(tickaxis) labels the tick lines of an axis using dates,
> replacing the default numeric labels. tickaxis is the string 'x',
> 'y', or 'z'. The default is 'x'.

--

Subject: Plot date labels

From: mklcst mklcst

Date: 10 Aug, 2012 17:15:23

Message: 10 of 16

ok, I get it. What about if I want to increase the number of displayed labels?

dpb <none@non.net> wrote in message <k03adt$o0s$1@speranza.aioe.org>...
> On 8/10/2012 10:11 AM, mklcst mklcst wrote:
>
> >> On 8/10/2012 9:26 AM, mklcst mklcst wrote:
> ...
>
>

Subject: Plot date labels

From: dpb

Date: 10 Aug, 2012 17:39:13

Message: 11 of 16

On 8/10/2012 12:15 PM, mklcst mklcst wrote:
> ok, I get it. What about if I want to increase the number of displayed
> labels?

doc datetick

--

Subject: Plot date labels

From: mklcst mklcst

Date: 11 Aug, 2012 07:07:09

Message: 12 of 16

I don't understand if I have to build manually the vector of dates.

dpb <none@non.net> wrote in message <k03h04$914$1@speranza.aioe.org>...
> On 8/10/2012 12:15 PM, mklcst mklcst wrote:
> > ok, I get it. What about if I want to increase the number of displayed
> > labels?
>
> doc datetick
>
> --

Subject: Plot date labels

From: Bruno Luong

Date: 11 Aug, 2012 08:22:07

Message: 13 of 16

"mklcst mklcst" wrote in message <k050as$enf$1@newscl01ah.mathworks.com>...
> I don't understand if I have to build manually the vector of dates.

t=now+(1:10);
plot(t,rand(size(t)));

set(gca,'xtick',t,'xlim',t([1 end]));
datetick('x','keeplimits','keepticks');

% Bruno

Subject: Plot date labels

From: mklcst mklcst

Date: 11 Aug, 2012 17:42:07

Message: 14 of 16

Bruno maybe you enlightened me, so if xData is my "t" (as in your example) if I code:
set(gca,'xtick',xData([1,50,100,150,300,end]),'xlim',xData([1 end]));

I enable the label of obs 1, 50, 100, 150,300, end ?




"Bruno Luong" <b.luong@fogale.findmycountry> wrote in message <k054nf$qmn$1@newscl01ah.mathworks.com>...
> "mklcst mklcst" wrote in message <k050as$enf$1@newscl01ah.mathworks.com>...
> > I don't understand if I have to build manually the vector of dates.
>
> t=now+(1:10);
> plot(t,rand(size(t)));
>
> set(gca,'xtick',t,'xlim',t([1 end]));
> datetick('x','keeplimits','keepticks');
>
> % Bruno

Subject: Plot date labels

From: Juliana Santos

Date: 4 May, 2013 14:13:09

Message: 15 of 16

"mklcst mklcst" wrote in message <k035m2$bo4$1@newscl01ah.mathworks.com>...
> My dates vector in in datenum, the problem is that if I set the gca Thr plot is overlapped... I searched on DOC and also I googled it my I didn't find a solution to my problem.
> So let's start with an example, suppose you have 1200 of monthly obs (for conveniente se simulate from a standard normal) and a vector of datenums from 1/1/1900 to 31/12/1999.
> Can you show me how to proceed?
>
>
> "Justin Ashmall" <juestan-matlabnews6h8@yahoo.co.uk> wrote in message <k02ot9$riq$1@newscl01ah.mathworks.com>...
> > "mklcst mklcst" wrote in message <k02bte$l1q$1@newscl01ah.mathworks.com>...
> > > Thank you Justin.
> > > But I don't see the label in date format as 'mmm-yy' but in numeric one.
> > > If I set "set(gca,'XTick',x)" I see an overlapped text and plot.
> >
> > Are your dates Matlab datenums?
> > > > Providing your number-dates are Matlab datenums
> >
> > Did you check the documentation?
> > > > open the documentation with "doc" and search for something like "plot dates".
> >
> > Looking at the the documentation for datetick it says:
> > "The axis data values should be generated by or be compatible with the output of the datenum function."
> >
> > I suspect your "dates in number" are some odd format and that's why it's not working. You don't give any examples so it's impossible to tell.

Hello Justin Ashmall,
I am experiencing the same problem described below. How could I get this fixed?
Cheers,
Juliana
 

Subject: Plot date labels

From: dpb

Date: 4 May, 2013 14:42:41

Message: 16 of 16

On 5/4/2013 9:13 AM, Juliana Santos wrote:
...

> I am experiencing the same problem described below. How could I get this
> fixed?
...

Well, there is no "below" but as the responses in the thread in general
note, to display dates on an axis w/ Matlab,

a) generate the subject axis values via datenum() and friends, and

b) utilized datetick() on the proper axis w/ format and all as desired

doc datenum % and friends
doc datetick

No specifics shown, no specifics in response are possible...

--

Tags for this Thread

What are tags?

A tag is like a keyword or category label associated with each thread. Tags make it easier for you to find threads of interest.

Anyone can tag a thread. Tags are public and visible to everyone.

Contact us