3

我正在尝试使用 matplotlib 创建一个由多个线框球体组成的图。我在这里找到了一个代码片段来绘制一个这样的球体,所以我认为只需多次调用 plot_wireframe 就可以很容易地将其扩展到多个球体。我错了。这是一个代码片段:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
x=np.cos(u)*np.sin(v)
y=np.sin(u)*np.sin(v)
z=np.cos(v)
fig = plt.figure(figsize=(8,6))
ax = fig.gca(projection='3d')
ax.plot_wireframe(x*3.+5., y*3., z*3.,linewidths=.2)
ax.view_init(azim=30,elev=40)
ax.set_aspect("equal")
plt.show()
fig = plt.figure(figsize=(8,6))
ax = fig.gca(projection='3d')
ax.plot_wireframe(x*3.+5., y*3., z*3.,linewidths=.2)
spheres = [ [0,0,0,1], [3,0,0,1.6] ]
for v in spheres:
    ax.plot_wireframe(x*v[3]+v[0], y*v[3]+v[1], z*v[3]+v[2],linewidths=.2)
ax.view_init(azim=30,elev=40)
ax.set_aspect("equal")
plt.show()

如果您运行该代码,第一个图将显示一个漂亮的球体,而在第二个图中,所有球体都被扭曲和移动。我搜索以确保可以在同一轴上多次调用 plot_wireframe 但找不到任何东西。另外,我是 Python 菜鸟,但我不认为我做错了什么。感谢您的帮助!

4

1 回答 1

1

简短回答:手动调整轴限制:

import matplotlib.pyplot as plt
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
u, v = np.mgrid[0:2*np.pi:20j, 0:np.pi:10j]
x=np.cos(u)*np.sin(v)
y=np.sin(u)*np.sin(v)
z=np.cos(v)

# I'm not sure what was this for.  
'''
fig = plt.figure(figsize=(8,6))
ax = fig.gca(projection='3d')
ax.plot_wireframe(x*3.+5., y*3., z*3.,linewidths=.2)
ax.view_init(azim=30,elev=40)
ax.set_aspect("equal")

plt.show()
'''

fig = plt.figure(figsize=(8,6))
ax = fig.gca(projection='3d')


ax.plot_wireframe(x*3.+5., y*3., z*3.,linewidths=.2)
spheres = [ [0,0,0,1], [3,0,0,1.6] ]
for v in spheres:
    ax.plot_wireframe(x*v[3]+v[0], y*v[3]+v[1], z*v[3]+v[2],linewidths=.2)
ax.view_init(azim=30,elev=40)

ax.set_xlim([0,7])   # Like so. 
ax.set_ylim([-3,3]) 
ax.set_zlim([-3,3]) 

ax.set_aspect("equal")
plt.show()
于 2012-09-06T10:51:39.320 回答