Search code examples
pythongeometrybokehellipse

How to plot an ellipse with bokeh and a correct orientation angle?


I want to plot an ellipse that based on two foci f1 and f2 and a third point. But for some reason the orientation of my ellipse for the given angle I calculated is not displayed correctly.

Am I missing something basic? Why is bokeh not orienting the ellipse with the given angle of pi/4?

Here is my example code:

import numpy as np
from bokeh.io import output_notebook, show
from bokeh.plotting import figure
from bokeh.models import Range1d
output_notebook()

f1 = np.array([0,0])
f2 = np.array([3,3])
point = np.array([1,3])

tools = "hover, box_zoom, undo, crosshair"
p = figure(tools=tools)
p.circle(f1[0], f1[1], radius = 0.05, alpha=0.5)
p.circle(f2[0], f2[1], radius = 0.05, alpha=0.5)
p.circle(point[0], point[1], radius = 0.05, color='red')

center = (f2 - f1)/2 + f1
print(center)

angle = np.arctan2((f2 - f1)[1], (f2 - f1)[0])
print(np.rad2deg(angle))
# minor axis
a = (np.linalg.norm(f2-point) + np.linalg.norm(f1-point))/2
# major axis
c = np.linalg.norm((f2 - f1)/2)
# b = np.sqrt(a**2 - c**2)/2
b = 0.2

p.circle(center[0], center[1], radius = 0.05, color='orange')
p.ellipse(x=center[0], y=center[1], width=2*a, height=2*b, angle=angle, fill_color="yellow", fill_alpha = 0.4)
p.x_range = Range1d(-2, 4)
p.y_range = Range1d(-2, 4)
show(p)

enter image description here


Solution

  • It's a pretty hard problem that has not yet been solved: https://github.com/bokeh/bokeh/issues/7965

    tl;dr: the angle basically uses screen pixel ratio that doesn't correspond to the data range ratio. In your particular case, it's the toolbar and the axes that "squish" the main plot area. If you remove them, then the area will be perfectly square and the angle will be correct.