gpt4 book ai didi

python - 如何绘制 3D 曲面?

转载 作者:太空宇宙 更新时间:2023-11-03 20:17:37 33 4
gpt4 key购买 nike

我想绘制一个 3d 曲面,其参数形式为:

x = (-2 * (s^2 - t)^  3)

y = log(t*(t-2*s^2)+2*s^2)

z = 1/(s^2-t)

其中 st 是参数。我使用plt_surface,但它不起作用。我不知道发生了什么事,你能帮我吗?

from matplotlib import pyplot as plt
import numpy as np
import math
import mpl_toolkits.mplot3d.axes3d as axes3d

s,t=symbols('s,t')
x=Function('x')(s,t)
z=Function('z')(s,t)
y=Function('y')(s,t)
figure = plt.figure()
ax = figure.add_subplot(1, 1, 1, projection='3d')
t1= np.linspace(1,100,200)
s1 = np.linspace(1,100,200)
s,t=np.meshgrid(s1,t1)
x = (-2 * (s*s - t)**3)
y = log(t*(t-2*s*s)+2*s*s)
z = 1/(s*s-t)
ax.plot_surface(x,y,z,cmap='rainbow')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('f')
plt.show()

运行后显示:

Traceback (most recent call last):
File "C:\Users\apple\Desktop\pywork\venv\lib\site-packages\sympy\core\cache.py", line 94, in wrapper
retval = cfunc(*args, **kwargs)
TypeError: unhashable type: 'numpy.ndarray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):
File "C:\Users\apple\Desktop\pywork\venv\lib\site-packages\sympy\core\cache.py", line 94, in wrapper
retval = cfunc(*args, **kwargs)
TypeError: unhashable type: 'numpy.ndarray'

During handling of the above exception, another exception occurred:

Traceback (most recent call last):


File "C:/Users/apple/Desktop/pywork/mining/rep.py", line 19, in <module>
y = log(s)
File "C:\Users\apple\Desktop\pywork\venv\lib\site-packages\sympy\core\cache.py", line 96, in wrapper
retval = func(*args, **kwargs)
File "C:\Users\apple\Desktop\pywork\venv\lib\site-packages\sympy\core\function.py", line 458, in __new__
result = super(Function, cls).__new__(cls, *args, **options)
File "C:\Users\apple\Desktop\pywork\venv\lib\site-packages\sympy\core\cache.py", line 96, in wrapper
retval = func(*args, **kwargs)
File "C:\Users\apple\Desktop\pywork\venv\lib\site-packages\sympy\core\function.py", line 277, in __new__
evaluated = cls.eval(*args)
File "C:\Users\apple\Desktop\pywork\venv\lib\site-packages\sympy\functions\elementary\exponential.py", line 577, in eval
coeff = arg.as_coefficient(S.ImaginaryUnit)
AttributeError: 'ImmutableDenseNDimArray' object has no attribute 'as_coefficient'

Process finished with exit code 1

最佳答案

要从代码中获得一些结果,您需要有效的输入数据。在这种情况下,t1 的定义域不合适,会导致 yz 的计算出现错误。下面是修改后的代码,其中 t1 的域发生了更改,使得原始代码可以运行并产生一些绘图。

from matplotlib import pyplot as plt
import numpy as np
#import math
import mpl_toolkits.mplot3d.axes3d as axes3d
from sympy import symbols, Function

s,t = symbols('s,t')
x = Function('x')(s,t)
z = Function('z')(s,t)
y = Function('y')(s,t)

figure = plt.figure(figsize=(8,8))
ax = figure.add_subplot(1, 1, 1, projection='3d')

# experiment with various range of data
t1 = np.linspace(-1,-100,200) #...to enable computation
s1 = np.linspace(1,100,200)
s,t = np.meshgrid(s1,t1)
x = -2*(s*s - t)**3

y = np.log(t*(t-2*s*s)+2*s*s) # need np.
z = 1/(s*s-t)

ax.plot_surface(x,y,z,cmap='rainbow')
ax.set_xlabel('x')
ax.set_ylabel('y')
ax.set_zlabel('z')
ax.set_title('f')
plt.show()

输出图:

enter image description here

关于python - 如何绘制 3D 曲面?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58356135/

33 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com