简体   繁体   中英

Plotting a 3D graph of normalized prices in MatLab

I'm doing Gaussian processes and I calculated a regression per year from a given matrix where each row represents a year , so the code is:

M1 = MainMatrix; %This is the given Matrix 
ker =@(x,y) exp(-1013*(x-y)'*(x-y)); 
[ns, ms] = size(M1);

for N = 1:ns
    x = M1(N,:);
    C = zeros(ms,ms);
    for i = 1:ms 
        for j = 1:ms 
            C(i,j)= ker(x(i),x(j));
        end
    end

    u = randn(ms,1); 
    [A,S, B] = svd(C);
    z = A*sqrt(S)*u; % z = A S^.5 u 

And I wanna plotting each regression in a Graph 3D as the below:

在此处输入图片说明

I know that plot is a ribbon , but I have not idea how can I do that

The desired plot can be generated without the use of ribbon . Just use a surf -plot for all the prices and a fill3 -plot for the plane at z=0 . The boundaries of the plane are calculated from the actual limits of the figure. Therefore we need to set the limits before plotting the plane. Then just some adjustments are needed to generate almost the same appearance.

Here is the code:

% generate some data
days  = (1:100)';
price = days*[0.18,-0.08,0.07,-0.10,0.12,-0.08,0.05];
price = price + 0.5*randn(size(price));
years = 2002+(1:size(price,2));

% prepare plot
width = 0.6;
X = ones(size(price,1),1)*0.5;
X = [-X,X]*width;
figure; hold on;

% plot all 'ribbons'
for i = 1:size(price,2)
    h = surf([days,days],X+years(i),[price(:,i),price(:,i)]);
    set(h,'MeshStyle','column');
end

% set axis limits
set(gca,'ZLim',[-20,20]);

% plot plane at z=0
limx = get(gca,'XLim');
limy = get(gca,'YLim');
fill3(reshape([limx;limx],1,[]),[flip(limy),limy],zeros(1,4),'g','FaceAlpha',0.2)

% set labels
xlabel('Day of trading')
ylabel('Year')
zlabel('Normalized Price')

% tweak appearance
set(gca,'YTick',years);
set(gca,'YDir','reverse');
view([-38,50])
colormap jet;
grid on;
%box on;

This is the result: RESULT1

这是一个ribbon图,在y = 0处有一个附加曲面,可以用fill3绘制fill3

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM