繁体   English   中英

从 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()
#----------------------------------------------------------

我可以成功获得曲面图: 3d_surface

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

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

然后我得到: 3d_contour

我想在独立的 2D 图中绘制红线轮廓。 它应该在实轴和虚轴上。 而且,我们可以得到轮廓线与轴的交点坐标吗? 如下图所示:

科普里娃

非常感谢!

所以这里的问题是 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()
#----------------------------------------------------------

产生这个情节:

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM