How to change axis tick values to specific dates?

1 回表示 (過去 30 日間)
Liwei Dai
Liwei Dai 2015 年 6 月 16 日
回答済み: Walter Roberson 2015 年 6 月 16 日
Hello, everybody, thanks for helping me on this. As I'm trying to reproduce a figure from a paper like this: it's a simple Monte Carlo simulated cumulative return series with 200 simulations spanning 10 years for daily returns.
In this figure as you see, the x-axis is labeled with years: 2004~2013, how do I reproduce something like this? I've tried the following code for simulation, and get the result, but the x-axis is not the years, just the indices for the returns, how to modify this?
>> mu = 0; sigma = 0.15; dt = 1/255; N = 10*255; nu = mu - sigma^2/2;
>> nudt = nu*dt;
>> sigmasqrtdt = sigma * sqrt(dt);
>> figure
>> M = 200;
>> plot(nudt.*cumsum(ones(N,M)) + sigmasqrtdt.*cumsum(randn(N,M)))
Resulting figure:
Besides, I noticed that the color set is different in the two figure, the top one has the order from red to yellow to blue as the cumulative returns goes down. But mine is random, could you help me to fix this, too? Thanks a lot.

採用された回答

Walter Roberson
Walter Roberson 2015 年 6 月 16 日
%do the calculations
cumreturns = nudt.*cumsum(ones(N,M)) + sigmasqrtdt.*cumsum(randn(N,M));
%find the year for each location
years = linspace(2004,2014,N+1); %the very next point would be 2014
years = years(1:end+1); %but the last used point does not reach 2014
%do the plot
linehandles = plot(years, cumreturns);
set(linehandles, 'xtick', 2004:2014);
%coloring will be by final value, so retrieve them and sort
final_returns = cumreturns(end,:);
[sorted_final, sortidx] = sort(final_returns, 'stable');
%map so lowest value is first color, highest value is last
%we probably have more lines than available colors so some will be duplicated
cmap = colormap();
numcolors = size(cmap,1);
coloridxmap = round(linspace(1,numcolors,length(sortidx)));
coloridx = coloridxmap(sortidx);
%put the color setting into effect
for K = 1 : length(linehandles)
set(linehandles(K), 'Color', cmap(coloridx,:));
end

その他の回答 (1 件)

arun
arun 2015 年 6 月 16 日
I think u have plot ur y value vs index value. I hope length of x axis is 2550 else check that using size() or length() Here u can do one mapping from one axis to another,
m*(2550-1)=(2014-2004)
calculate "m" and and multiply that with index number i.e (1:2550) and plot that resultant vector against ur y value.

カテゴリ

Help Center および File ExchangeGrid Lines, Tick Values, and Labels についてさらに検索

Community Treasure Hunt

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

Start Hunting!

Translated by