Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Plot a 3D surface from {x,y,z}-scatter data in python

I'm trying to plot a 3D surface constructed to fit some {x,y,z} points in python -- ideally something like the Mathematica ListSurfacePlot3D function. Thus far I've tried plot_surface and plot_wireframe on my points to no avail.

Only the axes render with plot_surface. plot_wireframe gives a bunch of squigglys, vaguely in the shape of the object, but not the nice sort that is shown in the documentation: enter image description here Compare to the result from ListSurfacePlot3D: enter image description here

Here is a minimal working example, using a test.csv file I posted here:

import csv
from matplotlib import pyplot
import pylab
from mpl_toolkits.mplot3d import Axes3D

hFile = open("test.csv", 'r')
datfile = csv.reader(hFile)
dat = []

for row in datfile:
        dat.append(map(float,row))

temp = zip(*(dat))

fig = pylab.figure(figsize=pyplot.figaspect(.96))
ax = Axes3D(fig)

Then, either

ax.plot_surface(temp[0], temp[1], temp[2])
pyplot.show()

or

ax.plot_wireframe(temp[0], temp[1], temp[2])
pyplot.show()

This is how it renders using plot_surface: enter image description here and using plot_wireframe: enter image description here and using ListSurfacePlot3D: enter image description here

like image 231
Eli Lansey Avatar asked Jun 28 '13 14:06

Eli Lansey


People also ask

How do you plot a scatter 3D in python?

Generally 3D scatter plot is created by using ax. scatter3D() the function of the matplotlib library which accepts a data sets of X, Y and Z to create the plot while the rest of the attributes of the function are the same as that of two dimensional scatter plot.

How do you make a 3D scatter plot?

After adding data, go to the 'Traces' section under the 'Structure' menu on the left-hand side. Choose the 'Type' of trace, then choose '3D Scatter' under '3D' chart type. Next, select 'X', 'Y' and 'Z' values from the dropdown menus. This will create a 3D scatter trace, as seen below.


1 Answers

plot_surface expects X,Y,Z values in the form of 2D arrays, as would be returned by np.meshgrid. When the inputs are regularly gridded in this way, the plot function implicitly knows which vertices in the surface are adjacent to one another and therefore should be joined with edges. In your example, however, you're handing it 1D vectors of coordinates, so the plotting function would need to be able to figure out which vertices should be joined.

The plot_trisurf function does handle irregularly spaced points by doing a Delaunay triangulation to determine which points should be joined with edges in such a way as to avoid 'thin triangles':

enter image description here

like image 107
ali_m Avatar answered Sep 19 '22 13:09

ali_m