필터 지우기
필터 지우기

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?
  댓글 수: 4
LM
LM 2017년 10월 27일
편집: LM 2017년 10월 27일
This is my code.
%plot cumulative return of four trading stratergies
plot(cum_ret_oos)
% Create xlabel
xlabel('Years out sample')
% Create ylabel
ylabel('Cumulative Returns (logs)')
% Create title
title('Cumulative Returns of 4 Trading Stratergies')
% set the y-axis scale to log
set(gca,'Yscale','log')
KSSV
KSSV 2017년 10월 27일
plot(cum_ret_oos)
YOu have not provided x values..so the plot is w.r.t to indices....

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

채택된 답변

KSSV
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
LM
LM 2017년 10월 27일
편집: LM 2017년 10월 27일
I have tried both date tick and datenum but both have not given me dates, why not?
%plot cumulative return of four trading stratergies
plot(cum_ret_oos)
startDate = datenum('01-01-2013');
endDate = datenum('30-06-2015');
xData = linspace(startDate,endDate,30);
% Create xlabel
xlabel('Years out sample')
% Create ylabel
ylabel('Cumulative Returns (logs)')
% Create title
title('Cumulative Returns of 4 Trading Stratergies')
% set the x-axis scale to log
set(gca,'Yscale','log')
or using date tick in the second screen shot

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

추가 답변 (1개)

Steven Lord
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';

카테고리

Help CenterFile Exchange에서 2-D and 3-D Plots에 대해 자세히 알아보기

Community Treasure Hunt

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

Start Hunting!

Translated by