python - 在 SymPy 中绘制 3D 点列表

标签 python matplotlib scipy ipython

如果我有 3D 点列表,请说

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

我想使用 SciPy 来绘制它们,我该怎么做?我试过了

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

但这会导致此错误:

---------------------------------------------------------------------------
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

最理想的解决方案是用这些顶点绘制一个多面体(四面体),或者用线段连接它们。

最佳答案

您可以使用scatter绘制这些点。如果您想用线段连接它们,一种可能的方法是首先绘制点,然后连接它们,如下所示:

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

关于python - 在 SymPy 中绘制 3D 点列表,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35452764/

相关文章:

python - pandas 在 groupby sum 之后对每组内的值进行排序,并在使用 cumsum 后获取值的百分比

matplotlib - 使用 Cartopy 添加网格线

python - Pandas DataFrame.apply 对于 scipy.stats 来说非常慢

python - 将图像分成 block

python - Django 模型 : Default for Choice Foreign Key

python - python plot 中的一个错误

python - matplotlib 不同数据点的不同颜色

python - 线性回归 ODR 失败

python - 无法合并两个已排序的单链表,因为 "type object ' _Node' 没有属性 '_element' "