Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Matlab: Adding third dimenstion to 2D plot

Tags:

plot

matlab

2d

3d

I have the following code to generate me a 2D plot or 2 normal distributions:

        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);            

This code generates me then the following picture:

2D Plot of my data

Now I am wondering how I could add a third dimension to this plot? Essentially, I would like to plot another 2 normal distributions, but this time in the Z-axis, ie., in the third dimension.

Anyone an idea how I could do that easily?

Thanks so much!

like image 671
Patrick Avatar asked Jul 22 '26 12:07

Patrick


1 Answers

If I understood correctly, you can simply give the plots different z-values. Example:

%# 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

like image 172
Amro Avatar answered Jul 25 '26 06:07

Amro



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!