Python matplotlib:3d绘图跨越边界(石墨烯分散)

Python matplotlib:3d绘图跨越边界(石墨烯分散),python,matplotlib,3d,Python,Matplotlib,3d,下面的代码试图绘制一个函数+/-f,它定义了动量空间中的石墨烯色散 # 3D Plot of graphene dispersion from matplotlib import pyplot as plt from mpl_toolkits.mplot3d import Axes3D from matplotlib import cm from matplotlib.ticker import LinearLocator, FormatStrFormatter import numpy as

下面的代码试图绘制一个函数
+/-f
,它定义了动量空间中的石墨烯色散

# 3D Plot of graphene dispersion

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np

def sqrt(x):
    return np.sqrt(x)

def cos(x):
    return np.cos(x)

# Constants
a = 1.0
d = a*np.sqrt(3)
t = 2.7
t2 = 0.5

print "The display is not up to the mark! Modification needed.\n" 


fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.arange(-2.0*np.pi, 2.0*np.pi, 0.1)
y = np.arange(-2.0*np.pi, 2.0*np.pi, 0.1)
x, y = np.meshgrid(x, y)

f=t*sqrt(3.0+2.0*cos(a*x)+4.0*cos(a/2.0*x)*cos(d/2.0*y))

surf = ax.plot_surface(x, y, f, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, antialiased=False)


f=-f
surf = ax.plot_surface(x, y, f, rstride=1, cstride=1, cmap=cm.jet, linewidth=0, antialiased=False)
ax.set_zlim3d(-3.0, 3.0)
fig.colorbar(surf, shrink=1.0, aspect=5)

plt.show()
这给了我一个溢出z轴边界的图:

然而,保持相同的函数定义,并使用gnuplot或Mathematica,我能够生成这个函数

还有这个


使用python和matplotlib是否可以复制最后两个呢?

我不太确定您想要什么,因为您明确地在数据范围内设置了z限制(
ax.set\u zlim3d(-3.0,3.0)
),但我只需注释这一行(并选择一个更好的颜色贴图),就可以得到类似的图:

(还请注意,您不需要定义包装函数来创建
np.sqrt
等的别名。函数是Python中的第一类对象,您只需指定一个名称:
sqrt=np.sqrt

# 3D Plot of graphene dispersion

from matplotlib import pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
from matplotlib import cm
from matplotlib.ticker import LinearLocator, FormatStrFormatter
import numpy as np

sqrt = np.sqrt
cos = np.cos

# Constants
a = 1.0
d = a*np.sqrt(3)
t = 2.7
t2 = 0.5

fig = plt.figure()
ax = fig.gca(projection='3d')
x = np.arange(-2.0*np.pi, 2.0*np.pi, 0.1)
y = np.arange(-2.0*np.pi, 2.0*np.pi, 0.1)
x, y = np.meshgrid(x, y)

f=t*sqrt(3.0+2.0*cos(a*x)+4.0*cos(a/2.0*x)*cos(d/2.0*y))

surf = ax.plot_surface(x, y, f, rstride=1, cstride=1, cmap=plt.get_cmap('PuOr'),
                       linewidth=0, antialiased=False)


f=-f
surf = ax.plot_surface(x, y, f, rstride=1, cstride=1, cmap=plt.get_cmap('PuOr'),
                       linewidth=0, antialiased=False)
#ax.set_zlim3d(-3.0, 3.0)
fig.colorbar(surf, shrink=1.0, aspect=5)

plt.show()