【发布时间】:2020-01-27 05:11:48
【问题描述】:
我有这个简单的代码,它试图获取两个复数 E1 和 E2 的实部的 3D 图,作为 t 和 g 的函数。
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import cmath
eps=0.5
def ReE1(t,g):
E1=eps+cmath.sqrt(t**2-g**2)
return E1.real
def ReE2(t,g):
E2=eps-cmath.sqrt(t**2-g**2)
return E2.real
fig = plt.figure()
ax = plt.axes(projection="3d")
t = np.linspace(0, 10, 50)
g = np.linspace(0, 10, 50)
X, Y = np.meshgrid(t, g)
Z = ReE1(X, Y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap='winter', edgecolor='none')
Z = ReE2(X, Y)
ax.plot_surface(X, Y, Z, rstride=1, cstride=1,
cmap='summer', edgecolor='none')
plt.show()
我在使用 Python 3 运行时遇到以下错误。
Traceback (most recent call last):
File "t2.py", line 28, in <module>
Z = ReE1(X, Y)
File "t2.py", line 11, in ReE1
E1=eps+cmath.sqrt(t**2-g**2)
TypeError: only length-1 arrays can be converted to Python scalars
我们该如何解决?另外,我们可以直接使用复杂函数E1 和E2(而不是ReE1 和ReE2)并在绘图时调用real 模块吗?
【问题讨论】:
标签: python python-3.x matplotlib 3d cmath