Change plot X-axis values and adjustments to the graph

Hey everyone!
I have a task to write a command which replicates a graph. The file "SPX" contains two columns with date info in the first column and daily closing values in the second column.
I am very close to succeeding; my line of code I've written so far is as follows:
h = plot(SPX(:,1),SPX(:,2),'LineWidth',0.4,'Color',[0 0.5 0.5])
and this line of code produces the following graph:
However, my graph should eventually look like is this:
As you can see, my graph is missing the correct years (1990, 1995, 2000 etc.) on the X-axis. Also, my graph should go from end to end (i.e. no empty space), which is does not do.
I'd be super happy with some advice/hints on how I can change my line of code to achieve this. Thank you!
Best regards
Daniel

댓글 수: 1

I tried to improve it by changing the code like this, but didn't get it corrected:
h = plot(SPX(:,1),SPX(:,2),'LineWidth',0.4,'Color',[0 0.5 0.5]);
xticks(SPX(:,1),[7.26 7.28 7.32 7.34 7.36 7.38])
xticklabels(SPX(:,1),{'x = 1990','x = 1995','x = 2000','x = 2005','x = 2010','x = 2015'})

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

답변 (1개)

Steven Lord
Steven Lord 2019년 9월 30일
Instead of plotting using date numbers, if you're using a release that supports them store your date and time data as a datetime array.
t = 0:20;
x = datetime('today') + days(t);
y = t.^2;
h = plot(x, y);
You can control the format of the ticks on the axes using xtickformat as shown on this documentation page.

댓글 수: 1

Thank you for the help, Steven!
I managed to add the years correctly on the X-Axis:
h = plot(SPX(:,1),SPX(:,2),'LineWidth',0.4,'Color',[0 0.5 0.5]);
datetick('x','YYYY')
I added the "datetick('x' , 'YYYY')" and this did the trick.
The last thing I would need to do is remove the space from both sides between the Y-Axis and my graph line. If you have any suggestions on how to do this, I'd be very thankful.
Best regards
Daniel

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

카테고리

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

제품

태그

질문:

2019년 9월 30일

댓글:

2019년 9월 30일

Community Treasure Hunt

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

Start Hunting!

Translated by