从 3d 曲面图获取 2d 等高线图

Get a 2d contour plot from a 3d surface plot

我使用 matplotlib plot_surface() 函数并在 3D 中绘制复杂函数。这是我的代码:

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

point_num = 200
x = np.linspace(-5, 3, point_num)
y = np.linspace(-5, 5, point_num)

# Real axis and imaginary axis-----------------------------
Re, Im = np.meshgrid(x, y) 
#----------------------------------------------------------

# here is the complex function I need evaluate-------------
z = Re + Im * 1j
R3 = 1 + z + 1/2 * z**2 + 1/6 * pow(z, 3)
#----------------------------------------------------------

# my 3d surface plot----------------------------------------
fig = plt.figure()
ax = plt.axes(projection='3d')

ax.plot_surface(Re, Im, np.abs(R1))
ax.set_xlabel("Im(z)")
ax.set_ylabel("Re(z)")
ax.set_zlabel('R(z)')

plt.show()
#----------------------------------------------------------

我可以成功得到曲面图:

表面看起来像一个圆锥体。现在我想用平面 R(z)=1 “切片”锥体并获得二维等高线图。我可以像这样在 3D 表面上得到轮廓:

ax.contour(Re, Im, np.abs(R3), [1], colors='r')

然后我得到:

我想在独立的二维图中绘制红线等高线。它应该在实轴和虚轴上。而且,我们能否得到等高线与轴线的交点坐标呢?如下图:

非常感谢!

所以这里的问题是 plt.Axes() 对象不能同时在 2D 和 3D 投影中。在我的版本中,我已经注释掉了你的 3D 图,这样我们就可以专注于独立的 2D 图。那就只有这个了:

fig = plt.figure()
ax = plt.axes()
ax.contour(Re, Im, np.abs(R3), [1], c='r')

完整版:

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

point_num = 200
x = np.linspace(-5, 3, point_num)
y = np.linspace(-5, 5, point_num)

# Real axis and imaginary axis-----------------------------
Re, Im = np.meshgrid(x, y)
#----------------------------------------------------------

# here is the complex function I need evaluate-------------
z = Re + Im * 1j
R3 = 1 + z + 1/2 * z**2 + 1/6 * pow(z, 3)
#----------------------------------------------------------

# my 3d surface plot----------------------------------------
fig = plt.figure()
# ax = plt.axes(projection='3d')
# ax.plot_surface(Re, Im, np.abs(R3))
# ax.set_xlabel("Im(z)")
# ax.set_ylabel("Re(z)")
# ax.set_zlabel('R(z)')

# ax.contour(Re, Im, np.abs(R3), [1], colors='r')
ax = plt.axes()
ax.contour(Re, Im, np.abs(R3), [1], c='r')

plt.show()
#----------------------------------------------------------