pltoting a variable with dates on horizontial axis

조회 수: 8 (최근 30일)
Muhammad Ramzan
Muhammad Ramzan 2018년 9월 5일
댓글: jonas 2018년 9월 13일
Hello,
I am importing a data from excel by clicking on excel file and than click import.
the first column in my data is date, that is year , quarter. I want to create a plot of the variables. that i have done but in the figure i want to have date on the horizontal axis. I am having trouble in having date on the horizontal axis.
and i want to shade some of the periods in my plot. like from 1948 to 1950.
Please help me how i can do that.
  댓글 수: 2
jonas
jonas 2018년 9월 5일
Please upload the file
Stephen23
Stephen23 2018년 9월 6일
Muhammad Ramzan's "Answer" moved here:
I want to get the picture like this. Please let me now who i can do this. thanks

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

답변 (2개)

jonas
jonas 2018년 9월 5일
편집: jonas 2018년 9월 12일
Here is something you can work from. I used a trick with the stairs and area functions to make the shaded bars. It may have been a bit overkill to use datetime in this case, but I could not resist. You can skip the first part if you'd prefer to use fraction years.
% load data
data=xlsread('MacroData.xlsx');
t=data(:,1);
y=data(:,7)
% Convert to datetime
year = floor(t);
fyear = years(mod(t,1));
ts=datetime(year,1,1)+fyear;
%Plot data
figure
hold on
h=plot(ts,y)
% fancy some shading? write list of [start time1; end time1; start time2; end time2...] and so on:
t2=[11 1948;10 1949;7 1953;5 1954;6 1957;4 1958;4 1960;2 1961;12 1969;11 1970;11 1973;3 1975;1 1980;7 1980;7 1981;11 1982;7 1990;3 1991;3 2001;11 2001;12 2007;6 2009]
t2=datetime(0,t2(:,1),1)+years(t2(:,2))
y2=zeros(1,length(t2))
y2(1:2:end)=1e6
% Fix shading
[xs,ys]=stairs(t2,y2)
ha=area(xs,ys,'facecolor',[0 0 0],'facealpha',.5,'edgecolor','none')
ylim([min(y) max(y)])
EDIT: Fixed an error found by Peter Perkins
  댓글 수: 11
Muhammad Ramzan
Muhammad Ramzan 2018년 9월 9일
@ Walter Roberson I want to have years on x-axis like from 1948 to 2011. Please help
Walter Roberson
Walter Roberson 2018년 9월 9일
mydata = xlsread('MacroData.xlsx');
t = mydata(:,1);
TIME = datenum([floor(t), round(mod(t,1)*30-2), 0*t+1]); %converts quarters from .1, .2, .3, .4 into appropriate months
y = mydata(:,2);
myrecessions = {'November 1948' , 'October 1949' ; 'July 1953', 'May 1954' ; 'August 1957', 'April 1958' ; 'April 1960' , 'February 1961' ; 'December 1969', 'November 1970' ; 'November 1973', 'March 1975' ; 'January 1980', 'July 1980' ; 'July 1981' , 'November 1982' ; 'July 1990' , 'March 1991' ; 'March 2001' , 'November 2001' ;
'December 2007' , 'June 2009' };
recessions_datenum = reshape(datenum(myrecessions(:), 'mmmm yyyy'),size(myrecessions));
figure(3)
plot(TIME,y)
recessionplot('recessions', recessions_datenum);
datetick('x','yyyy')
xlim(datenum([1948 1 1; 2011 12 31]))
Tested. But you should re-check which column of data you want to plot: before it was column 7 but you seem to have switched to column 2.

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


Peter Perkins
Peter Perkins 2018년 9월 12일
If possible, use datetimes, not datenums. Jonas' answer seems good, except I think it isn't correct when dealing with the non-standard way that quarters are encoded as fractional years (as tenths!). I thik this would do it:
y = floor(t);
q = 10*years(mod(t,1)); % maybe even round this?
ts = datetime(y,1,1) + calquarters(q);
  댓글 수: 4
Walter Roberson
Walter Roberson 2018년 9월 13일
The input data had year numbers and .1 .2 .3 .4 but no .5 or higher fraction, and the question stated "quarters".
jonas
jonas 2018년 9월 13일

Thanks guys, I did not realize the fraction refered to quarter.

@Peter: Thanks a lot for the elaborate explanation. Did not know about calyear, which seems great for dealing with those annoying leap years!

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

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by