3d plot with given 2d data

前端 未结 1 1183
花落未央
花落未央 2021-01-25 16:41

I want to understand how the 2d data is related to z axis to get the 3d plots

let us say that i have x=[-1:0.1:1], vector

and y=[1 2 3 4 5 4

相关标签:
1条回答
  • 2021-01-25 17:16

    You can view a line/curve plot as a function of a single variable, y=f(x), and typically, x and y are both vectors. For e.g., you can plot the Gaussian bell curve as

    x=linspace(-3,3,1000);
    y=exp(-x.^2/2);
    plot(x,y)
    

    A surface plot, on the other hand, is a function of two variables, z=f(x,y) where x and y can be either vectors or matrices and z is a matrix. meshgrid is a very handy function that generates 2D x and y arrays from 1D vectors by proper replication.

    It is the z matrix that you plot either as a 2D image (values of z are represented by colors) or a 3D plot (values of z are represented as heights along the z-axis). For e.g., a 3D Gaussian bell curve can be plotted as

    x=linspace(-3,3,1000);y=x';               %'
    [X,Y]=meshgrid(x,y);
    z=exp(-(X.^2+Y.^2)/2);
    surf(x,y,z);shading interp
    

    This is how the respective plots should look like

    enter image description here

    0 讨论(0)
提交回复
热议问题