I am drawing an ellipsoid using the following function from empet plotly community form:
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
However, I am getting unperfect ellipsoids around the lines. Refer to the image attached for more clarification. How we can update the function to consider the rotational axis of the lines and draw perfect ellipsoids around the lines?
The code to call the function and plot the ellipsoid as follow:
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
Data: h2=540.05, k2=-54.75, a2=36.67, b2=577.62 h5=550.28, k5=-246.58, a5=43.17, b5=590.69
This is the link that has the x and y data as CSV files. https://drive.google.com/drive/folders/1JGKdnfqu9aZy8YFtL_2kL-NnJ-fJHcMM?usp=sharing
Any help on this will be highly appreciated!