Python 带有标签和球体的三维散点图
我正在尝试用一些数据点(x、y、z、半径)绘制散点图,这是我到目前为止的结果:Python 带有标签和球体的三维散点图,python,numpy,matplotlib,3d,axis-labels,Python,Numpy,Matplotlib,3d,Axis Labels,我正在尝试用一些数据点(x、y、z、半径)绘制散点图,这是我到目前为止的结果: import numpy as np from mpl_toolkits.mplot3d import Axes3D import matplotlib.pyplot as plt x = np.random.rand(20) y = np.random.rand(20) z = np.random.rand(20) r = np.random.rand(20) plt.rc('text', usetex=Tr
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
x = np.random.rand(20)
y = np.random.rand(20)
z = np.random.rand(20)
r = np.random.rand(20)
plt.rc('text', usetex=True)
plt.rcParams['text.latex.preamble']=[r"\usepackage{amsmath}"]
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
ax.scatter(x, y, z, s=np.pi*r**2*100, c='blue', alpha=0.75)
ax.set_xlabel(r'$x$ $\left[\frac{\text{Mpc}}{h}\right]$')
ax.set_ylabel(r'$y$ $\left[\frac{\text{Mpc}}{h}\right]$')
ax.set_zlabel(r'$z$ $\left[\frac{\text{Mpc}}{h}\right]$')
#plt.savefig('spheres.png')
plt.show()
如何改进此绘图以使x和y标签不与TIC重叠
是否有可能在3D绘图中绘制球体而不是区域?您可以创建一个使用球体而不是圆形标记的绘图,方法是在所述的每个位置绘制一个球体标记。下面是一个例子:
import numpy as np
from mpl_toolkits.mplot3d import Axes3D
import matplotlib.pyplot as plt
def drawSphere(xCenter, yCenter, zCenter, r):
#draw sphere
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)
# shift and scale sphere
x = r*x + xCenter
y = r*y + yCenter
z = r*z + zCenter
return (x,y,z)
x = 10*np.random.rand(20)
y = 10*np.random.rand(20)
z = 10*np.random.rand(20)
r = np.random.rand(20)
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
# draw a sphere for each data point
for (xi,yi,zi,ri) in zip(x,y,z,r):
(xs,ys,zs) = drawSphere(xi,yi,zi,ri)
ax.plot_wireframe(xs, ys, zs, color="r")
plt.show()
要解决标签定位问题,请尝试添加第二行,如前所述 标签的问题,我不知怎的无法解决,因为我使用乳胶字体。。。但是谢谢你的线框创意!