2015-05-17 22 views
6

Tôi có dữ liệu 4D (điểm phân tán 3D + màu) được vẽ bằng thư viện mplot3d của matplotlib. Để hỗ trợ phân tích cách đám mây của các điểm được phân phối trong không gian, tôi muốn hiển thị một phép chiếu của đám mây trên mỗi 3 mặt phẳng (XY, XZ, YZ) sử dụng biểu đồ 2D/đường bao.Làm thế nào để vẽ sơ đồ chiếu của dữ liệu phân tán 3D trên máy bay XY/XZ/YZ?

Đây là một MWe sử dụng ax.plot làm những gì tôi muốn (theo liên kết bên dưới). Đây kỹ thuật công trình, nhưng tôi nghĩ rằng thay buck-shot từ ax.plot với lô đường viền sẽ được trực quan dễ chịu hơn:

import numpy as np 

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 

# Prepare sample data - normally distributed 
NSamples = 5000 
vmin, vmax = -2, 2 

X = np.random.normal(loc=-.1, scale=.5, size=(NSamples,)) 
Y = np.random.normal(loc=.1, scale=.25, size=(NSamples,)) 
Z = np.random.normal(loc=0, scale=1, size=(NSamples,)) 

# Create figure, add subplot with 3d projection 
fig = plt.figure(figsize=(5,5)) 
ax = fig.add_subplot(111, projection='3d') 
ax.set_xlabel("X") 
ax.set_ylabel("Y") 
ax.set_zlabel("Z") 
ax.set_xlim(vmin, vmax) 
ax.set_ylim(vmin, vmax) 
ax.set_zlim(vmin, vmax) 

# Plot the data cloud 
ax.scatter(X, Y, Z, s=.5, alpha=.05, color='k') 

# Plot the 2D projections using `plot`. This is the piece I'd like to improve 
ax.plot(X, Y, '+', markersize=.2, color='r', zdir='z', zs=-2.) 
ax.plot(X, Z, '+', markersize=.2, color='g', zdir='y', zs=2.) 
ax.plot(Y, Z, '+', markersize=.2, color='b', zdir='x', zs=-2.) 

plt.savefig("3DScatter.png") 

# Now, I'd *like* for the following histograms to be plotted on each of the XY, XZ, YZ planes 
instead of using `plot` above 
for label, data_x, data_y in [ ['XY', X, Y], ['XZ', X, Z], ['YZ', Y, Z] ]: 
    hist, binx, biny = np.histogram2d(data_x, data_y, bins=[xbins, ybins]) 

    plt.figure(figsize=(5,5)) 
    plt.imshow(hist, extent=[vmin,vmax,vmin,vmax]) 
    plt.xlabel(label[1]) 

nào sản xuất:

3D scatterXY

XZYZ

vv

Vì vậy, để được clea r, có cách nào để vẽ biểu đồ XY, XZ, YZ 2D được vẽ bằng imshow ở trên trên các trục 3D được liên kết không? Giải pháp dựa trên contour cũng sẽ ổn.

Lưu ý rằng (Tôi khá chắc chắn) đây là không một sự lặp lại của this related question, có giải pháp chỉ hoạt động cho dữ liệu 2D (f (x, y)), chứ không phải 3D (f (x, y, z)).

Trả lời

4

Nếu bạn là ổn với việc sử dụng contour hoặc contourf, bạn có thể làm một cái gì đó như thế này:

import numpy as np 

import matplotlib.pyplot as plt 
from mpl_toolkits.mplot3d import Axes3D 

# Prepare sample data - normally distributed 
NSamples = 5000 
vmin, vmax = -2, 2 

X = np.random.normal(loc=-.1, scale=.5, size=(NSamples,)) 
Y = np.random.normal(loc=.1, scale=.25, size=(NSamples,)) 
Z = np.random.normal(loc=0, scale=1, size=(NSamples,)) 

# Create figure, add subplot with 3d projection 
fig = plt.figure(figsize=(5,5)) 
ax = fig.add_subplot(111, projection='3d') 
ax.set_xlabel("X") 
ax.set_ylabel("Y") 
ax.set_zlabel("Z") 
ax.set_xlim(vmin, vmax) 
ax.set_ylim(vmin, vmax) 
ax.set_zlim(vmin, vmax) 

# Plot the data cloud 
ax.scatter(X, Y, Z, s=.5, alpha=.05, color='k') 

hist, binx, biny = np.histogram2d(X, Y) 
x = np.linspace(X.min(), X.max(), hist.shape[0]) 
y = np.linspace(Y.min(), Y.max(), hist.shape[1]) 
x, y = np.meshgrid(x, y) 
ax.contour(x, y, hist, zdir='z', offset=-3.) 

hist, binx, biny = np.histogram2d(X, Z) 
x = np.linspace(X.min(), X.max(), hist.shape[0]) 
z = np.linspace(Z.min(), Z.max(), hist.shape[1]) 
x, z = np.meshgrid(x, z) 
ax.contour(x, hist, z, zdir='y', offset=3) 

hist, binx, biny = np.histogram2d(Y, Z) 
y = np.linspace(Y.min(), Y.max(), hist.shape[0]) 
z = np.linspace(Z.min(), Z.max(), hist.shape[1]) 
z, y = np.meshgrid(z, y) 
ax.contour(hist, y, z, zdir='x', offset=-3) 

ax.set_xlim([-3, 3]) 
ax.set_ylim([-3, 3]) 
ax.set_zlim([-3, 3]) 
+0

Tôi thề rằng tôi đã cố gắng một cách chính xác giải pháp này, nhưng rõ ràng tôi không. Nhìn vào phiên bản trước của tôi cho nơi tôi đã đi sai. Điều này hoạt động chính xác như dự định, cảm ơn bạn! – paradiso

+0

Bạn được chào đón :-) –

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