Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/user-interface/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 3.x 三维绘图仪仅适用于某些形状_Python 3.x_Matplotlib - Fatal编程技术网

Python 3.x 三维绘图仪仅适用于某些形状

Python 3.x 三维绘图仪仅适用于某些形状,python-3.x,matplotlib,Python 3.x,Matplotlib,问题是该脚本无法绘制球体,例如,它可以绘制多个圆锥体,如脚本中的圆锥体 我已经更改了形状,并尝试使用绘制球体时给出的错误消息查找错误来源的直线 import sympy as sy import numpy as np import matplotlib.pyplot as plt from mpl_toolkits import mplot3d # Plot Figure nd = 50 # Number of points in graph ax = plt.axes(projecti

问题是该脚本无法绘制球体,例如,它可以绘制多个圆锥体,如脚本中的圆锥体

我已经更改了形状,并尝试使用绘制球体时给出的错误消息查找错误来源的直线

import sympy as sy
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits import mplot3d

# Plot Figure
nd = 50  # Number of points in graph

ax = plt.axes(projection='3d')  # Adds 3d axis to figure

x1 = np.linspace(-15, 15, nd)
y1 = np.linspace(-15, 15, nd)

X, Y = np.meshgrid(x1, y1)  # Create 2D grid with x1 and y1

i = 0
a = 0
b = 0
Z = np.array([])

x = sy.Symbol('x')
y = sy.Symbol('y')
z = (x**2+y**2)**0.5  # Function goes here

for i in range(nd):  # Iterate over rows
    b = 0
    xv1 = X[a, :]
    yv1 = Y[a, :]
    for i in range(nd):  # Iterate over elements in one row
        xv = xv1[b]
        yv = yv1[b]
        z1 = z.subs([(x, xv), (y, yv)])
        Z = np.append(Z, z1)  # Append values to array just a row
        b = b + 1
    a = a + 1

Z = np.reshape(Z, (nd, nd))

print(Z.dtype)
print(Y.dtype)
print(X.dtype)
Z = Z.astype(float)
Y = Y.astype(float)
X = X.astype(float)

ax.plot_surface(X, Y, Z, rstride=1, cstride=1, cmap='viridis', edgecolor='none')
plt.show()


球体函数的结果是脚本崩溃。我希望此脚本能够绘制这种3D形状。

这是您偶然遇到的错误吗

TypeError: can't convert complex to float
按照这个公式,你要的是一个虚数。如果将其定义为球体方程:

z = (x**2+y**2-1)**0.5
当x=y=0时,您将最终请求sqrt-1,这将不起作用。尝试使用球坐标进行参数化,如本例所示: