2014-06-09 20 views
5

tôi đang cố gắng để tạo ra một biểu đồ phân tán với một số điểm dữ liệu (x, y, z, bán kính) và đây là kết quả của tôi đến nay:Scatter Lô 3D với nhãn và hình cầu

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

http://www.file-upload.net/download-9033814/spheres.png.html

Làm thế nào tôi có thể cải thiện lô đất này để không có sự trùng lặp của các nhãn x và y với các tics?

Và có khả năng tạo hình cầu thay vì các khu vực trong ô 3D này không?

Trả lời

8

Bạn có thể tạo cốt truyện sử dụng hình cầu thay vì điểm đánh dấu vòng tròn bằng cách vẽ một ô tại mỗi vị trí như được mô tả here. Dưới đây là một ví dụ:

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

spheres in 3D plot

Để khắc phục các vị trí nhãn vấn đề thử thêm một dòng thứ hai như đã mô tả here.

+0

vấn đề nhãn tôi bằng cách nào đó không thể giải quyết, bởi vì tôi sử dụng phông chữ Latex ... nhưng cảm ơn bạn cho ý tưởng wireframe! – Andy

Các vấn đề liên quan