Can someone tell me why the xz projection in this code does not appear on the x-z plane?
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy.stats import kde
# Sample data
np.random.seed(42)
x = np.random.randn(100)
y = np.random.randn(100)
z = np.random.randn(100)
# Create figure and 3D axes
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Scatter plot
ax.scatter(x, y, z, c='b', marker='o')
# KDE for projections
def kde_projection(data1, data2):
values = np.vstack([data1, data2])
kernel = kde.gaussian_kde(values)
x_range = np.linspace(data1.min(), data1.max(), 100)
y_range = np.linspace(data2.min(), data2.max(), 100)
X, Y = np.meshgrid(x_range, y_range)
positions = np.vstack([X.ravel(), Y.ravel()])
Z = np.reshape(kernel(positions).T, X.shape)
return X, Y, Z
# Projections with KDE
# XY plane
X_xy, Y_xy, Z_xy = kde_projection(x, y)
ax.contour(X_xy, Y_xy, Z_xy, zdir='z', offset=z.min()-1, cmap='Blues')
# XZ plane
X_xz, Z_xz, Y_xz = kde_projection(x, z)
ax.contour(X_xz, Y_xz, Z_xz, zdir='y', offset=y.min()-1, cmap='Reds')
# YZ plane
Y_yz, Z_yz, X_yz = kde_projection(y, z)
ax.contour(X_yz, Y_yz, Z_yz, zdir='x', offset=x.min()-1, cmap='Greens')
# Set labels and limits
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim(x.min() - 1, x.max() + 1)
ax.set_ylim(y.min() - 1, y.max() + 1)
ax.set_zlim(z.min() - 1, z.max() + 1)
# Show plot
plt.show()
It correcly plots the yz and xy projections onto their respective planes, but xz doesn't want to work... I'm not sure if I'm missing something, but I think the pattern should hold for all three planes.
Thanks.
Can someone tell me why the xz projection in this code does not appear on the x-z plane?
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
import numpy as np
from scipy.stats import kde
# Sample data
np.random.seed(42)
x = np.random.randn(100)
y = np.random.randn(100)
z = np.random.randn(100)
# Create figure and 3D axes
fig = plt.figure(figsize=(10, 8))
ax = fig.add_subplot(111, projection='3d')
# Scatter plot
ax.scatter(x, y, z, c='b', marker='o')
# KDE for projections
def kde_projection(data1, data2):
values = np.vstack([data1, data2])
kernel = kde.gaussian_kde(values)
x_range = np.linspace(data1.min(), data1.max(), 100)
y_range = np.linspace(data2.min(), data2.max(), 100)
X, Y = np.meshgrid(x_range, y_range)
positions = np.vstack([X.ravel(), Y.ravel()])
Z = np.reshape(kernel(positions).T, X.shape)
return X, Y, Z
# Projections with KDE
# XY plane
X_xy, Y_xy, Z_xy = kde_projection(x, y)
ax.contour(X_xy, Y_xy, Z_xy, zdir='z', offset=z.min()-1, cmap='Blues')
# XZ plane
X_xz, Z_xz, Y_xz = kde_projection(x, z)
ax.contour(X_xz, Y_xz, Z_xz, zdir='y', offset=y.min()-1, cmap='Reds')
# YZ plane
Y_yz, Z_yz, X_yz = kde_projection(y, z)
ax.contour(X_yz, Y_yz, Z_yz, zdir='x', offset=x.min()-1, cmap='Greens')
# Set labels and limits
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.set_xlim(x.min() - 1, x.max() + 1)
ax.set_ylim(y.min() - 1, y.max() + 1)
ax.set_zlim(z.min() - 1, z.max() + 1)
# Show plot
plt.show()
It correcly plots the yz and xy projections onto their respective planes, but xz doesn't want to work... I'm not sure if I'm missing something, but I think the pattern should hold for all three planes.
Thanks.
Share Improve this question edited Mar 8 at 16:03 earnric asked Mar 8 at 0:31 earnricearnric 5335 silver badges20 bronze badges 4- I think you'll need to supply an image of the plot you have, and a better description of what you want. I suspect this is an issue with the camera position or axes orientation, and that you'll be able to rotate the axes to get what you want. – bnaecker Commented Mar 8 at 3:57
- 1 Your code already works fine. Just wiggle the view a bit and you will find it is OK. Strictly, your offset is putting the contours at ymin()-1 rather than the xz plane (which is y=0), but I assume that is intended anyway. – lastchance Commented Mar 8 at 9:11
- Well, ymin()-1 is 'just behind' the xz display plane... y =0 is in the middle of the plot... I beleive this is some kind of bug with the projection. – earnric Commented Mar 8 at 16:07
- @bnaecker --- you were right. It was a weird projection effect. Rotating the camera solved it and the original code is fine. – earnric Commented Mar 8 at 16:25
2 Answers
Reset to default 0Sorry everyone... the code is correct. It was a weird projection issue. One I rotated the plot a bit the original code produced the desired plot. Thanks...
Also, chaning the xz default projection location to:
# XZ plane
X_xz, Z_xz, Y_xz = kde_projection(x, z)
ax.contour(X_xz, Y_xz, Z_xz, zdir='y', offset=y.max()+1, cmap='Reds')
works for the default view.
X_xz, Y_xz, Z_xz = kde_projection(x, z)