如何绘制3D曲面?

时间:2019-10-12 16:26:05

标签: python numpy matplotlib sympy

我想绘制一个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

1 个答案:

答案 0 :(得分:0)

要从代码中获得一些结果,您需要有效的输入数据。在这种情况下,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