0
votes

I use matplotlib plot_surface() function and plot a complex function in 3D. Here is my code:

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

I can successfully get the surface plot:3d_surface

The surface looks like a cone. Now I want to "slice" the cone with plane R(z)=1 and get a 2D contour plot. I can get the contour on the 3D surface like this:

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

Then I get: 3d_contour

I want to plot the red line contour in an independent 2D plot. It should be in the real and imaginary axis. Moreover, can we get the coordinates of the intersection points of the contour line with the axis? Like the figure below:

Kopriva

Thank you very much!

1

1 Answers

0
votes

So the issue here is that a plt.Axes() object can't be in the 2D and 3D projections simultaneously. In my version, I have commented out your 3D plots, so that we can focus on the independent 2D plot. It's then only this:

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

Full version:

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

Produces this plot: