7

How to do something like this in matplotlib but not with points but with surface? (I have coordinates of points)

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

#X = ...
#Y = ... Some coordinates points from file it is list
#Z = ...

fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')

ax.scatter(X, Y, Z, c='r')
ax.set_xlabel('X Label')
ax.set_ylabel('Y Label')
ax.set_zlabel('Z Label')

plt.show()
  • 2
    possible duplicate of [surface plots in matplotlib](http://stackoverflow.com/questions/9170838/surface-plots-in-matplotlib) – Bas Jansen Aug 19 '15 at 08:13

1 Answers1

-1

You will have to use the plot_surface command from your ax object. I will present a code snippet from my own plotting library, you will just have to refactor it as needed. However, this (or highly similar) questions have been asked before (e.g. surface plots in matplotlib).

from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt

def surface_plot(X,Y,Z,**kwargs):
    """ WRITE DOCUMENTATION
    """
    xlabel, ylabel, zlabel, title = kwargs.get('xlabel',""), kwargs.get('ylabel',""), kwargs.get('zlabel',""), kwargs.get('title',"")
    fig = plt.figure()
    fig.patch.set_facecolor('white')
    ax = fig.add_subplot(111, projection='3d')
    ax.plot_surface(X,Y,Z)
    ax.set_xlabel(xlabel)
    ax.set_ylabel(ylabel)
    ax.set_zlabel(zlabel)
    ax.set_title(title)
    plt.show()
    plt.close()

PS: There has been a recent string of down votes on this answer. Therefore I will add that the original question was badly formulated and that this answer is exactly what the OP wanted based on a private conversation with the OP.

Bas Jansen
  • 3,273
  • 5
  • 30
  • 66
  • 13
    Throws an error : `Argument Z must be 2-dimensional` – mrgloom Feb 20 '18 at 09:40
  • @mrgloom That is correct, the data you are using must not be in the correct format for surface_plot. – Bas Jansen Feb 25 '18 at 18:31
  • I would like to know why this got a downvote, years after being an accepted answer. – Bas Jansen Jul 23 '18 at 14:17
  • 3
    @BasJansen Since in your answer you assume every pair of (x_i, y_j) holds a corresponding Z value, which means Z is a 2D array. While the question is "from points coordinates", which means there is no 2D array for Z at all... – Kite.Y Nov 18 '18 at 22:48
  • @Kite.Y There has to be a Z for each coordinate, otherwise it wouldn't be a surface plot. – Bas Jansen Dec 06 '18 at 09:26
  • 2
    @BasJansen Of course there should be a Z. The point is the coordinates (X,Y) might not be a grid, in which case you have a Nx3 arrays with N (x,y,z) coodinates, and Z is not a 2D-array --- that's the original question, though... – Kite.Y Dec 07 '18 at 15:20
  • 5
    I support the down vote. The question asked for a coordinates based solution with the same interface as the `scatter` function. @BasJansen, you just provided a fancy way to call `plot_surface` which is not the same thing – Ido_f May 17 '19 at 16:25
  • The original question was badly formulated by the OP, the answer I gave was provided based on a private conversation with the OP and it did what the OP wanted. That's all that I will say versus all the 'recent' down votes on an accepted answer from years ago. – Bas Jansen Dec 17 '19 at 21:28