我对这个基本问题表示歉意 - 我是 Python 新手。我试图在 R3 中显示两个交错的圆圈,但 python 无法正确显示谁在“前面”。我想要的是 Python 能够提供 Mathematica 所提供的功能,如下图所示: 代替 我使用的Python代码如下,它需要调整(不用担心视角——我可以稍后处理)。 非常感谢!
import numpy as np
import matplotlib.pyplot as plt
from mpl_toolkits.mplot3d import Axes3D
# Define the parametric equations for the circles
def circle_xy(t, h, k, r):
x = h + r * np.cos(t)
y = k + r * np.sin(t)
z = 0 * np.ones_like(t)
return x, y, z
def circle_yz(t, h, k, r):
x = h + r * np.sin(t)
y = 0 * np.ones_like(t)
z = k + r * np.cos(t)
return x, y, z
t = np.linspace(0, 2 * np.pi, 1000)
# Plotting
fig = plt.figure()
ax = fig.add_subplot(111, projection='3d')
x1, y1, z1 = circle_xy(t, 0, 0, 1)
x2, y2, z2 = circle_yz(t, 1, 0, 1)
ax.plot(x1, y1, z1, label='unit circle centered at x=0, y=0 in the plane z=0',linewidth=3)
ax.plot(x2, y2, z2, label='unit circle centered at x=1, z=0 in the plane y=0',linewidth=3)
ax.set_xlabel('X')
ax.set_ylabel('Y')
ax.set_zlabel('Z')
ax.legend()
ax.view_init(30, 120)
plt.show()
我尝试了上面显示的代码。
Plotly 对此非常有用,它使得在 R3 中检查表面变得更容易,而在 matplotlib 中则更困难(允许在 HTML 输出中平移、旋转和缩放所有内容)
import numpy as np
import plotly.graph_objects as go
# Define the parametric equations for the circles
def circle_xy(t, h, k, r):
x = h + r * np.cos(t)
y = k + r * np.sin(t)
z = 0 * np.ones_like(t)
return x, y, z
def circle_yz(t, h, k, r):
x = h + r * np.sin(t)
y = 0 * np.ones_like(t)
z = k + r * np.cos(t)
return x, y, z
t = np.linspace(0, 2 * np.pi, 1000)
x1, y1, z1 = circle_xy(t, 0, 0, 1)
x2, y2, z2 = circle_yz(t, 1, 0, 1)
# Create the 3D plot in plotly
fig = go.Figure()
# Add circles to the plot
fig.add_trace(go.Scatter3d(x=x1, y=y1, z=z1, mode='lines', name='unit circle centered at x=0, y=0 in the plane z=0', line=dict(width=3)))
fig.add_trace(go.Scatter3d(x=x2, y=y2, z=z2, mode='lines', name='unit circle centered at x=1, z=0 in the plane y=0', line=dict(width=3)))
# Setting the axis labels
fig.update_layout(scene=dict(xaxis_title='X', yaxis_title='Y', zaxis_title='Z'))
# Display the
情节 图.show()