Matlab:在2D绘图中添加第三维

时间:2012-07-24 12:29:13

标签: matlab 3d plot 2d

我有以下代码来生成2D绘图或2个正态分布:

        res = zeros(2, 320); 
        index = 1:320;

        % assign some data to the res array and then approximate:  

        PD = fitdist(index','normal', 'frequency', res(1,:)')
        pdfNormal = normpdf(index',PD.mu,PD.sigma);
        plot(index', pdfNormal, 'Color', 'r', 'LineWidth', 2);
        hold on;
        PD = fitdist(index','normal', 'frequency', res(2,:)')
        pdfNormal = normpdf(index',PD.mu,PD.sigma);
        plot(index', pdfNormal, 'Color', 'b', 'LineWidth', 2);            

这段代码生成了下面的图片:

2D Plot of my data

现在我想知道如何在这个情节中添加第三个维度?实质上, 我想绘制另外2个正态分布,但这次是在Z轴上, 即,在第三维度。

任何人都知道如何轻松地做到这一点?

非常感谢!

1 个答案:

答案 0 :(得分:4)

如果我理解正确,您可以简单地给出不同的z值。例如:

%# some random data
x = 1:300;
y = zeros(5,300);
for i=1:5
    y(i,:) = normpdf(x,100+i*20,10);
end

%# plot
hold on
clr = lines(5);
h = zeros(5,1);
for i=1:5
    h(i) = plot(x, y(i,:), 'Color',clr(i,:), 'LineWidth',2);
    set(h(i), 'ZData',ones(size(x))*i)
end
zlim([0 6]), box on, grid on
view(3)
hold off

screenshot