how to plot the time series data with dates showing in x axis

조회 수: 3 (최근 30일)
Qian cao
Qian cao 2016년 1월 11일
편집: Walter Roberson 2016년 1월 11일
Hi all, I am trying to plot the monthly time series in the same graph with x axis representing month of year and y axis the time series of data1 and data2 . In the original excel file tss.xlsx are 3 sheets, each has one column(303 rows) of data with names as sheet1:dates( from 07.1990 to 09.2015), sheet2: data1, sheet3:data2. Data is shown as below
tss=
1990/7/31 1.007180601 0.955050804
1990/8/31 0.986132061 0.930056005
1990/9/28 1.143781772 0.944318813
1990/10/31 1.180854071 0.970127084
1990/11/30 1.144684607 0.926718054
1990/12/31 0.958882349 0.870540688
....
2015/7/30 0.800130487 0.819143168
2015/8/31 0.787216726 0.862432198
2015/9/28 0.904850084 0.948935783
I first import the data into matlab and further run the codes as follows
dates=xlsread('tss.xlsx','dates');
data1=xlsread('tss.xlsx','data1');
data2=xlsread('tss.xlsx','data2');
plot(tss(1:303,1),data1,tss(1:303,1),data2)
datetick('x', 'mmmyy', 'keeplimits', 'keepticks')
legend('ts of data1','ts of data2')
grid on
grid minor
I am not satisfied with plotting 1) The x axis does show every year.. I need an x.axis which is labeled from year 1990 to year 2015, with grid of 1 year only. 2) The x axis begins from year 1987 and ends at 2020, but I want the plotting to start in 1990 and ends at 2015. Could you help me to figure out the above questions please? Thank you very much.
Best Jessie

답변 (1개)

Titus Edelhofer
Titus Edelhofer 2016년 1월 11일
Hi Jessie,
you need to set those properties as you want them to be before calling datetick:
t = datenum(1990, 1, 1):25:datenum(2015, 12, 15);
plot(t, rand(size(t)));
xlim([datenum(1990,1,1) datenum(2015, 12, 31)])
set(gca, 'xtick', datenum(1990:2015, 1, 1))
datetick('x', 'mmmyy', 'keeplimits', 'keepticks')
should do about what you want (or if not give hints how to get there :)).
Titus

카테고리

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

태그

Community Treasure Hunt

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

Start Hunting!

Translated by