# 2d scatter plot with ellipse

Id like to create a scatter plot using ellipses instead of circles, stars, etc. The ellipses need to be centered on a specific x,y, and have their major/minor axis defined individually for each ellipse, also the ellipse needs to be able to be rotated on an individual basis. It don’t see any examples of this for plotly.js specifically, is there a way to create this plot?

Not using a scatter trace. You can always resort to using layout shapes like this example.

Is there a convenient way to rotate layout shapes?

1 Like

I think what @sbaile30 was asking is adding confidence ellipse on the scatter plot, i.e. https://www.researchgate.net/post/How_to_draw_a_95_confidence_ellipse_to_an_XY_scatter_plot2.

Is it currently available? If not, can this feature be considered?

1 Like

All I’m really trying to do here is put a geoJSON polygon shape on an orthographic map without the use of mapbox.

I am trying to draw an ellipsoid on rotated lines but facing the same problem here? Is any solution appear or any consideration since 2018? I am using the following function to draw my ellipsoid:

`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 `

But still, the ellipses are not perfect since they do not rotate with the line? Please see the image and any help will be much appreciated?

One of the solutions I found is to include the rotational angel when we creating the ellipsoids.

When we call the function, and we define the rotational angle as follow:

Example-1:

Notice that we define pi/1.93, which will result in the rotational angel in my model. This will be user define and verified visually by the user.

``````
# 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)

``````

Example-2:

Notice that we define pi/1.935 to rotate the ellipse.

``````
# 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)

``````

Example-3

Notice that we define pi/1.921 to rotate the ellipse.

``````
# 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)

``````

Please see the attached figure as an example.