Search code examples
pythonpandasdataframematplotlibsurface

Plotting 3D surface with matplotlib with 3 columns of data


I know usually we use:

plot_surface(X, Y, Z)

where X, Y are 1-D array and Z a matrix of dimension len(X) × len(Y), to plot a surface.

But I now have only three columns of 1-D array as data. In other words, I have something like:

X=[1,2,3,4,5]
Y=[1,2,3,4,5]

and

Z=f(X,Y)=[1,2,3,4,5]

I do not have the expression of f, but I want to plot a surface diagram nevertheless. How should I do it?

Please feel free to ask me to clarify my question. This is my first time asking questions on the platform.

Thank you in advance!


Solution

  • is this what you mean?:

    import numpy as np
    import matplotlib.pyplot as plt
    
    x=[1,2,3,4,5]
    y=[1,2,3,4,5]
    z=np.array([[1],[2],[3],[4],[5]])
    ax = plt.axes(projection='3d')
    ax.plot_surface(x, y, z,cmap='viridis')
    
    plt.show()