我正在尝试绘制3D图,因为Wolfram无法处理eq。但是python似乎也有麻烦。我基本上使用了冲浪图示例:http://matplotlib.org/mpl_toolkits/mplot3d/tutorial.html,但它不起作用。有人知道为什么吗?
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import matplotlib.pyplot as plt
import numpy as np
fig = plt.figure()
ax = fig.gca(projection='3d')
x = y = np.arange(-10, 10, 0.25)
x,y = np.meshgrid(x,y)
z = (2*x**2 *(np.sqrt(x**2-4 * x+y**2+8)+ np.sqrt(x**2+4 * x+y**2+8))-x*(y**2-4)*
(np.sqrt(x**2-4 * x+y**2+8)-np.sqrt(x**2+4 * x+y**2+8))-2*(y**2+4)+(np.sqrt(x**2-4 * x+y**2+8)+np.sqrt(x**2+4 * x+y**2+8))+x**3 *
(np.sqrt(x**2+4 * x+y**2+8)-np.sqrt(x**2-4 * x+y**2+8)))/((x**2-4 * x+y**2+4) * np.sqrt(x**2-4 * x+y**2+8)*(x**2+4 * x+y**2+4)*np.sqrt(x**2+4 * x+y**2+8))
surf = ax.plot_surface(x,y,z, rstride=1, cstride=1, cmap=cm.coolwarm,
linewidth=0, antialiased=False)
ax.set_zlim(-0.1, 0.1)
ax.zaxis.set_major_locator(LinearLocator(10))
ax.zaxis.set_major_formatter(FormatStrFormatter('%.02f'))
fig.colorbar(surf, shrink=0.5, aspect=5)
plt.show()
最佳答案
问题是z
表面的形状非常尖峰,实际上是z.max( ) = inf
。您可能要做的一件事是将无限值替换为有限最大值:
z[ np.isinf( z )] = z[ np.isfinite(z)].max( )
这将为您提供下面的图。请注意,我尚未设置z限制(
ax.set_zlim
),以便可以看到整个形状。如果您只对图的在
[-.1, .1]
之间的部分感兴趣,则可以通过以下方法在这些点处切断z
:z[ z > .1 ] = .1
z[ z < -.1 ] = -.1
您将获得:
关于python - ax.plot_surface没有给出颜色,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/20594207/