守着星空守着你
这是我的解决方案:# Import packagesimport numpy as npfrom mpl_toolkits.mplot3d import Axes3D # Needed to create 3D plotsimport matplotlibimport matplotlib.pyplot as plt#%matplotlib notebook # Uncomment this if using Jupyter# Define functiondef fun(x): # Note: x is a complex() object, and * and + are defined for complex numbers return x*x + 1# Create 1x1 figure with 3 axesfig = plt.figure()ax = fig.add_subplot(111, projection='3d')# Define grid of x and w using a step of 0.05X = W = np.arange(-20.0, 20.0, 0.05)X, W = np.meshgrid(X,W)complexVals = X + 1j*W # Convert X/W grid into complex numbers# Call the function on the complex numbers and extract the real/imag partsY_Z = fun(complexVals)Y = Y_Z.realZ = Y_Z.imag# Create colormap for Wcolor_dimension = Wminn, maxx = color_dimension.min(), color_dimension.max()norm = matplotlib.colors.Normalize(minn, maxx)m = plt.cm.ScalarMappable(norm=norm, cmap='jet')m.set_array([])fcolors = m.to_rgba(color_dimension)# Create surfaceax.plot_surface(X, Y, Z, facecolors=fcolors, vmin=minn, vmax=maxx, shade=False, linewidth=0, antialiased=False)# Set axis labelsax.set_xlabel('X (Re)')ax.set_ylabel('Y (Re)')ax.set_zlabel('Z = f(x) (Im)')# Create colorbar and set titlecbr = plt.colorbar(m)cbr.ax.set_title('W')# Show plot with colorbarplt.show()这是输出:但是,如果您使用 Anaconda,我强烈建议将此代码添加到 Jupyter Notebook 中。这就是你可以获得交互式情节的原因。它已经随 Anaconda 一起安装,您可以通过jupyter notebook在 Anaconda 提示符中输入来启动它。