我正在使用empet plotly 社区表单中的以下函数绘制一个椭圆体:
def ellipse(x_center=0, y_center=0, ax1 = [1, 0], ax2 = [0,1], a=1, b =1, N=100):
# x_center, y_center the coordinates of ellipse center
# ax1 ax2 two orthonormal vectors representing the ellipse axis directions
# a, b the ellipse parameters
if np.linalg.norm(ax1) != 1 or np.linalg.norm(ax2) != 1:
raise ValueError('ax1, ax2 must be unit vectors')
if abs(np.dot(ax1, ax2)) > 1e-06:
raise ValueError('ax1, ax2 must be orthogonal vectors')
t = np.linspace(0, 2*pi, N)
#ellipse parameterization with respect to a system of axes of directions a1, a2
xs = a * cos(t)
ys = b * sin(t)
#rotation matrix
R = np.array([ax1, ax2]).T
# coordinate of the ellipse points with respect to the system of axes [1, 0], [0,1] with origin (0,0)
xp, yp = np.dot(R, [xs, ys])
x = xp + x_center
y = yp + y_center
return x, y
但是,我在线条周围得到了不完美的椭圆体。有关更多说明,请参阅所附图片。我们如何更新函数以考虑线的旋转轴并围绕线绘制完美的椭圆体?
调用函数并绘制椭球的代码如下:
x22 = tw2_df['x']
z22 = tw2_df['z']
x5 = tw5_df['x']
z5 = tw5_df['z']
# Create figure
fig = go.Figure()
# Add scatter traces
fig.add_trace(go.Scatter(x=x22, y=z22, name="line-2", mode="markers", marker_color='gray'))
fig.add_trace(go.Scatter(x=x5, y=z5, name="line-3", mode="markers", marker_color='gray'))
# well-2
x_center=h2
y_center=k2
x22, y22 = ellipse(x_center=x_center, y_center=y_center,
ax1 =[cos(pi/2), sin(pi/2)], ax2=[-sin(pi/2),cos(pi/2)],
a=a2, b =b2)
fig.add_scatter(
x=x22,
y=y22,
mode = 'lines')
# well-5
x_center=h5
y_center=k5
x5, y5 = ellipse(x_center=x_center, y_center=y_center,
ax1 =[cos(pi/2), sin(pi/2)], ax2=[-sin(pi/2),cos(pi/2)],
a=a5, b =b5)
fig.add_scatter(
x=x5,
y=y5,
mode = 'lines')
fig.update_layout(showlegend=True)
fig.show()
数据:h2=540.05,k2=-54.75,a2=36.67,b2=577.62 h5=550.28,k5=-246.58,a5=43.17,b5=590.69
这是将 x 和 y 数据作为 CSV 文件的链接。 https://drive.google.com/drive/folders/1JGKdnfqu9aZy8YFtL_2kL-NnJ-fJHcMM?usp=sharing
对此的任何帮助将不胜感激!