所以我从3D球体中采样并想要显示它,尽管plt.axis('equal')
命令它仍然看起来像椭圆形而不是球形。这是我的代码:
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
def sphere_sampler(dimension=2,sample_size=1):
phi_1=np.random.uniform(low=0,high=np.pi,size=sample_size)
phi_2=np.random.uniform(low=0,high=2*np.pi,size=sample_size)
sample=np.empty((sample_size,dimension))
sample[:,0]=np.cos(phi_1)
sample[:,1]=np.sin(phi_1)*np.cos(phi_2)
sample[:,2]=np.sin(phi_1)*np.sin(phi_2)
return sample
pre_sample=sphere_sampler(3,1000)
sample=pre_sample.reshape(pre_sample.shape[0],3)
fig=plt.figure()
ax = fig.gca(projection='3d')
ax.scatter(sample[:,0],sample[:,1],sample[:,2])
ax.set_xlim(-1,1)
ax.set_ylim(-1,1)
ax.set_zlim(-1,1)
plt.axis('equal')
plt.show()
我做错了哪一部分?看起来显示器有问题。如何让show()方法不改变比例?
答案 0 :(得分:2)
而不是plt.axis('equal')
,请使用:
ax.set_aspect("equal")