如何使用Plotly使用倾斜线或旋转线绘制椭球体?

2024-09-27 07:34:41 发布

您现在位置:Python中文网/ 问答频道 /正文

我正在使用empetplotly社区表单中的以下函数绘制椭球体:

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

然而,我得到了线周围不完美的椭球体。有关更多说明,请参阅随附的图片。我们怎样才能更新函数来考虑直线的旋转轴并画出完美的椭圆线?p>

调用函数并绘制椭球体的代码如下所示:

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

Plotly points represent lines and ellipsoids

数据: 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

在此方面的任何帮助都将不胜感激


Tags: oftheadddfnppifigsin
1条回答
网友
1楼 · 发布于 2024-09-27 07:34:41

我发现的解决方案之一是在创建椭球体时加入旋转角度。以下是角度的更新版本:

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')
   #rotation matrix   
   R = np.array([ax1, ax2]).T
   if np.linalg.det(R) <0: 
       raise ValueError("the det(R) must be positive to get a  positively oriented ellipse reference frame")
   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)
   
   # 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

此外,当我们调用函数时,我们定义旋转角度如下: 示例1: 请注意,我们定义了pi/1.93,这将在我的模型中产生旋转角度。这将由用户定义,并由用户进行可视化验证

# well-4 
x_center = h4
y_center = k4
angel_4 = pi/1.93
x, y = ellipse(x_center=x_center, y_center=y_center, ax1 =[cos(angel_4), sin(angel_4)],  ax2=[-sin(angel_4),cos(angel_4)], a=a4, b =b4)
fig.add_scatter(x=x, y=y, mode = 'lines', name='Zone-1 - well-4', fill='toself', opacity=0.5)

示例2: 请注意,我们定义了pi/1.935来旋转椭圆

# well-5
x_center = h5
y_center = k5
angel_5 = pi/1.935
x, y = ellipse(x_center=x_center, y_center=y_center, ax1 =[cos(angel_5), sin(angel_5)],  ax2=[-sin(angel_5),cos(angel_5)], a=a5-20, b =b5-140)
fig.add_scatter(x=x, y=y, mode = 'lines', name='Zone-1 - well-5', fill='toself', opacity=0.5)

示例3 请注意,我们定义了pi/1.921来旋转椭圆

# well-6
x_center=h6
y_center=k6
angel_6 = pi/1.921
x, y = ellipse(x_center=x_center, y_center=y_center, ax1 =[cos(angel_6), sin(angel_6)],  ax2=[-sin(angel_6),cos(angel_6)], a=a6-25, b =b6-140)
fig.add_scatter(x=x, y=y, mode = 'lines', name='Zone-1 - well-6', fill='toself', opacity=0.8)

请参见附图作为示例

enter image description here

相关问题 更多 >

    热门问题