2015-11-05 115 views
0

在Matplotlib中,我想繪製一個球體,其表面上有一個網格,分成30度的球面座標。用Matplotlib繪製球體網格

代碼:

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import numpy as np 

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 

u = np.linspace(0, 2 * np.pi, 13) 
v = np.linspace(0, np.pi, 7) 

x = 10 * np.outer(np.cos(u), np.sin(v)) 
y = 10 * np.outer(np.sin(u), np.sin(v)) 
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v)) 
ax.plot_surface(x, y, z, rstride=1, cstride=1, color='w', shade=0) 

plt.show() 

可生產圖:

enter image description here

不過,我想在球體上的輪廓平滑,而不是繪製點之間直接畫。如果我增加取樣密度,我得到一個光滑的球體,但線被描繪過於密集:

enter image description here

我如何可以繪製在光滑球體用30度分隔線?

回答

0

您可以先插入數據,然後使用更大的步幅進行繪圖。

from mpl_toolkits.mplot3d import Axes3D 
import matplotlib.pyplot as plt 
import numpy as np 
import scipy.ndimage 

fig = plt.figure() 
ax = fig.add_subplot(111, projection='3d') 

u = np.linspace(0, 2 * np.pi, 13) 
v = np.linspace(0, np.pi, 7) 

x = 10 * np.outer(np.cos(u), np.sin(v)) 
y = 10 * np.outer(np.sin(u), np.sin(v)) 
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v)) 

# use scipy to interpolate 
xdata = scipy.ndimage.zoom(x, 3) 
ydata = scipy.ndimage.zoom(y, 3) 
zdata = scipy.ndimage.zoom(z, 3) 

ax.plot_surface(xdata, ydata, zdata, rstride=3, cstride=3, color='w', shade=0) 

plt.show() 

基於這個其他問題here

請注意,scipy.ndimage.zoom可能是矯枉過正。我不明白你爲什麼不能在uv上使用較小的分辨率,然後相應地提高步幅。這似乎工作也很不錯:

... 
# increased num pts by 3 
u = np.linspace(0, 2 * np.pi, 39) 
v = np.linspace(0, np.pi, 21) 

x = 10 * np.outer(np.cos(u), np.sin(v)) 
y = 10 * np.outer(np.sin(u), np.sin(v)) 
z = 10 * np.outer(np.ones(np.size(u)), np.cos(v)) 

# Use 3x the stride, no scipy zoom 
ax.plot_surface(x, y, z, rstride=3, cstride=3, color='w', shade=0) 
... 
+0

感謝。我試過你的代碼,但是它給了我下面的圖:http://i.stack.imgur.com/LBxAQ.png,這看起來不太合適..... – Karnivaurus

+0

我認爲搖擺不定只是由於它是如何繪製...不知道。如果旋轉它,實際的黑線是平滑的。 – bornruffians

0

bornruffians的解決方案看起來搖搖晃晃的,因爲你沒有實際的表面你想到哪裏去。所以你有點看透它,這就是線框出現彎曲的原因。

很難找到其他的解決方案,因爲你的需要一個好的分辨率球體(也就是用小參數步驟),否則你會看到「通過」它。那麼你需要在這個高分辨率的球體上畫線。

您可以看看matplotlib中的底圖模塊:http://matplotlib.org/basemap/users/examples.html 這可能有點矯枉過正,但它會起作用。

enter image description here

有可能與Poly3DColelction和Line3D採集解決方案:Transparency for Poly3DCollection plot in matplotlib