1

I have a python code that produces the following figures. I would like to do an annotation with ellipses to surround the curves as the figure mentions.

enter image description here

N.B. The figure is produced using MATLAB and I cannot do it in python-matplotlib. Thanks.

Jika
  • 1,556
  • 4
  • 16
  • 27

1 Answers1

5

You could try the following to position your ellipses: choose an x-coordinate and calculate the height of the ellipse necessary to enclose the provided list of functions at that coordinate.

import matplotlib.pyplot as plt
import numpy as np
from matplotlib.patches import Ellipse

x = np.linspace(1,10,1000)

flogs = [lambda x, a=a: np.log(a * x) for a in range(1,4)]
fexps = [lambda x, a=a: np.exp(a * x) for a in [0.18,0.2]]
funcs = flogs + fexps

fig = plt.figure()
ax = fig.add_subplot(111)

for func in funcs:
    ax.plot(x,func(x))


def add_ellipse(funcs, x):
    # the y-coordinate of the center of our ellipse:
    y = np.mean([funcs[i](x) for i in range(len(funcs))])
    # fix the width of the ellipse to this value:
    w = 0.4
    # find the height of the ellipse needed, and pad a bit:
    h = np.ptp([funcs[i](x) for i in range(len(funcs))]) * 1.5
    e = Ellipse(xy=(x,y), width=w, height=h, angle=0)
    e.set_facecolor('none')
    ax.add_artist(e)

add_ellipse(fexps, 8.5)
add_ellipse(flogs, 8)

plt.show()

enter image description here

xnx
  • 24,509
  • 11
  • 70
  • 109