2015-08-08 40 views
0

我有下面的代碼,試圖繪製一個函數+/- f,它定義了動量空間中的石墨烯色散。matplotlib:3d圖橫穿邊界(石墨烯色散)

# 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軸邊界:

enter image description here

然而,保持相同的功能定義和使用gnuplot的或數學我是能夠生產這種

Source: http://mathematica.stackexchange.com/questions/6916/can-the-color-in-meshstyle-be-specified-by-a-colorfunction-such-as-sunsetcolor

and this Source:

最後兩個中的任何一個可以通過使用python和matplotlib來重現嗎?

回答

2

由於您明確地設置了數據範圍內的z限制(ax.set_zlim3d(-3.0, 3.0)),所以我不太清楚您想要什麼,但是通過註釋掉此行(並選取更好的顏色映射):

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

enter image description here

(還要注意的是,你並不需要定義一個包裝函數創建一個別名np.sqrt等功能在Python第一類對象,你可以簡單地分配名稱:sqrt = np.sqrt。)