Cupitor
Cupitor

Reputation: 11637

Plot a sphere that looks like a sphere

So I am sampling from a 3D sphere and want to display it and despite the plt.axis('equal') command it still looks elliptic rather than spheric. Here is my code:

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

Which part of it I am doing wrong? It looks like that something is wrong with display. How can I make the show() method to not to change the scale?

Upvotes: 3

Views: 227

Answers (1)

HYRY
HYRY

Reputation: 97291

instead of plt.axis('equal'), use:

ax.set_aspect("equal")

Upvotes: 2

Related Questions