Change plot X-axis values and adjustments to the graph
16 views (last 30 days)
Show older comments
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:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240568/image.png)
However, my graph should eventually look like is this:
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/240569/image.png)
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
Answers (1)
Steven Lord
on 30 Sep 2019
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.
See Also
Categories
Find more on 2-D and 3-D Plots in Help Center and File Exchange
Products
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!