Why is my plot not showing years on the x axis but random numbers and what code do i use to make it years?
2 ビュー (過去 30 日間)
古いコメントを表示
In the screen shot my plot has got random numbers on the x axis instead of years, what code do i use to change this?
![](https://www.mathworks.com/matlabcentral/answers/uploaded_files/168860/image.png)
4 件のコメント
KSSV
2017 年 10 月 27 日
plot(cum_ret_oos)
YOu have not provided x values..so the plot is w.r.t to indices....
採用された回答
KSSV
2017 年 10 月 27 日
It should have got plotted w.r.t to indices....you need to change the x-axis ticks to the years you want. Read about datetick, datenum.
その他の回答 (1 件)
Steven Lord
2017 年 10 月 27 日
Here's a small example you can use as a model for your own code.
% Read in some sample data.
% This contains two variables: cdate (year numbers) and pop (population)
load census
% Turn the year numbers into a datetime array
x = datetime(cdate, 1, 1);
% Plot it. Note that the X axis contains year numbers
h = plot(x, pop);
% If you want finer-grained control of the formatting on the axes
% and you're using release R2016a or later, get the axes ruler
ax = ancestor(h, 'axes');
xrule = ax.XAxis;
% The xrule ruler has a number of properties. Let's customize the format.
%
% After running the line below the axes should show the ticks as
% Jan1xxx (1840, 1880, 1920, etc.) because the x vector I created
% contains January 1st for years that are multiples of 10
xrule.TickLabelFormat = 'MMMyyyy';
0 件のコメント
参考
Community Treasure Hunt
Find the treasures in MATLAB Central and discover how the community can help you!
Start Hunting!