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.

 채택된 답변

Star Strider
Star Strider 2016년 3월 8일
편집: Star Strider 2016년 3월 8일

1 개 추천

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')

댓글 수: 8

Rita
Rita 2016년 3월 8일
편집: Rita 2016년 3월 8일
Thanks.If I have 6 years of daily data what number should be replaced instead of 2016? for example size of dn =2191 and data from 2001-2006
Star Strider
Star Strider 2016년 3월 8일
My pleasure. I apologise for the delay — today is errand day and I was away for a while.
Your ‘test.mat’ file has three variables, ‘A’, ‘B’ and ‘dn’. I did some initial plots of them individually, and then plotted them used the first column of ‘A’ and ‘B’ respectfully as indices into ‘dn’. I had problems with the plot of them as continuous lines because they weren’t sorted.
What do the ‘A(:,1)’ and ‘B(:,1)’ represent? How do they correspond with the elements of ‘dn’?
Rita
Rita 2016년 3월 9일
편집: Rita 2016년 3월 9일
Thanks Star Strider.the first column of A and B is day of years (6 years) and the second column of A is flux which measured and the second column of B is the flux which calculated each day if I did not measure the fux I have to calculate it therefore the flux of very day has been calculated or measured .dn is based on your comment I created but I am not sure how I should define years from 2001 to 2006 instead of just 2016. Thanks again
Star Strider
Star Strider 2016년 3월 9일
My pleasure.
O.K. Now I understand the reason ‘dn’ isn’t mapping correctly.
So your data observation days begin on 2001-01-01 and go consecutively and continuously without any interruptions, correct? I need to know exactly when they start and exactly when they end. I’ll create a new ‘dn’ vector once I get the necessary information.
I’m in the UTC-7 time zone, so this is the end of my day. I will wait for you to provide the information I requested. Once I have it, I’ll get on it and post back here.
Rita
Rita 2016년 3월 9일
편집: Rita 2016년 3월 9일
Thanks star.The start date is 2001/1/1 and end date is last day of 2006 (2006/12/31)continuously.Goodnight.
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
Rita 2016년 3월 9일
Thank you so much.It is exactly what I need.I really appreciate your great help.
Star Strider
Star Strider 2016년 3월 9일
As always, my pleasure!

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

추가 답변 (1개)

Chad Greene
Chad Greene 2016년 3월 8일

0 개 추천

Is this what you want?
set(gca,'xtick',1:8,'xticklabel',{'jan','feb','mar','apr','may','jun','jul','aug'})

카테고리

도움말 센터File Exchange에서 Annotations에 대해 자세히 알아보기

태그

질문:

2016년 3월 8일

댓글:

2016년 3월 9일

Community Treasure Hunt

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

Start Hunting!

Translated by