Langston
Langston

Reputation: 1093

Plotting a list of 3D points in SymPy

If I have a list of 3D points, say

pts = [(0,0,0),(1,0,0),(0,2,0),(0,0,3)]

and I wanted to use SciPy to plot them, how would I do that? I've tried

plot3d([Point(0,0,0),Point(1,0,0),Point(0,2,0),Point(0,0,3)], (x, -5, 5), (y, -5, 5))

but that results in this error:

---------------------------------------------------------------------------
TypeError                                 Traceback (most recent call last)
<ipython-input-54-851ee4534010> in <module>()
----> 1 plot3d([Point(0,0,0),Point(1,0,0),Point(0,2,0),Point(0,0,3)], (x, -5, 5), (y, -5, 5))

/usr/lib/python3.5/site-packages/sympy/plotting/plot.py in plot3d(*args, **kwargs)
   1618     series = []
   1619     plot_expr = check_arguments(args, 1, 2)
-> 1620     series = [SurfaceOver2DRangeSeries(*arg, **kwargs) for arg in plot_expr]
   1621     plots = Plot(*series, **kwargs)
   1622     if show:

TypeError: 'NoneType' object is not iterable

Optimally, the solution would plot a polyhedron (tetrahedron) with those verticies, or connect them with line segments.

Upvotes: 1

Views: 4519

Answers (1)

adrianus
adrianus

Reputation: 3199

You can plot these points with scatter. If you want to connect them with line segments, one possible way would be to first plot the points and then connect them, like this:

import matplotlib as mpl
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
import matplotlib.pyplot as plt
import itertools

fig = plt.figure()
ax = fig.gca(projection='3d')

# plotting the points
pts = [(0,0,0),(1,0,0),(0,2,0),(0,0,3)]
for p in pts:
    ax.scatter(p[0], p[1], p[2], zdir='z', c='r')

# plotting lines for each point pair
for a, b in itertools.product(pts, pts):
    x = np.linspace(a[0], b[0], 100)
    y = np.linspace(a[1], b[1], 100)
    z = np.linspace(a[2], b[2], 100)
    ax.plot(x, y, z)

ax.legend()
ax.set_xlim3d(0, 1)
ax.set_ylim3d(0, 2)
ax.set_zlim3d(0, 3)

plt.show()

Plot

Upvotes: 1

Related Questions