Asked by Rita
on 8 Mar 2016

I have two matrices to plot

A=[1 1.2

3 2.3

7 3.2

8 3 ]

and

B=[2 3.2

4 4.5

5 7

6 0 ]

I want to create a plot

plot(A(:,1),A(:,2),'r');

hold on

plot(B(:,1),B(:,2),'b');

but instead of x it should be month. Any suggestion would be appreciated in advance.

Answer by Star Strider
on 8 Mar 2016

Edited by Star Strider
on 8 Mar 2016

Accepted Answer

One approach that will use local month names:

A=[1 1.2

3 2.3

7 3.2

8 3 ]

B=[2 3.2

4 4.5

5 7

6 0 ]

dn = datenum([repmat(2016, size(A,1)+size(B,1), 1) [A(:,1); B(:,1)], ones(size(A,1)+size(B,1), 1)]); % Create Date Numbers From Months

plot(dn(1:size(A,1)) ,A(:,2),'r');

hold on

plot(dn(size(A,1)+1:end),B(:,2),'b');

datetick('x', 'mmm')

Star Strider
on 9 Mar 2016

My pleasure.

I’m not quite certain what you want, so I did two plots:

D = load('Rita test.mat');

A = D.A;

B = D.B;

% ‘The start date is 2001/1/1 and end date is last day of 2006 (2006/12/31) continuously.’

dn = datenum([2001 01 01]) + [0:(size(A,1)+size(B,1))-1]; % Recalculate ‘dn’

DateA = A(:,1); % First Column Are Indices Into ‘dn’

DateB = B(:,1);

figure(1) % Plot ‘A’ & ‘B’ As Separate Series

plot(dn(DateA), A(:,2), '-')

hold on

plot(dn(DateB), B(:,2), '-')

hold off

grid

set(gca, 'XTickLabelRotation',30, 'FontSize',8)

datetick('x', 'mmm yy')

AcatBsorted = sortrows([A; B], 1); % Concatenate & Sort ‘A’ & ‘B’

figure(2) % Plot ‘A’ & ‘B’ Together

plot(dn(AcatBsorted(:,1)), AcatBsorted(:,2))

grid

set(gca, 'XTickLabelRotation',30, 'FontSize',8)

datetick('x', 'mmm yy')

I don’t know what date resolution you want. I cannot get datetick to plot more ticks even when I tell it to. That works without using datetick, but datetick refuses to plot mid-year dates as well. We can do this with a text call if you want to. If the datetick defaults work for you, I’ll not pursue this further.

Rita
on 9 Mar 2016

Thank you so much.It is exactly what I need.I really appreciate your great help.

Star Strider
on 9 Mar 2016

As always, my pleasure!

Sign in to comment.

Answer by Chad Greene
on 8 Mar 2016

Is this what you want?

set(gca,'xtick',1:8,'xticklabel',{'jan','feb','mar','apr','may','jun','jul','aug'})

Sign in to comment.

Opportunities for recent engineering grads.

Apply Today
## 0 Comments

Sign in to comment.